float() is a part of the built-in function of the Python Interpreter.
Syntax:class float(x=0.0)
The float() function returns a floating point number from the passed in number or string x.
Example 1: passing a String argument
>>> float('10.5')
10.5
>>> float('-10.5')
-10.5
>>> float('10')
10.0
>>> float('-10')
-10.0
>>> float('a')
Traceback (most recent call last):
File "", line 1, in <module>
ValueError: could not convert string to float: 'a'
Documentation: https://docs.python.org/3/library/functions.html#float

Example 2: passing exponential values:
>>> float('2e3')
2000.0
>>> float('-2e3')
-2000.0
Example 3: When no argument is passed
When no argument is passed, the value is set as 0.0
>>> float()
0.0
Example 4: NaN and infinity
>>> float('NaN')
nan
>>> float('Infinity')
inf
>>> float('-Infinity')
-inf
Python 3 Code example with float()
Add two float numbers
float1 = float(input("Enter float number 1: "))
float2 = float(input("Enter floart number 2: "))
sum_of_two_numbers = float1 + float2
print(f"Sum of {float1} and {float2} is {sum_of_two_numbers}")
Output:
Enter float number 1: 1.4
Enter float number 2: 2.4
Sum of 1.4 and 2.4 is 3.8
-
Have Questions? Post them here!
More Posts related to Python,
- Check version of pip package installer for Python
- How to install Python 3.9 using brew on Mac
- Python Program To Calculate Simple Interest (SimpleInterest.py)
- Indent Python code in Notepad++
- Change label (text) color in tkinter
- Float built-in function in Python
- [Fix] ValueError: substring not found in Python
- pip get list of all outdated Python packages
- Calculate discount amount python code
- 3 Python program to add two numbers
- How to List all Packages installed using pip [Python]
- Python range() function examples
- How to resolve Failed to create interpreter PyCharm Error
- Format Python Code in Visual Studio Code (VS Code)
- Validate email address in Python using regular expression (regex)
- Install and Run Jupyter Notebook on Mac (macOS)
- How to delete a file using Python code example
- Base64 Encoding Decoding in Python Programming
- How to pip install Python Modules in VSCode
- Change the background of Tkinter label or text
- Comments in Python Programming
- Take input argument from command line in Python Programming
- How to Convert Python String to DateTime Object
- Tkinter - add x and y padding to label text
- [Tutorial] Install Python on Visual Studio Code (VS Code)
More Posts:
- SharePoint error - An exception occurred when trying to issue security token: This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms.. - SharePoint
- Move Copy Migrate SharePoint OneDrive files folders to different site collection location - SharePoint
- Add blank lines after each lines using Notepad++ text editor - NotepadPlusPlus
- Call PHP function on Button click using jquery ajax - PHP
- How to recover unsaved notepad file Windows 10 - NotepadPlusPlus
- List of 60 useful FTP Client Commands to access server - FTP
- How to Change Mouse Wheel Scroll Direction on Mac - MacOS
- Find Difference of two numbers - C-Program