Detailed explanation of Python integer type (int)

352d5699ebab4f9389de3cd653fab228.png

 

An integer is a number without a decimal part.  Integers in Python include positive integers, 0 and negative integers.

Some strongly typed programming languages ​​provide multiple integer types. Each type has a different length and can accommodate different sizes of integers. Developers should choose different types based on the actual number size. For example, C language provides four types of integers: short, int, long, and long long. Their lengths increase in sequence. Beginners are often confused when choosing integer types, which sometimes lead to numerical overflows.

Python is different. Its integers are not divided into types, or it has only one type of integers. The value range of Python integers is unlimited. No matter how big or small the number is, Python can handle it easily.

When the used values ​​exceed the computer's own computing capabilities, Python will automatically switch to high-precision calculations (large number calculations).

Please look at the code below:

 
  1. #Assign 78 to variable n
  2. n = 78
  3. print(n)
  4. print( type(n) )
  5.  
  6. #Assign a large integer to x
  7. x = 8888888888888888888888
  8. print(x)
  9. print( type(x) )
  10.  
  11. #Assign a small integer to y
  12. y = -7777777777777777777777
  13. print(y)
  14. print( type(y) )

operation result:

78
<class 'int'>
8888888888888888888888
<class 'int'>
-7777777777777777777777
<class 'int'>

Even if x is a very large number and y is a very small number, Python can output it correctly without overflow, which shows that Python has a very powerful ability to process integers.

No matter how large or small the integer is, Python only uses one type to store it, which is int.

About Python 2.x

Python 3.x only uses int type to store integers, but Python 2.x will use long type to store larger integers. The result of running the above code under Python 2.x is:

78
<type 'int'>
8888888888888888888888
<type 'long'>
-7777777777777777777777
<type 'long'>

But no matter which version of Python, it can easily handle very large and very small numbers, and programmers don't have to worry about whether the underlying type is int or long.

Different bases for integers

In Python, you can use multiple bases to represent integers:

1) Decimal form

The common integers we usually see are in decimal form, which is composed of ten numbers from 0 to 9.

Note that integers using decimal form cannot start with 0, unless the value itself is 0.

2) Binary form

It consists of two numbers, 0 and 1, and is written starting with 0bor 0B. For example, 101 corresponds to the decimal number 5.

3) Octal format

Octal integers consist of eight digits from 0 to 7, starting with 0oor 0O. Note that the first symbol is the number 0 and the second symbol is the uppercase or lowercase letter O.

In Python 2.x, octal numbers can also 0start directly with (digit zero).

4) Hexadecimal format

It consists of ten numbers from 0 to 9 and six letters from A to F (or a~f). When written, it starts with 0xor 0X.

If you don’t understand different base systems and the conversion methods between them, please click on the link below :


[Example] Use of different base integers in Python:

 
  1. #hexadecimal
  2. hex1 = 0x45
  3. hex2 = 0x4Af
  4. print("hex1Value: ", hex1)
  5. print("hex2Value: ", hex2)
  6.  
  7. #binary
  8. bin1 = 0b101
  9. print('bin1Value: ', bin1)
  10. bin2 = 0B110
  11. print('bin2Value: ', bin2)
  12.  
  13. #Octal
  14. oct1 = 0o26
  15. print('oct1Value: ', oct1)
  16. oct2 = 0O41
  17. print('oct2Value: ', oct2)

operation result:

hex1Value:  69
hex2Value:  1199
bin1Value:  5
bin2Value:  6
oct1Value:  22
oct2Value:  33

The output results in this example are all decimal integers.

Number separator

To improve the readability of numbers, Python 3.x allows the use _of underscores as separators for numbers (including integers and decimals). Usually an underscore is added to every third digit, similar to the comma in English numbers. Underscores do not affect the value of the number itself.

[Example] Use underline to write numbers:

 
  1. click = 1_301_547
  2. distance = 384_000_000
  3. print("Python tutorial reading volume:", click)
  4. print("The distance between the earth and the moon:", distance)

operation result:

Python tutorial reading count: 1301547
Distance between the Earth and the Moon: 384000000

Guess you like

Origin blog.csdn.net/weixin_74774974/article/details/133420739