How to Use the int () function in python, pythonint

Source: Internet
Author: User

How to Use the int () function in python, pythonint

int(x, [base])

Function:

A function is used to convert a numeric or base string to an integer.

Function prototype:

Int (x = 0)
Int (x, base = 10). The default value of base is 10. That is to say, if the base value is not specified, the function processes x in decimal format.

Applicable Python version:

Python2.x
Python3.x

Note:

1. x can be a number or string, but x can only be a string after base is assigned a value.
2. When x is used as a string, it must be of the base type, that is, it must be expressed in base notation when x is converted to a number.

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 are 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 be a string, bytes, or bytearray instance representing an integer literal 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, with a to z (or A to Z) having values 10 to 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 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 that int ('010 ', 0) is not legal, while int ('010 ') is, as well as int ('010', 8 ).
The integer type is described in Numeric Types-int, float, complex.
Changed in version 3.4: If base is not an instance of int and the base object has a base. _ index _ method, that method is called to obtain an integer for the base. previous versions used base. _ int _ instead of base. _ index __.
Changed in version 3.6: Grouping digits with underscores as in code literals is allowed.

Code example:

1. x is a number:

Int (3.14) # 3int (2e2) # 200int (100, 2) # error. After the base value is assigned, the function only receives strings.

2. x is a string:

Int ('23', 16) # 35int ('pythontab', 8) # error, Pythontab is not an octal number

3. The base value range is 2 ~ 36, including all English letters (case-insensitive). In hexadecimal notation, F indicates 15, then G indicates 16 in, and so on .... Z represents 35 in hexadecimal notation

Int ('fz', 16) # error, FZ cannot represent int ('fz', 36) in hexadecimal notation #575

4. The character string 0x can appear in the hexadecimal notation and is considered as a hexadecimal notation. Similarly, the 0b can appear in the binary notation. In addition, it can be regarded as a number 0 and a letter x.

Int ('0x10', 16) #16, 0x is the hexadecimal symbol int ('0x10', 17) # error, x in '0x10' is regarded as the English letter xint ('0x10', 36) #42804,36 contains the letter x.

Summary

The above is a small Editor to introduce the int () function in python, I hope to help you, if you have any questions, please leave a message, the small editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.