Python float() Built in Function
The float() function in Python is used to convert a value into a floating point number, which is a number with a decimal point. For example, float(3) returns 3.0 and float(“5.5”) returns 5.5. You can also use float() with numbers that are too large or too small for Python to represent as integers, such as 1.82e310 or -32.54e100. Here are some examples of using float() in Python:
# Example 1: Convert an integer to a float
x = 25
y = float(x)
print(y) # Output: 25.0
# Example 2: Convert a string to a float
s = "12.34"
z = float(s)
print(z) # Output: 12.34
# Example 3: Convert an infinity value to a float
inf = "infinity"
w = float(inf)
print(w) # Output: inf
If you have any questions about this code, you can drop a line in comment.
Comments
Post a Comment