How to Use float () Function in Python
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.
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)
Here, the parameter value is optional; it can be a regular integer or a string. Python float () function returns
- a floating-point number.
- If no value assigns it will return 0.0.
- If string is passed without number, it will return an error.
Example-1: Convert integer value to float number.
#convert integer to decimal number
float(2)
Output
Example β 2: Convert string value to float number
#convert string number to decimal number
float("2")
Ouput
Programming Online Courses and Certification | Python Online Courses and Certifications |
Data Science Online Courses and Certifications | Machine Learning Online Courses and Certifications |
Example β 3: Assign no value to float function.
#no value assigned to the float function
float()
Output
Example β 4: Use float function with string.
#convert string (without having any number) to float
float("infoEdge")
Output
Example β 5: Use float function with infinity
# float() function with infinity
print(float("inf"))print(float("InF"))print(float("InFiNiTy"))print(float("infinity"))
Output
Example β 6: Use float function with NaN (Not a Number)
# float() function with NaN
print(float("nan"))print(float("NaN"))
Output
Best-suited Python courses for you
Learn Python with these high-rated online courses
Conclusion
In this article, we have discussed, how to use float function in python with the help of example.
Hope, you will like the article.
Top Trending Article
Top Online Python Compiler | How to Check if a Python String is Palindrome | Feature Selection Technique | Conditional Statement in Python | How to Find Armstrong Number in Python | Data Types in Python | How to Find Second Occurrence of Sub-String in Python String | For Loop in Python |Prime Number | Inheritance in Python | Validating Password using Python Regex | Python List |Market Basket Analysis in Python | Python Dictionary | Python While Loop | Python Split Function | Rock Paper Scissor Game in Python | Python String | How to Generate Random Number in Python | Python Program to Check Leap Year | Slicing in Python
Interview Questions
Data Science Interview Questions | Machine Learning Interview Questions | Statistics Interview Question | Coding Interview Questions | SQL Interview Questions | SQL Query Interview Questions | Data Engineering Interview Questions | Data Structure Interview Questions | Database Interview Questions | Data Modeling Interview Questions | Deep Learning Interview Questions |
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