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 "<stdin>", 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
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Python,
- Python: Convert Date to DateTime
- How to sort a List using Lambda in Python
- Python matplotlib segmentation fault: 11 macOS Big Sur
- What is Terminal Velocity and its Formula? How to calculate it programmatically?
- How to install Python 3.11 on Mac
- How to flatten a nested list in Python
- Python: Pandas Merge DataFrames on Index Example
- How to Run all Cells at Once Jupyter Notebook
- Python - Convert float to String
- How to add borders to tkinter label text
- How to Exit a Loop in Python Code
- [Python] Fix: ValueError: All arrays must be of the same length
- Sorting an array using Bubble Sort in Python Programming
- How to Unzip a file using Python
- Python: Merge DataFrames Pandas Outer Join Example
- Change label (text) color in tkinter
- Convert Float to String in Python
- Fix: fatal error: No such file or directory compilation terminated
- Python: Access index/counter of a for loop iteration
- Import Other Python Files Examples
- How to install Anaconda on Mac (M1/M2 Mac)
- Python Regular Expression to Find All Matches in List
- How to Read a binary File with Python
- How to disable warnings while Python file execution
- Know current Python Version
More Posts:
- Simple Login Page using Bootstrap - Bootstrap
- How to hide or display Wifi icon in macOS Bug Sur Menu Bar - MacOS
- Cant find System Preferences... under macOS Ventura Apple Menu? - MacOS
- How to enter Rockstar Activation Code? - HowTos
- Obsolete marquee element alternatives html5 - Html
- Fix: zsh: command not found: adb (Android) - MacOS
- Increase Font Size of Eclipse Java Code - Eclipse
- How to add Sleep to PowerShell Script - Powershell