Python built-in functions (33) -- int, python built-in 33int

Source: Internet
Author: User

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 return0If 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, OrbytearrayInstance 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,aToz(OrAToZ) 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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.