float() in Python | Python float() Function

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

In Python Programming, float() is an inbuilt function that converts an integer or specific strings into floating-point values.

Syntax of float() function in Python

Parameters of float() function in Python

It accepts only one parameter but it's optional:

  • x (Optional) - number or string that needs to be converted to floating point number If it's a string, the string should contain decimal points.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Return values of float() function in Python

Argument typeInputReturn valueOutput
No argument is passed-0.00.0
Integer4Floating point number4.0
Floating point number30.0Floating point number30.0
String"20"Floating point number20.0
String"nan", "NaN"nannan
String"inf", "InF", "InFiNiTy", "infinity"infinf
String"Scaler"ValueError: could not convert string to float : 'Scaler'-

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Example of float() function in Python

Let's look at an example to convert string 30 to floating-point value 30.0:

Code:

Output

What is float() function in Python?

In all programming languages, typecasting of the data has been a important feature.

Python provides us an inbuilt function to convert integers and few strings to floating point numbers. Let's see how float in Python works with different parameters:

  • If no argument is passed then 0.0 is returned.
  • If any number is passed, then floating point number is returned.
  • If a string is passed which is not a decimal point number or strings mentioned above, then ValueError is raised.
  • If a number passed is outside the range of Python float() then OverflowError is generated.

More Examples

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Example 1: How float() works in Python?

Python code to explain the working of the float() in Python.

Code:

Output:

Explanation:

The last string passed does not have a decimal point number hence the code returns ValueError.

Example 2: Converting a string into a floating-point number

Code:

Output:

Explanation:

The string passed into the float() in the Python program contains integers and hence the program returns the required floating-point value.

Example 3: Using float() with infinity and Nan(Not a number)

Code:

Output:

Only for the specified strings, the float() in Python returns the above output.

Example 4: Converting an Integer to a Float in Python

Code:

Output:

Explanation:

The above code represents when an integer is passed into the float function. The code will always return the required floating-point value in such cases.

Example 5: Python float() Exception

Code:

Output:

Explanation:

In the above code, a string is passed to the float() function to check if it is a decimal point value. But it turns out not to be, hence the code returns an error.

Conclusion

  • float() in Python is an inbuilt function that is used to convert any number or specific strings to a floating-point number.These specific strings are: "nan", "NaN", "inf", "InF", "InFiNiTy", "infinity".
  • If other strings are given as input then the code returns an error i.e., ValueError.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more