Float in python returns a float value from either a string or an integer. Float in python takes only one argument as its parameter. Let us learn this in detail.
Syntax:
float(a)
‘a’ is a number or a string that is converted to the floating value.
float() Parameters
The type of arguments that the float () accepts is mentioned below:
- A number- the float() accepts an integer or floating numbers as an argument.
- Strings- the conditions for the string argument to be considered are given as below:
- The argument must contain numbers of any type.
- Any whitespaces on either side of the argument or a new line are ignored by the method.
- Mathematical Operators can be utilized.
- It’s possible to have NaN, Infinity, or infinity (any cases)
Return value from float()
Based on the type of arguments passed to the float(), the return type will be:
- The equivalent floating-point number is returned if an argument is passed.
- If there are no arguments passed as input, the float() returns 0.0
- An error will be raised if any string is provided that is not a decimal point number or does not match any of the criteria listed above.
- If a number is passed that is outside of the Python float range, an OverflowError is returned.
Errors:
If any improper input is provided, an appropriate exception is returned.
- ValueError – When you pass an invalid argument, such as a string that doesn’t contain a number
- TypeError – When you pass a type argument that it doesn’t recognize, such as a complex number or NoneType, it throws a TypeError exception.
What is Float Used for?
Float() is a Python function that returns the result of converting an integer or a string to a float value.
Example 1: How float() works in Python?
# for integer arguments
print(float(101))
# for float arguments
print(float(311.22))
# for string float arguments
print(float("-14.33"))
# for string floats that contain whitespaces
print(float(" -24.35\n"))
# string float error
print(float("Hey, what a beautiful day"))
Output
101.0
311.22
-14.33
-24.35
ValueError: could not convert string to float: 'Hey, what a beautiful day."
Example 2: float() for infinity and Nan(Not a number)?
# for NaN
print(float("nan"))
print(float("NaN"))
# for inf/infinity
print(float("inf"))
print(float("InF"))
print(float("InFiNiTy"))
print(float("infinity"))
Output
nan
nan
inf
inf
inf
inf
What is a float input?
If the user wants to take a float integer as the input, the float() must be used to explicitly convert the string value into a float integer.
Example:
a = float(input(“Enter a float value: “))
b = float(input(“Enter a float value: “))
Source Code:
a = float(input(“Enter a float value: “))
b = float(input(“Enter a float value: “))
print(“Sum of a and b:”,a + b)
print(“Multiplication of a and b:”,a * b)
Output:
Enter a float: 23.43
Enter a float: 34.32
Sum of a and b: 57.75
Multiplication of a and b: 804.1176
What is a float variable?
The float variable in Python represents a floating-point number. A decimal point divides the integer and fractional elements of the float variable used to represent the real values. The float variables have 64-bit precision values. The floating-point numbers that have a value greater than 1.8 x 10^308 exceed the range. In such cases, the number is represented by the string inf in Python.
Example:
print(1.7e308)
# greater than 1.8 * 10^308
print(1.82e308)
Output:
1.7e+308
inf
What are Floats and Integers in Python?
Integers, floating-point numbers, and complex numbers are all supported by the Python programming language.
Float and integers are variables accepted in Python. A decimal point decides whether the number is an integer or a floating point.
In Python, if you declare a number without a decimal point it is automatically considered an integer. The values that have a decimal point (e.g., 6.00, 2.543, 233.5, 1.0) are referred to as float.
Positive whole numbers (e.g., 8, 37, 226), their negative counterparts (e.g., -8, -37, -226), and zero are all examples of integers. In Python, integer variables are written as int.
Converting an integer value to a Floating-point in Python
The float() is used to convert an integer to float. For example:
Input:
f_numb = float(15)
print(f_numb)
Output:
15.0
After using the float function, we can see the output as 15.0, this indicates that the number has been converted successfully.
Leave a Reply