# float () in Python

Python Methods and Functions

The float () method is used to return a floating point number from a number or string.
Syntax :

`float(x)`

The method takes only one parameter, which is also optional. Let's take a look at the different types of arguments, the method takes:

1. Number: can be an integer or a floating point number.
2. String :
• Must contain numbers of any type.
• Any spaces to the left or right or new lines are ignored by the method.
• Math operators can be used.
• May contain NaN, Infinity or inf (whichever occurs)

Values ​​that the float () method can return depending on the argument passed

• If an argument is passed, an equivalent floating point number is returned.
• If no argument is passed, the method returns 0.0.
• If any string is passed that is not a decimal number or does not match any of the cases mentioned above, an error is raised.
• If the number is out of the Python range, an OverflowError is thrown.

Now let's p Let's look at various examples and how the float () method works.

 ` # Python program for illustration ` ` # Various examples and working with floating point () ` ` # for integers ` ` print ` ` (` ` float ` ` (` ` 21.89 ` `)) `   ` # for floating point numbers ` ` print ` ` (` ` float ` ` (` ` 8 ` `)) `   ` # for lines of integer type ` ` print ` ` (` ` float ` ` (` ` "23" ` `)) `   ` # for float lines ` ` print ` ` (` ` float ` ` (` ` "- 16.54" ` ` )) `   ` # for floating point lines with spaces ` ` print ` ` (` ` float ` ` (` ` " -24.45 " ` `)) `   ` # for inf / infinity ` ` print ` ` (` float ` (` ` "InF" ` `)) ` ` print ` ` (` ` float ` ` (` ` "InFiNiTy" ` `)) `   ` # for NaN ` ` print ` ` (` ` float ` ` (` ` "nan" ` `)) ` ` print ` ` (` ` float ` ` (` ` "NaN" ` ` )) `   ` # Finally generated error ` ` print ` ` (` float ` (` ` "Geeks" ` `)) `

Output:

` 21.89 8.0 23.0 - 16.54 -24.45 inf inf nan nan `

All lines are executed correctly, but the last one that will throw an error:

` Traceback (most recent call last): File "/home/21499f1e9ca207f0052f13d64cb6be31.py" , line 25, in print (float ("Geeks")) ValueError: could not convert string to float: 'Geeks' `