Python built-in functions (33) -- int, python built-in 33int
English document:
Classint
(X = 0)Classint
(X,Base = 10)
Return an integer object constructed from a number or stringX, Or return0
If no arguments are given. IfXIs a number, returnx.__int__()
. For floating point numbers, this truncates towards zero.
IfXIs not a number or ifBaseIs given, thenXMust be a string,bytes
, Orbytearray
Instance representing an integer literal in radixBase. Optionally, the literal can be preceded+
Or-
(With no space in between) and surrounded by whitespace. A base-n literal consists of the digits 0 to n-1,a
Toz
(OrA
ToZ
) Having values 10 to 35. The defaultBaseIs 10. The allowed values are 0 and 2-36. Base-2,-8, and-16 literals can be optionally prefixed0b
/0B
,0o
/0O
, Or0x
/0X
, As with integer literals in code. Base 0 means to interpret exactly as a code literal, so that the actual base is 2, 8, 10, or 16, and so thatint('010', 0)
Is not legal, whileint('010')
Is, as wellint('010', 8)
.
Note:
1. If no parameter is input, the result 0 is displayed.
>>> int()0
2. When a value is passed in, the _ int _ () method is called, and the floating point number is rounded down.
>>> int(3)3>>> int(3.6)3
3. When a string is input, the conversion is in decimal format by default.
>>> int('36')36>>> int('3.6')Traceback (most recent call last): File "<pyshell#2>", line 1, in <module> int('3.6')ValueError: invalid literal for int() with base 10: '3.6'
4. A string can contain "+" and "-", but there must be no space between the plus or minus sign and the value. spaces can appear before and after the value.
>>> int('+36')36>>> int('-36')-36>>> int(' -36 ')-36>>> int(' - 36 ')Traceback (most recent call last): File "<pyshell#7>", line 1, in <module> int(' - 36 ')ValueError: invalid literal for int() with base 10: ' - 36
5. Input a string and specify the hexadecimal notation. Then, convert the string to a 10-hexadecimal integer in the hexadecimal notation.
>>> int('01',2)1>>> int('02',3)2>>> int('07',8)7>>> int('0f',16)15