Python built-in int function detailed description

Source: Internet
Author: User
English documents:

class int (x=0) class int (x, base=10)

Return an integer object constructed from a number or string x, or return 0 if no arguments is given. If x is a number, return x.__int__ (). For floating point numbers, this truncates towards zero.

If x is not a number or if base is given, then x must are a string, bytes, or ByteArray instance representing an integer li Teral in Radix base. Optionally, the literal can be preceded by + or – (with no space in between) and surrounded by whitespace. A base-n literal consists of the digits 0 to n-1 and with A to Z (or A to Z) has the values of 35. The default base is 10. The allowed values are 0 and 2-36. Base-2,-8, and-16 literals can be optionally prefixed with 0b/0b, 0o/0o, or 0x/0x, as with the integer literals in code. Base 0 means to interpret exactly as a code literal, so then the actual base is 2, 8, ten, or, and so that int (' 010 ', 0) Is isn't legal, while INT (' 010 ') are, as well as int (' 010 ', 8).

Description

1. When the parameter is not passed in, the result 0 is obtained.

>>> Int () 0

2. When passing in a value, call its __int__ () method, and the floating-point number will be rounded down.

>>> Int (3) 3>>> int (3.6) 3

3. When a string is passed in, the conversion is made by default in 10 binary.

>>> int (' 1 ') 36>>> int (' 3.6 ') Traceback (most recent call last):  File "<pyshell#2>", line, In <module>    int (' 3.6 ') valueerror:invalid literal for int. () with base 10: ' 3.6 '

4. The string is allowed to contain "+", "-" number, but plus minus and number can not have spaces, after the value, the symbol can appear before the space.

>>> int (' +36 ') 36>>> int (' -36 ') -36>>> int ('   -36        ') -36>>> int ('--        Traceback (most recent):  File "<pyshell#7>", line 1, in <module>    int ('-        ') valueerr Or:invalid literal for int. () with base 10: '-36

5. When the string is passed in, and the binary is specified, the string is converted to a 10-binary integer by the corresponding binary.

>>> int (' 0f ', 2) 1>>> int (' 2>>> ', 3) ' int ' (' "', ' 8 ') ' 7>>> int ' (' ', 16) 15
Related Article

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.