Data Types
Primitive Types
The Building Blocks
You're building a user profile: age (whole number), height (decimal), first initial (single letter), premium member (yes/no). Each piece of data needs the right container - that's what types are for.
Personal profile data
Store different types of information about a person.
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age =
height =
initial =
is_student =
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
Python figures out the type from the value. No need to declare types explicitly.
Big integers - no overflow!
Python handles arbitrarily large integers automatically.
big_number = 2000000000
doubled = big_number * 2
print(f"Original: {big_number}")
print(f"Doubled: {doubled}")
# Python handles huge numbers!
huge = 10 ** 100
print(f"10^100 has {len(str(huge))} digits")
Unlike many languages, Python integers grow as needed - no overflow!
Floating point precision
Decimal numbers aren't always exact. This can surprise you.
a = 0.1
b = 0.2
total = a + b
print(f"0.1 + 0.2 = {total}")
print(f"Equals 0.3? {total == 0.3}")
# For money, use integer cents instead
cents1 = 10
cents2 = 20
total_cents = cents1 + cents2
print(f"10 + 20 cents = {total_cents}")
# Or use the Decimal module for exact decimals
from decimal import Decimal
d1 = Decimal('0.1')
d2 = Decimal('0.2')
print(f"Decimal: {d1 + d2} == 0.3? {d1 + d2 == Decimal('0.3')}")
0.1 + 0.2 isn't exactly 0.3 - floating point has tiny rounding errors.
Boolean flags
Booleans represent states and conditions - very useful for tracking status.
is_student =
has_license =
age =
is_adult = age >= 18
can_drive = is_adult and has_license
gets_discount = is_student or age < 18
print(f"Adult: {is_adult}")
print(f"Can drive: {can_drive}")
print(f"Gets discount: {gets_discount}")
# Python booleans are also integers!
print(f"True + True = {True + True}")
print(f"False * 10 = {False * 10}")
is_student =
has_license =
age =
is_adult = age >= 18
can_drive = is_adult and has_license
gets_discount = is_student or age < 18
print(f"Adult: {is_adult}")
print(f"Can drive: {can_drive}")
print(f"Gets discount: {gets_discount}")
# Python booleans are also integers!
print(f"True + True = {True + True}")
print(f"False * 10 = {False * 10}")
is_student =
has_license =
age =
is_adult = age >= 18
can_drive = is_adult and has_license
gets_discount = is_student or age < 18
print(f"Adult: {is_adult}")
print(f"Can drive: {can_drive}")
print(f"Gets discount: {gets_discount}")
# Python booleans are also integers!
print(f"True + True = {True + True}")
print(f"False * 10 = {False * 10}")
is_student =
has_license =
age =
is_adult = age >= 18
can_drive = is_adult and has_license
gets_discount = is_student or age < 18
print(f"Adult: {is_adult}")
print(f"Can drive: {can_drive}")
print(f"Gets discount: {gets_discount}")
# Python booleans are also integers!
print(f"True + True = {True + True}")
print(f"False * 10 = {False * 10}")
is_student =
has_license =
age =
is_adult = age >= 18
can_drive = is_adult and has_license
gets_discount = is_student or age < 18
print(f"Adult: {is_adult}")
print(f"Can drive: {can_drive}")
print(f"Gets discount: {gets_discount}")
# Python booleans are also integers!
print(f"True + True = {True + True}")
print(f"False * 10 = {False * 10}")
is_student =
has_license =
age =
is_adult = age >= 18
can_drive = is_adult and has_license
gets_discount = is_student or age < 18
print(f"Adult: {is_adult}")
print(f"Can drive: {can_drive}")
print(f"Gets discount: {gets_discount}")
# Python booleans are also integers!
print(f"True + True = {True + True}")
print(f"False * 10 = {False * 10}")
is_student =
has_license =
age =
is_adult = age >= 18
can_drive = is_adult and has_license
gets_discount = is_student or age < 18
print(f"Adult: {is_adult}")
print(f"Can drive: {can_drive}")
print(f"Gets discount: {gets_discount}")
# Python booleans are also integers!
print(f"True + True = {True + True}")
print(f"False * 10 = {False * 10}")
is_student =
has_license =
age =
is_adult = age >= 18
can_drive = is_adult and has_license
gets_discount = is_student or age < 18
print(f"Adult: {is_adult}")
print(f"Can drive: {can_drive}")
print(f"Gets discount: {gets_discount}")
# Python booleans are also integers!
print(f"True + True = {True + True}")
print(f"False * 10 = {False * 10}")
is_student =
has_license =
age =
is_adult = age >= 18
can_drive = is_adult and has_license
gets_discount = is_student or age < 18
print(f"Adult: {is_adult}")
print(f"Can drive: {can_drive}")
print(f"Gets discount: {gets_discount}")
# Python booleans are also integers!
print(f"True + True = {True + True}")
print(f"False * 10 = {False * 10}")
is_student =
has_license =
age =
is_adult = age >= 18
can_drive = is_adult and has_license
gets_discount = is_student or age < 18
print(f"Adult: {is_adult}")
print(f"Can drive: {can_drive}")
print(f"Gets discount: {gets_discount}")
# Python booleans are also integers!
print(f"True + True = {True + True}")
print(f"False * 10 = {False * 10}")
is_student =
has_license =
age =
is_adult = age >= 18
can_drive = is_adult and has_license
gets_discount = is_student or age < 18
print(f"Adult: {is_adult}")
print(f"Can drive: {can_drive}")
print(f"Gets discount: {gets_discount}")
# Python booleans are also integers!
print(f"True + True = {True + True}")
print(f"False * 10 = {False * 10}")
is_student =
has_license =
age =
is_adult = age >= 18
can_drive = is_adult and has_license
gets_discount = is_student or age < 18
print(f"Adult: {is_adult}")
print(f"Can drive: {can_drive}")
print(f"Gets discount: {gets_discount}")
# Python booleans are also integers!
print(f"True + True = {True + True}")
print(f"False * 10 = {False * 10}")
Python uses True and False (capitalized). Use descriptive names like is_student.
Type checking and conversion
Check what type a value has and convert between types.
age = 25
height = 1.75
name = "Alice"
active = True
# Check types
print(f"age is {type(age)}")
print(f"height is {type(height)}")
print(f"name is {type(name)}")
print(f"active is {type(active)}")
# Type conversions
print(f"\nint(3.7) = {int(3.7)}") # truncates
print(f"float(5) = {float(5)}") # adds .0
print(f"str(42) = '{str(42)}'") # to string
print(f"bool(0) = {bool(0)}") # 0 is False
print(f"bool(1) = {bool(1)}") # non-zero is True
print(f"bool('') = {bool('')}") # empty string is False
print(f"bool('hi') = {bool('hi')}") # non-empty is True
Use type() to inspect, int(), float(), str(), bool() to convert.
Exercise: numeric_ops.py
Explore useful numeric operations: abs, round, divmod, power