Float() In Python

What is the Float() function?

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).

Syntax:

The syntax for the float() method is float([x]). Here the x is an optional parameter and can be either a number or a string.

Now, let’s understand Float() parameters with better clarity.

As already mentioned above, the float() parameter is optional only. Having said that, the float() method or function takes only a single parameter. This single parameter can be any type of the following, i.e., Integer (Number), String (Text), and Floating Number (Decimal).

FloatInPython_1

The Output Returned By float() 

  • When the parameter is passed, the float() returns the floating-point or decimal number which is equivalent to the parameter of the integer or string that is being passed.
  • If no parameter is passed, then the float() returns 0.0 as a floating-point or decimal number.
  • Float() will return the error on the following couple of cases.

i) If you pass a string that does not contain a decimal point number.

ii) If you pass an integer that is outside the Python float() range.

Float () Function Example 1

The following is what float() returns for 3 i.e. Integer. 

FloatInPython_2.

Output

/FloatInPython_3

 Example 2: This is what float() returns for 5.5 i.e. decimal.

FloatInPython_4

 Output

FloatInPython_5.

FREE Data Science With Python Course

Start Learning Data Science with Python for FREEStart Learning
FREE Data Science With Python Course
Examples of float() With String Parameter:

Example 1: This is what float() returns for “5.5” i.e. String.

FloatInPython_6

Output

FloatInPython_7.

 Example 2: This is what float() returns for “5.5” i.e. String with white spaces.

FloatInPython_8

 Output

/FloatInPython_9.

Example 3: This is what float() returns for “5.500” i.e. String, which is decimal.

FloatInPython_10

 Output

FloatInPython_11

Example 4: This is what float() returns for “-15.5 \n” i.e. a string, which is a negative decimal.

FloatInPython_12

 Output

FloatInPython_13

Example 5: This is what float() returns for “Cucumber” i.e. String, which is not an integer or decimal.

FloatInPython_14

 Output–float() returns an error, as the string is not an integer or decimal.

FloatInPython_15

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.

FloatInPython_16.

Output

/FloatInPython_17

Python Training Course

Learn Data Operations in PythonExplore Course
Python Training Course
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.

FloatInPython_18

 Output

FloatInPython_19

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

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

FloatInPython_20.

 Output

FloatInPython_21

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

FloatInPython_22.

Output

FloatInPython_23

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:

FloatInPython_24

Output 

/FloatInPython_25.

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: 

FloatInPython_26.

Output

FloatInPython_27.

What is float.hex()?

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

Example

FloatInPython_28.

Output

FloatInPython_29

What is float.fromhex()?

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

Example:

/FloatInPython_30

Output

FloatInPython_31 

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. 

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.