Lesson 1 · Python data types deep dive
Python Integers Explained: Bases, Division & Modulo | Data Types #1
Video one of a twelve-part series covering every built-in Python data type in full depth, one type per video. This video is entirely about int: every…
- CoursePython data types deep dive
- Lesson1 of 12
- Video30 min
- FormatJupyter notebook · 25 code cells
What you'll learn
Data
No separate download needed — the notebook creates or downloads everything it uses.
📓 Full notebook
Download .ipynbPython Data Types Deep-Dive, Video 1: Integers (int)#
- Video one of a twelve-part series covering every built-in Python data type in full depth, one type per video.
- This video is entirely about int: every literal form, every operator, and all ten of its public methods, one at a time.
- No prior experience with data types needed. Let's get straight into it.
Part 1: What Makes int Different#
small_number = 7
negative_number = -42
zero = 0
huge_number = 99999999999999999999999999999999999999
print(type(small_number))
print(huge_number * huge_number)
Part 2: Integer Literals#
readable_million = 1_000_000
binary_literal = 0b1010
octal_literal = 0o17
hex_literal = 0xFF
print(readable_million)
print(binary_literal)
print(octal_literal)
print(hex_literal)
Part 3: Arithmetic Operators#
a, b = 17, 5
print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a // b)
print(a % b)
print(a ** b)
print(-17 // 5)
print(-17 % 5)
print(+a)
print(-a)
Part 4: Comparison Operators and Chaining#
print(3 < 5)
print(5 <= 5)
print(5 == 5.0)
print(5 != 6)
print(1 < 5 < 10)
print(1 < 10 < 5)
Part 5: Bitwise Operators#
x, y = 0b1100, 0b1010
print(bin(x & y))
print(bin(x | y))
print(bin(x ^ y))
print(bin(~x))
print(bin(x << 2))
print(bin(x >> 2))
Part 6: Converting Values to int#
print(int('42'))
print(int(' 42 '))
print(int(3.9))
print(int(-3.9))
print(int(True))
print(int(False))
print(int('101', 2))
print(int('ff', 16))
print(int('17', 8))
try:
int('not a number')
except ValueError as e:
print(f'Caught: {e}')
Part 7: Built-in Functions That Work With int#
print(abs(-15))
print(divmod(17, 5))
print(pow(2, 10))
print(pow(2, 10, 1000))
print(round(1234, -2))
print(min(4, 9, 1, 7))
print(max(4, 9, 1, 7))
print(sum([1, 2, 3, 4, 5]))
Part 8: bit_length()#
print((0).bit_length())
print((1).bit_length())
print((255).bit_length())
print((256).bit_length())
print((-255).bit_length())
Part 9: bit_count()#
print((0).bit_count())
print((7).bit_count())
print((8).bit_count())
print((255).bit_count())
print(bin(255))
Part 10: to_bytes()#
value = 1024
print(value.to_bytes(2, byteorder='big'))
print(value.to_bytes(2, byteorder='little'))
print(value.to_bytes(4, byteorder='big'))
negative_value = -5
print(negative_value.to_bytes(1, byteorder='big', signed=True))
try:
(300).to_bytes(1, byteorder='big')
except OverflowError as e:
print(f'Caught: {e}')
Part 11: from_bytes()#
encoded = (1024).to_bytes(2, byteorder='big')
print(int.from_bytes(encoded, byteorder='big'))
print(int.from_bytes(encoded, byteorder='little'))
signed_encoded = (-5).to_bytes(1, byteorder='big', signed=True)
print(int.from_bytes(signed_encoded, byteorder='big', signed=True))
print(int.from_bytes(signed_encoded, byteorder='big', signed=False))
Part 12: as_integer_ratio()#
print((5).as_integer_ratio())
print((-5).as_integer_ratio())
print((0).as_integer_ratio())
Part 13: numerator and denominator#
value = 42
print(value.numerator)
print(value.denominator)
from fractions import Fraction
print(Fraction(value.numerator, value.denominator))
Part 14: real and imag#
value = 42
print(value.real)
print(value.imag)
print(type(value.real))
Part 15: conjugate()#
print((42).conjugate())
print((-7).conjugate())
print((42).conjugate() == 42)
Part 16: Formatting Integers#
value = 1234567
print(f'{value:,}')
print(f'{value:_}')
print(f'{value:010d}')
print(f'{value:+d}')
print(f'{value:x}')
print(f'{value:#x}')
print(f'{value:o}')
print(f'{value:b}')
print(format(value, ','))
Part 17: Arbitrary Precision, No Overflow#
import math
factorial_100 = math.factorial(100)
print(factorial_100)
print(len(str(factorial_100)))
print(2 ** 200)
Part 18: Small Integer Caching and Identity#
a = 100
b = 100
print(a is b)
c = 1000
d = 1000
print(c is d)
print(c == d)
Part 19: Immutability#
value = 5
print(id(value))
value = value + 1
print(id(value))
value += 1
print(id(value))
Part 20: Common Patterns and Pitfalls#
def is_even(n):
return n % 2 == 0
print(is_even(4))
print(is_even(7))
print(is_even(-4))
try:
result = 10 // 0
except ZeroDivisionError as e:
print(f'Caught: {e}')
print(type(10 / 2))
print(type(10 // 2))
average = (3 + 4) / 2
print(average)
Wrap-Up: What You Learned#
- Integer literals: decimal, underscores, binary, octal, and hexadecimal.
- Every arithmetic, comparison, and bitwise operator, including floor division and modulo with negative numbers.
- Converting to int with int(), including parsing strings in any base.
- Built-in functions that operate on integers: abs, divmod, pow, round, min, max, sum.
- All ten public int methods: bit_length, bit_count, to_bytes, from_bytes, as_integer_ratio, numerator, denominator, real, imag, and conjugate.
- Formatting integers with f-strings and format().
- Arbitrary precision, small-integer caching, immutability, and the true-division-versus-floor-division distinction.
- Next up in this series: float, the second numeric type, and where its extra methods and rounding behavior genuinely diverge from int.
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



