Python Tutorials - Herong's Tutorial Examples - v2.14, by Herong Yang
Data Type - 'int' for Integer Values
This section describes the 'int' data type, which has infinite number of objects covering all integer values.
What Is the "int" Data Type? "int" is the Integer data type in Python. It has infinite number of objects covering all integer values.
"int" data type has the following main Features.
1. "int" data objects can be created in several ways:
Here are some examples on how to create "int" objects:
>>> 255, -255, 0 (255, -255, 0) >>> 0xff, -0xff, 0x0 (255, -255, 0) >>> 0o377, -0o377, 0o0 (255, -255, 0) >>> 0b11111111, -0b11111111, 0b0 (255, -255, 0) >>> int(1.1), int('1') (1, 1)
2. "int" data type is an immutable data type. Once an "int" data object is created to store an integer value, this value will never change.
Don't get confused about "int" immutability and "int" variable re-assignment capability. An "int" variable can be re-assigned with different "int" objects many times.
# new object is created for 1 and assigned to x >>> x = 1 >>> id(x) 4547439248 # assign the same object to y >>> y = x >>> id(y) 4547439248 # new object is created for 2 and assigned to x >>> x = 2 >>> id(x) 4547439280 # the first object for 1 is still there in memory >>> y 1 >>> id(y) 4547439248
3. Because "int" data type is immutable, an object created for a given integer value is not allowed to change its value. This allow "int" objects to be cached in memory and reused later whenever they are needed again. Reusing objects will reduce execution time and memory consumption.
On my macOS computer, "int" objects for smaller integers are cached and reused, since they are used more frequently. For example, "int" object for integer 1 is cached and reused. No matter how you create it, you will get the same object.
>>> x = 1 >>> y = 1 >>> (id(x), id(y), id(1), id(int(1.1)), id(int('1'))) (4547439248, 4547439248, 4547439248, 4547439248, 4547439248)
However, "int" objects for large integers are not cached, since their likelihood of being reused is very low. Caching them is actually wasting memory.
>>> id(1234567801234567890), id(1234567801234567890) (4459214720, 4459214720) >>> id(1234567801234567890), id(1234567801234567890) (4459213472, 4459213472)
4. "int" objects support the following arithmetic operations:
Syntax Operation Resulting Type ------ --------- -------------- x + y Addition int x - y Subtraction int x * y Multiplication int x / y Division float x // y Floored Quotient int x % y Remainder int
Here are some examples of "int" arithmetic operations:
>>> 2 + 3 5 >>> 5 - 7 -2 >>> 2*5 10 >>> 11/3 3.6666666666666665 >>> 11//3 3 >>> 11%3 2
5. "int" objects support the following comparison operations.
Syntax Operation ------ --------- x < y Less than x <= y Less than or equal x > y Greater than x >= y Greater than or equal x == y Equal x != y Not equal
Here are some examples of "int" comparison operations:
>>> 5 < 6 True >>> 5 <= 6 True >>> 5 > 6 False >>> 5 >= 6 False >>> 5 == 6 False >>> 5 != 6 True
6. "int" objects support the following bitwise operations. They are performed by representing integers in the two’s complement format with length limitation.
Syntax Operation ------ --------- x | y Bitwise OR x ^ y Bitwise XOR x & y Bitwise AND x << n Left shift x >> n Right shift ~ x Bitwise NOT
Here are some examples of "int" arithmetic operations:
# '01010' | '00100' = '01110' >>> 10 | 4 14 # '01010' ^ '00100' = '01110' >>> 10 ^ 4 14 # '01010' & '00100' = '00000' >>> 10 & 4 0 # '01010' << 1 = '10100' >>> 10 << 1 20 # '01010' >> 1 = '00101' >>> 10 >> 1 5 # ~'01010' > '10101' >>> ~10 -11
7. Some built-in functions are provided for "int" objects.
8. Some class/instance methods are provided for "int" objects.
Table of Contents
Common Features of All Data Types
Data Type - NoneType for Nothing
Data Type - 'bool' for Boolean Values
►Data Type - 'int' for Integer Values
Data Type - 'float' for Real Numbers
Data Type - 'bytes' for Byte Sequence
Data Type - 'str' for Character String
Data Type - 'tuple' for Immutable List
Data Type - 'list' for Mutable List
Data Type - 'dict' for Dictionary Table
Variables, Operations and Expressions
Function Statement and Function Call
Iterators, Generators and List Comprehensions
Packages and Package Directories
"pathlib" - Object-Oriented Filesystem Paths
"pip" - Package Installer for Python
SciPy.org - Python Libraries for Science
pandas - Data Analysis and Manipulation
Anaconda - Python Environment Manager