Lesson 1 · Python data types deep dive
Python Integers Explained: Bases, Division & Modulo | Data Types #1
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…
- 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.
🧠 Test yourself
Pick an answer to check it. Your best score is saved in this browser.
What does this print?
print(-17 // 5, -17 % 5)
Floor division rounds toward negative infinity, so -17 // 5 is -4, and the remainder that keeps the equation true is 3.
What type does
10 / 2produce in Python 3?True division with / always returns a float, even when the result is whole. Use // when you want floor division.
What does this print?
print((256).bit_length(), (255).bit_count())
256 is 1 followed by eight zeros in binary, so it needs 9 bits. 255 is eight 1s, so bit_count() gives 8.
Why should you compare integer values with
==and notis?Python caches small integers, so
iscan look right by accident.==always compares values.What does this print?
print(pow(2, 10, 1000))
The third argument is a modulus: 2**10 is 1024, and 1024 % 1000 is 24.
💻 Practice exercises
Try each one in the notebook or in your own editor before opening the solution.
Given n = 1234567, print it with comma separators, then print it in binary, then in hexadecimal (all using f-string format specs).
💡 Hint
Use format specs like :, then :b and :x.
✅ Show solution
n = 1234567 print(f"{n:,}") # 1,234,567 print(f"{n:b}") # binary digits print(f"{n:x}") # hexadecimal digits
Write digit_sum(n) that returns the sum of the digits of a non-negative integer using divmod (no string conversion). digit_sum(4821) should be 15.
💡 Hint
Loop with divmod(n, 10) to peel off the last digit each time.
✅ Show solution
def digit_sum(n): total = 0 while n > 0: n, last = divmod(n, 10) # split off the last digit total += last return total print(digit_sum(4821)) assert digit_sum(4821) == 15 assert digit_sum(0) == 0
Write to_base(n, base) that converts a non-negative integer to a string in any base from 2 to 16 without using bin/hex/format. Check it against int(text, base).
💡 Hint
Repeatedly divmod by the base and collect digits from a lookup string, then reverse.
✅ Show solution
DIGITS = "0123456789abcdef" def to_base(n, base): if n == 0: return "0" out = [] while n > 0: n, r = divmod(n, base) # remainder is the next digit out.append(DIGITS[r]) return "".join(reversed(out)) # digits came out last-first for n in (0, 5, 255, 1024, 99999): for base in (2, 8, 16): text = to_base(n, base) assert int(text, base) == n print(to_base(255, 2), to_base(255, 16))
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



