How to Use float () Function in Python

How to Use float () Function in Python

2 mins read268 Views Comment
Vikram
Vikram Singh
Assistant Manager - Content
Updated on Oct 30, 2023 13:40 IST

Float function is a built-in function in python that returns the floating-point number of a numeric value or a string representation of a numeric value. In this article, we will discuss how to use float function in python with the help of examples.

2023_01_MicrosoftTeams-image-129.jpg

In any programming language, there are different data types that are stored in variables, so in Python. Data type in python is broadly divided into two different types: Mutable and Immutable.

In this article, we will discuss one of the types of python number functions, i.e., the float() function in Python.

Must Read: Data Types in Python

So, let’s dive deep to learn more about what is float function in Python and how to use them.

What is the float () function in Python?

The float () function in Python returns a floating-point number of a number or a string representation of a numeric value. Floating point numbers are decimal numbers like 9.0, 45.2, etc.

Syntax

 
float (value)
Copy code

Here, the parameter value is optional; it can be a regular integer or a string. Python float () function returns

  1. a floating-point number.
  2. If no value assigns it will return 0.0.
  3. If string is passed without number, it will return an error.
What is Programming What is Python
What is Data Science What is Machine Learning

Example-1: Convert integer value to float number.

 
#convert integer to decimal number
float(2)
Copy code

Output

Example – 2: Convert string value to float number

 
#convert string number to decimal number
float("2")
Copy code
#no value assigned to the float function
float()
Copy code

Output

Example – 4: Use float function with string.

 
#convert string (without having any number) to float
float("infoEdge")
Copy code

Output

Example – 5: Use float function with infinity

 
# float() function with infinity
print(float("inf"))
print(float("InF"))
print(float("InFiNiTy"))
print(float("infinity"))
Copy code

Output

2023_01_image-66.jpg

Example – 6: Use float function with NaN (Not a Number)

 
# float() function with NaN
print(float("nan"))
print(float("NaN"))
Copy code

Output

2023_01_image-67.jpg
Recommended online courses

Best-suited Python courses for you

Learn Python with these high-rated online courses

Free
6 weeks
– / –
3 months
– / –
2 weeks
– / –
16 weeks
β‚Ή1.7 K
3 months
– / –
– / –
β‚Ή4.24 K
2 weeks
β‚Ή3 K
3 weeks
– / –
4 months
About the Author
author-image
Vikram Singh
Assistant Manager - Content

Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio