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:

• Using decimal literals like 255, -255, 0, etc.
• Using hexadecimal literals like 0xff, -0xff, 0x0, etc.
• Using octal literals like 0o377, -0o377, 0o0, etc.
• Using octal literals like 0b11111111, -0b11111111, 0b0, etc.
• Using the cast function, int(object), like int(1.1), int('1'), etc.

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

• abs(x) - Returns an "int" object for the absolute value of x.
• divmod(x, y) - Returns a tuple of (x//y, x%y).
• pow(x, y) - Returns an "int" object for x to the power y. This can also be written as x**y using the ** operator.

8. Some class/instance methods are provided for "int" objects.

• int.bit_length() - Instance method returns the number of bits necessary to represent this integer in binary, excluding the sign and leading zeros.
• int.to_bytes(length, byteorder, *, signed=False) - Instance method returns a "bytes" object representing this integer.
• int.from_bytes(bytes, byteorder, *, signed=False) - Class method returns an "int" object represented by the given "bytes" object.