Float() is a method that returns a floating-point number for a provided number or string.

Float() returns the value based on the argument or parameter value that is being passed to it. If no value or blank parameter is passed, it will return the values 0.0 as the floating-point output.

Python float values are represented as 64-bit double-precision values. 1.8 X 10308 is an approximate maximum value for any floating-point number. If it exceeds or exceeds the max value, Python returns an error with string inf (infinity).

## float () Syntax

Every inbuilt function in the python programming language has a predefined objective and a syntax. The term syntax refers to how a particular function needs to be used or called. In the below line, we can learn more about the syntax of the float() function in the python programming language.

Syntax: The syntax of the inbuilt float() function: float(X)

Where X is an input parameter passed to the function when it is called in the program.

## float () Parameters

One of the salient features of the python programming language is that it has rich predefined libraries with numerous methods defined in it. We can use them by just calling the function. While a function is being called, the necessary syntax must be followed and suitable parameters must be passed to the function. Here the term parameters refer to the input arguments given to the method by the user. In the next lines, let us understand more about the parameter of the float() function.

Calling float function: float(X)

Here, the variable X is called the parameter to that function. X can be a normal integer value or any string that has decimal points. An interesting fact about the float() is that the parameter is optional. ## float () Return Value

Python programming language has many inbuilt libraries and functions. One among them is the float() function. With the help of the float() function, we can convert an input string or an integer value to a floating point value. We already knew that it is not mandatory to send a parameter to the float(), in such a case the function returns 0.0 as output. On another side, the float() function returns the corresponding floating-point value when an integer or string with decimals is given as an input argument. If any input value is out of range of floating value in python, then it returns OverflowError.

## Float () Function Example 1

The following is what float() returns for 3 i.e. Integer. Output Example 2: This is what float() returns for 5.5 i.e. decimal. Output

### FREE Data Science With Python CourseStart Learning Data Science with Python for FREE Examples of float() With String Parameter:

Example 1: This is what float() returns for “5.5” i.e. String. Output Example 2: This is what float() returns for “5.5” i.e. String with white spaces. Output Example 3: This is what float() returns for “5.500” i.e. String, which is decimal. Output Example 4: This is what float() returns for “-15.5 \n” i.e. a string, which is a negative decimal. Output Example 5: This is what float() returns for “Cucumber” i.e. String, which is not an integer or decimal. Output–float() returns an error, as the string is not an integer or decimal.

### Examples of float() With Infinity:

Example 1: This is what float() returns for 1.82e310 i.e. an integer that exceeds the maximum value of the Python floating-point number. Output

### Python Training CourseLearn Data Operations in Python Examples of float() With Not a Number, i.e. NaN as a String.

Example 1: This is what float() returns for “NaN” i.e. String, which is not a number. Output

### Examples of float() With Infinity, i.e. Infinity as a String.

Example 1: This is what float() returns for “Infinity” i.e. String. Output Example 2: This is what float() returns for “inf” i.e. String. Output

## Example: How does float () work with Python?

With this example, let us get a clearer understanding of the float() in the python programming language.

Snippet 1: print(float())

Output 1: 0.0

Snippet 2: print(float(17))

Output 2: 17.0

Snippet 3: print(float(10.02))

Output 3: 10.02

Snippet 4: print(float('podium'))

Output 4: ValueError: could not convert string to float: 'podium'

Snippet 5: print(float(-8))

Output 5: -8.0

Snippet 6: print(float('1234'))

Output 6: 1234.0

Snippet 7: print(float(123456789123499999991111111111111111111122255555555555555555555555555555555555555555555550000000000000000000011111111111111111111111111111111111111111111111188888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888555555555555555555555555555555555555555555555555555555555555555555555555555555522222222222222222222222222222222222222222))

Output 7: OverflowError: int too large to convert to float

### Example: float() for infinity and NaN (Not a Number)?

With this example, let us get a clearer understanding of the float() function in the programming language when infinity and NaN (Not a Number) values are given as input parameters to the float() function.

Snippet 8: print(float("nan"))

Output 8: nan

Snippet 9: print(float("NaN"))

Output 9: nan

Snippet 10: print(float("inf"))

Output 10: inf

Snippet 11: print(float("InF"))

Output 11: inf

Snippet 12: print(float("InFiNiTy"))

Output 12: inf

Snippet 13: print(float("infinity"))

Output 13: inf

## Converting an Integer to Float in Python

As we discussed earlier, we can pass an integer value to the float() function as an input argument and get its equivalent floating point value as output. Let us understand this with the help of an example.

Snippet 14: print(float(23))

Output 14: 23.0

Snippet 15: print(float(112000))

Output 15: print(float(112000))

## Converting a String to Float in Python

As we discussed earlier, we can pass a string value to the float() function as an input argument and get its equivalent floating point value as output (given condition is that the string must be numerical). Let us understand this with the help of an example.

Snippet 16: print(float('python'))

Output 16: ValueError: could not convert string to float: 'python'

Snippet 17: print(float('64'))

Output 17: 64.0

### Additional Methods That Are Available In Float():

Float() also has some additional methods that are useful to identify finite integers, decimal from hexadecimal strings, etc. To understand this better, it makes sense to see it in-detail with examples.

## What is float.as_integer_ratio()?

Float.as_integer_ratio() is a built-in function that provides a pair of integers whose ratio is equivalent to the original float provided.

Syntax: float.as_integer_ratio()

return Tupple (integer pair)

Example: Output ## What is float.is_integer()?

float.is_integer() is used to identify whether the given float instance is a finite integer or not. If it is finite, the integer returns true, else it returns false.

Example: Output

## What is float.hex()?

float.hex() returns the hexadecimal string for the given floating number.

Example Output

## What is float.fromhex()?

float.fromhex() returns the floating-point number which represents the given hexadecimal string.

Example: Output Looking forward to making a move to the programming field? Take up the Python Training Course and begin your career as a professional Python programmer

## Conclusion:

Float() in python is an important method that is useful to represent floating-point numbers. It is used to represent real numbers and is written with the decimals dividing the integer and fractional parts.

In this article, you have learned what float() is, the syntax for float(), and additional float() methods along with good working examples. You also saw how to define float(), how to print, as well as get the output from a float, etc. To learn more about this topic, enroll in the Python Training Course from Simplilearn, to learn the fundamentals concepts of Python, including data operations, Django, conditional statements, shell scripting, and much more. This certification course consists of 38 hours of Blended Learning and 8 hours of online self-paced learning, which will provide you with practical programming experience and train you for a rewarding career as a professional Python programmer.

In this article, you have learnt about Float(), syntax for Float() in Python as_integer_ratio() in Float(), is_integer() in Float(), hex() in Float(), and fromhex() in Float() with a working example.

If you are on the other hand looking to learn the specific applications of Python in Data Science, and perhaps move into a career in Data Science, join Simplilearn’s Data Science with Python Certification Course. This completely online certification program on Python Data Science will help you learn the essential Python programming concepts and gain expertise in key data science-related concepts including, data mining and visualization, machine learning, web crawling, natural language processing and much more. By completing this Python for Data Science Training program, you will master the fundamental skills and tools of Data Science with Python and become job-ready.

This online certification course in Data Science with Python features 68 applied learning hours, interactive learning with Jupyter notebooks laboratories, 4 business-based assignments, self-paced learning modules available to you lifelong, and dedicated mentoring sessions from industry experts.

Do you have any questions or feedback for us on this float() in python article? Do share them with us in the comments section of this article, and our experts will review and get back to you soon. Simplilearn