Python built-in int functions

Source: Internet
Author: User
This document introduces the Python built-in int function in detail:

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

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 "
 
  ", line 1, in 
  
       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 "
 
  ", line 1, in 
  
       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

The above is a detailed description of the Python built-in int function. For more information, see other related articles in the first PHP community!

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.