Int () function in Python
The int () function in Python returns an integer object from any number or string. How is the int () function syntax, what parameters does it have and how is it used? Invites you to read the track.
Syntax of int () function in Python
int(value, base)
Parameters of int function ()
The int () function has 2 parameters:
- value: number or string to convert into an integer
- base: number represents the number format. Default value: 10
Value returned from int ()
The int function returns:
- an integer from the transmitted number or string.
- If there are no parameters, return 0
For example, how does the int () function work?
# integer
print("int(123) là:", int(123))
# float
# viết bởi TipsMake.com
print("int(123.23) là:", int(123.23))
# string
print("int('123') là:", int('123'))
Run the program, the result is:
int(123) là: 123
int(123.23) là: 123
int('123') là: 123
Example 2: Where decimal, octal and hexadecimal?
# nhị phân 0b hoặc 0B
print("Chuyển đổi giá trị nhị phân 1010 sang int là:", int('1010', 2))
print("Chuyển đổi giá trị nhị phân 0b1010 sang int là:", int('0b1010', 2))
# bát phân 0o hoặc 0O
# viết bởi TipsMake.com
print("Chuyển đổi giá trị bát phân 12 sang int là:", int('12', 8))
print("Chuyển đổi giá trị bát phân 0o12 sang int là:", int('0o12', 8))
# thập lục phân
print("Chuyển đổi giá trị thập lục phân A sang int là:", int('A', 16))
print("Chuyển đổi giá trị thập lục phân 0xA sang int là:", int('0xA', 16))
Run the program, the result is:
Chuyển đổi giá trị nhị phân 1010 sang int là: 10
Chuyển đổi giá trị nhị phân 0b1010 sang int là: 10
Chuyển đổi giá trị bát phân 12 sang int là: 10
Chuyển đổi giá trị bát phân 0o12 sang int là: 10
Chuyển đổi giá trị thập lục phân A sang int là: 10
Chuyển đổi giá trị thập lục phân 0xA sang int là: 10
Don't forget to explore other important Python functions that TipsMake.com has introduced.
5 ★ | 1 Vote
You should read it
May be interested
- 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.
- 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.
- 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.
- The compile () function in Pythonthe compile () function returns a code object in python from the specified source. so how to use compile () function? please find out in this article.