The float () function in Python
The float () function in Python converts the value specified to a floating-point number. How does the float () function have a syntax, what parameters do you have, how can you find out in this article with Quantrimang?
The float () function syntax in Python
float([x])
Parameters of float () function:
float () has only one parameter:
- x : number or string to be converted into a floating-point number
The function accepts value types that can be parameterized as follows:
- Number: May be an integer or a decimal.
- Chain:
- Contains any type of number.
- The left or right whitespace or a new line of values are ignored by the program.
- Can use mathematical operators.
- Can be used with NaN, Infinity or inf (both lower and upper case).
Value returned from float ()
The values returned by the float () function depend on the parameter passed.
- If the parameters are passed in accordance with Python requirements, the result returned is a corresponding floating point number.
- If no parameters are passed, the function returns 0.0.
- If the passed parameter is not a decimal number or does not match any of the cases mentioned above, the program will report an error.
- If a number is passed outside of the Python float, the output will occur exception OverflowError .
Example 1: How does the float () function work?
# viết bởi TipsMake.com
# đối với số nguyên
print(float(10))
# đối với số thập phân
print(float(11.22))
# đối với chuỗi
print(float("-13.33"))
# đối với chuỗi có khoảng trắng
print(float(" -24.45n"))
# chuỗi gây ra lỗi
print(float("abc"))
Run the program, the result is:
10.0
11.22
-13.33
-24.45
ValueError: could not convert string to float: 'abc'
Example 2: float () with infinity and Nan (Not a number)?
# viết bởi TipsMake.com
# đối với NaN
print(float("nan"))
print(float("NaN"))
# đối với inf/infinity
print(float("inf"))
print(float("InF"))
print(float("InFiNiTy"))
print(float("infinity"))
Run the program, the result is:
nan
nan
inf
inf
inf
inf
The float () function has only a few notes like that. Remember to practice regularly with Python exercises.
Previous lesson: eval () function in Python
Next lesson: exec () function in Python
4 ★ | 2 Vote
You should read it
May be interested
- The function set () in Pythonin this article, tipsmake.com will learn with you about set (), syntax, parameters and specific examples. invites you to read the track.
- Help () function in Pythonthe built-in help () function in python is used to display documents and invoke the help system of modules, functions, classes, keywords ...
- Sum () function in Pythonthe built-in function sum () in python returns the sum of all numbers in iterable.
- The slice () function in Pythonthe slice () function in python returns a slice object that helps you determine how to cut an existing string.
- The chr () function in Pythonin python, the chr () function returns a character (a string) from an integer that represents the unicode of the returned character.
- The iter () function in Pythoncontinuing with the topic of python's built-in functions, the article will introduce you to the iter () function with the syntax, usage as well as specific examples. invites you to read the track.
- The pow () function in Pythonthe pow () function built into python returns the value of x with the power of y (xy). if there is a third parameter, the function returns x to the power y, the module z.
- The reversed () function in Pythonthe reversed () function is one of the built-in functions in python, used to reverse the original string returning the iterator.
- Exec () function in Pythonthe exec () function used to execute python programs dynamically can be string or object code. how does exec () function syntax, what parameters do it have, and how is it used? invites you to read the track.
- Built-in Python functionsin the previous article, you learned that python has a basic function type, one is a built-in function, two are user-defined functions. today, we will learn about a list of python's built-in functions.