python int () function instance detailed

Source: Internet
Author: User
int (x, [base])

Function:

The function is to convert a string of a number or base type into an integer. (Python video tutorial)


Function Prototypes:

Int (x=0)

int (x, base=10), the base default value is 10, which means that when the value of base is not specified, the function will handle x in decimal.


For Python versions:

python2.x

python3.x


Attention:

1. x can be a number or a string, but base is assigned a value of x can only be a string

2. x as a string must be a base type, meaning that x becomes a number when it must be able to be represented by a base binary


Python English document Explanation:

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).


The integer type is described in Numeric types-int, float, complex.


Changed in version 3.4:if base isn't a instance of int and the base object has a base.__index__ method, which is Called to obtain a integer for the base. Previous versions used base.__int__ instead of base.__index__.


Changed in version 3.6:grouping digits and underscores as in code literals are allowed.


code example:


1. x is the case of a number:

Int (3.14)            # 3 int (2e2)             # int (2) #          error, Base is assigned after function only receives string


2. x is the case of a string:

Int (' + ', +)      # int (' Pythontab ', 8)      # Error, Pythontab is not a 8-digit binary


3. The base value range is 2~36, which contains all the English letters (not case sensitive), the hexadecimal is 15, then G will represent 16 in two decimal, and so on .... Z represents 35 in 36 binary

Int (' FZ ', +)      # error, FZ cannot represent int in hexadecimal (' FZ ', $)      # 575


4. String 0x can appear in hexadecimal, as a hexadecimal symbol, the same 0b can appear in the binary, in addition to the number 0 and the letter X

Int (' 0x10 ', +)  # 16,0x is the hexadecimal symbol int (' 0x10 ', ')  # error, ' 0x10 ' x is treated as an English letter x int (' 0x10 ',  *) # 42804,36 binary contains the letter X
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.