Python float() Function Explained with Examples
In this article, we will see what is and various examples of “Python Float Function”. It will help you in implementing this concept and get a tight grip over this.
What is the Python float() method?
The Python float() function is a built-in Python function that converts the given value (number or string) into a floating-point number and returns the result. If the float() method fails to convert string to float then it raises the ValueError.
The Syntax of float() is:
float(Parameter value)
Parameter Value:
A number or a string that can be converted into a floating-point number.
Parameter is optional. If there is no parameter then it returns 0.0.
Note: If the value is a string then it should contain a decimal point and the valid argument should contain some numeric value.
Return Values
The float() function returns a floating-point value equivalent to the number passed as is or in the form of a string.
Errors
This method raises exceptions when it receives invalid parameter value.
- ValueError
- TypeError
ValueError – Python ValueError is raised when a Python function receives an argument of the correct type but a wrong value.
TypeError – Python TypeError is raised when a Python function receives an argument is not of the expected type
Python float() function examples
Example 1: float() function with +ve number values
Let’s pass positive values and see how float() method converts to an equivalent floating-point value.
print(float(0)) print(float(1)) print(float(0.0)) print(float(1.0)) print(float(1.1001)) print(float(100))
Output:
0 1 0.0 1.0 1.1001 100
Example 2: float() function with -ve number values
Let’s pass negative values and see how float() method converts to an equivalent floating-point value.
print(float(-1)) print(float(-1000)) print(float(-0.0)) print(float(-1.1001)) print(float(-1.0))
Output:
-1.0 -1000.0 -0.0 -1.1001 -1.0
Example 3: float() function with a string containing numbers
Let’s pass a number in string format and see how float() method converts to an equivalent floating-point value and returns the result.
print(float('-1')) print(float('0.0')) print(float('-1000')) print(float('1.1001')) print(float(' 1.0000 '))
Output:
-1.0 0.0 -1000.0 1.1001 1.0
Note: It ignores the leading and trailing spaces
Example 4: float() for Infinity, Inf and Nan(Not a number)
It also accepts words like infinity and Nan(Not a number).
print(float('inf')) print(float('InF')) print(float('InFiNiTy')) print(float('infinity')) print(float('nan')) print(float('NaN'))
Output:
inf inf inf inf nan nan
Example 5: float() with invalid inputs
Let’s pass some invalid input values and find how float() method operates with the wrong parameter values.
inputValues = [None, "STM", "0,1", "1 0", 1+2j] for everyItem in inputValues: try: if isinstance(everyItem, str): print("float('{}') = {}".format(everyItem, float(everyItem))) else: print("float({}) = {}".format(everyItem, float(everyItem))) except Exception as ex: print("float({}) = {}".format(everyItem, ex))
Output:
float(None) = float() argument must be a string or a number, not 'NoneType' float(STM) = could not convert string to float: 'STM' float(0,1) = could not convert string to float: '0,1' float(1 0) = could not convert string to float: '1 0' float((1+2j)) = can't convert complex to float
Also read:
- Python Strings
- Python Data Types
- Python Multiline String
- Python Join() Method with Examples
- Python Interview Questions
- How to Convert Python List to String (4 Ways)