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 = 25
height = 1.75
initial = 'J'
is_student = True
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age = 17
height = 1.75
initial = 'J'
is_student = True
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age = 42
height = 1.75
initial = 'J'
is_student = True
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age = 25
height = 1.60
initial = 'J'
is_student = True
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age = 25
height = 1.85
initial = 'J'
is_student = True
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age = 25
height = 1.75
initial = 'M'
is_student = True
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age = 25
height = 1.75
initial = 'S'
is_student = True
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age = 25
height = 1.75
initial = 'J'
is_student = False
print(f"Age: {age}")
print(f"Height: {height}m")
print(f"Initial: {initial}")
print(f"Student: {is_student}")
age ← 25, height ← 1.75, initial ← J, is_student ← True
1age→ 25 = 25 #@age=17, 422height→ 1.75 = 1.75 #@height=1.60, 1.853initial→ J = 'J' #@initial='M', 'S'4is_student→ True = True #@is_student=False56print(f"Age: {age25}")7print(f"Height: {height1.75}m")8print(f"Initial: {initialJ}")9print(f"Student: {is_studentTrue}")outputAge: 25 Height: 1.75m Initial: J Student: True
age ← 17, height ← 1.75, initial ← J, is_student ← True
1age→ 17 = 172height→ 1.75 = 1.753initial→ J = 'J'4is_student→ True = True56print(f"Age: {age17}")7print(f"Height: {height1.75}m")8print(f"Initial: {initialJ}")9print(f"Student: {is_studentTrue}")outputAge: 17 Height: 1.75m Initial: J Student: True
age ← 42, height ← 1.75, initial ← J, is_student ← True
1age→ 42 = 422height→ 1.75 = 1.753initial→ J = 'J'4is_student→ True = True56print(f"Age: {age42}")7print(f"Height: {height1.75}m")8print(f"Initial: {initialJ}")9print(f"Student: {is_studentTrue}")outputAge: 42 Height: 1.75m Initial: J Student: True
age ← 25, height ← 1.6, initial ← J, is_student ← True
1age→ 25 = 252height→ 1.6 = 1.603initial→ J = 'J'4is_student→ True = True56print(f"Age: {age25}")7print(f"Height: {height1.6}m")8print(f"Initial: {initialJ}")9print(f"Student: {is_studentTrue}")outputAge: 25 Height: 1.6m Initial: J Student: True
age ← 25, height ← 1.85, initial ← J, is_student ← True
1age→ 25 = 252height→ 1.85 = 1.853initial→ J = 'J'4is_student→ True = True56print(f"Age: {age25}")7print(f"Height: {height1.85}m")8print(f"Initial: {initialJ}")9print(f"Student: {is_studentTrue}")outputAge: 25 Height: 1.85m Initial: J Student: True
age ← 25, height ← 1.75, initial ← M, is_student ← True
1age→ 25 = 252height→ 1.75 = 1.753initial→ M = 'M'4is_student→ True = True56print(f"Age: {age25}")7print(f"Height: {height1.75}m")8print(f"Initial: {initialM}")9print(f"Student: {is_studentTrue}")outputAge: 25 Height: 1.75m Initial: M Student: True
age ← 25, height ← 1.75, initial ← S, is_student ← True
1age→ 25 = 252height→ 1.75 = 1.753initial→ S = 'S'4is_student→ True = True56print(f"Age: {age25}")7print(f"Height: {height1.75}m")8print(f"Initial: {initialS}")9print(f"Student: {is_studentTrue}")outputAge: 25 Height: 1.75m Initial: S Student: True
age ← 25, height ← 1.75, initial ← J, is_student ← False
1age→ 25 = 252height→ 1.75 = 1.753initial→ J = 'J'4is_student→ False = False56print(f"Age: {age25}")7print(f"Height: {height1.75}m")8print(f"Initial: {initialJ}")9print(f"Student: {is_studentFalse}")outputAge: 25 Height: 1.75m Initial: J Student: False
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")
big_number ← 2000000000, doubled ← 4000000000, huge ← 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
1big_number→ 2000000000 = 20000000002doubled→ 4000000000 = big_number2000000000 * 23print(f"Original: {big_number2000000000}")4print(f"Doubled: {doubled4000000000}")56# Python handles huge numbers!7huge→ 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 = 10 ** 100 #?huge_number8print(f"10^100 has {len(str(huge10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000))} digits")outputOriginal: 2000000000 Doubled: 4000000000 10^100 has 101 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')}")
a ← 0.1, b ← 0.2, total ← 0.30000000000000004, cents1 ← 10, cents2 ← 20
1a→ 0.1 = 0.12b→ 0.2 = 0.23total→ 0.30000000000000004 = a0.1 + b0.245print(f"0.1 + 0.2 = {total0.30000000000000004}")6print(f"Equals 0.3? {total0.30000000000000004 == 0.3}") #?precision_issue78# For money, use integer cents instead9cents1→ 10 = 1010cents2→ 20 = 2011total_cents→ 30 = cents110 + cents22012print(f"10 + 20 cents = {total_cents30}")1314# Or use the Decimal module for exact decimals15from decimal import Decimal16d1→ 0.1 = Decimal('0.1')17d2→ 0.2 = Decimal('0.2')18print(f"Decimal: {d10.1 + d20.2} == 0.3? {d1 + d2 == Decimal('0.3')}")output0.1 + 0.2 = 0.30000000000000004 Equals 0.3? False 10 + 20 cents = 30 Decimal: 0.3 == 0.3? True
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 = True
has_license = False
age = 20
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 = False
has_license = False
age = 20
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 = True
has_license = True
age = 20
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 = True
has_license = False
age = 16
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 = True
has_license = False
age = 25
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 ← True, has_license ← False, age ← 20, is_adult ← True
1is_student→ True = True #@is_student=False2has_license→ False = False #@has_license=True3age→ 20 = 20 #@age=16, 2545is_adult→ True = age20 >= 186can_drive→ False = is_adultTrue and has_licenseFalse7gets_discount→ True = is_studentTrue or age20 < 1889print(f"Adult: {is_adultTrue}")10print(f"Can drive: {can_driveFalse}")11print(f"Gets discount: {gets_discountTrue}")1213# Python booleans are also integers!14print(f"True + True = {True + True}")15print(f"False * 10 = {False * 10}")outputAdult: True Can drive: False Gets discount: True True + True = 2 False * 10 = 0
is_student ← False, has_license ← False, age ← 20, is_adult ← True
1is_student→ False = False2has_license→ False = False3age→ 20 = 2045is_adult→ True = age20 >= 186can_drive→ False = is_adultTrue and has_licenseFalse7gets_discount→ False = is_studentFalse or age20 < 1889print(f"Adult: {is_adultTrue}")10print(f"Can drive: {can_driveFalse}")11print(f"Gets discount: {gets_discountFalse}")1213# Python booleans are also integers!14print(f"True + True = {True + True}")15print(f"False * 10 = {False * 10}")outputAdult: True Can drive: False Gets discount: False True + True = 2 False * 10 = 0
is_student ← True, has_license ← True, age ← 20, is_adult ← True
1is_student→ True = True2has_license→ True = True3age→ 20 = 2045is_adult→ True = age20 >= 186can_drive→ True = is_adultTrue and has_licenseTrue7gets_discount→ True = is_studentTrue or age20 < 1889print(f"Adult: {is_adultTrue}")10print(f"Can drive: {can_driveTrue}")11print(f"Gets discount: {gets_discountTrue}")1213# Python booleans are also integers!14print(f"True + True = {True + True}")15print(f"False * 10 = {False * 10}")outputAdult: True Can drive: True Gets discount: True True + True = 2 False * 10 = 0
is_student ← True, has_license ← False, age ← 16, is_adult ← False
1is_student→ True = True2has_license→ False = False3age→ 16 = 1645is_adult→ False = age16 >= 186can_drive→ False = is_adultFalse and has_licenseFalse7gets_discount→ True = is_studentTrue or age16 < 1889print(f"Adult: {is_adultFalse}")10print(f"Can drive: {can_driveFalse}")11print(f"Gets discount: {gets_discountTrue}")1213# Python booleans are also integers!14print(f"True + True = {True + True}")15print(f"False * 10 = {False * 10}")outputAdult: False Can drive: False Gets discount: True True + True = 2 False * 10 = 0
is_student ← True, has_license ← False, age ← 25, is_adult ← True
1is_student→ True = True2has_license→ False = False3age→ 25 = 2545is_adult→ True = age25 >= 186can_drive→ False = is_adultTrue and has_licenseFalse7gets_discount→ True = is_studentTrue or age25 < 1889print(f"Adult: {is_adultTrue}")10print(f"Can drive: {can_driveFalse}")11print(f"Gets discount: {gets_discountTrue}")1213# Python booleans are also integers!14print(f"True + True = {True + True}")15print(f"False * 10 = {False * 10}")outputAdult: True Can drive: False Gets discount: True True + True = 2 False * 10 = 0
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
age = 16
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
age = 40
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
age ← 25, height ← 1.75, name ← Alice, active ← True
1age→ 25 = 25 #@age=16, 402height→ 1.75 = 1.753name→ Alice = "Alice"4active→ True = True56# Check types7print(f"age is {type(age25)}")8print(f"height is {type(height1.75)}")9print(f"name is {type(nameAlice)}")10print(f"active is {type(activeTrue)}")1112# Type conversions13print(f"\nint(3.7) = {int(3.7)}") # truncates14print(f"float(5) = {float(5)}") # adds .015print(f"str(42) = '{str(42)}'") # to string16print(f"bool(0) = {bool(0)}") # 0 is False17print(f"bool(1) = {bool(1)}") # non-zero is True18print(f"bool('') = {bool('')}") # empty string is False19print(f"bool('hi') = {bool('hi')}") # non-empty is Trueoutputage is <class 'int'> height is <class 'float'> name is <class 'str'> active is <class 'bool'> int(3.7) = 3 float(5) = 5.0 str(42) = '42' bool(0) = False bool(1) = True bool('') = False bool('hi') = True
age ← 16, height ← 1.75, name ← Alice, active ← True
1age→ 16 = 162height→ 1.75 = 1.753name→ Alice = "Alice"4active→ True = True56# Check types7print(f"age is {type(age16)}")8print(f"height is {type(height1.75)}")9print(f"name is {type(nameAlice)}")10print(f"active is {type(activeTrue)}")1112# Type conversions13print(f"\nint(3.7) = {int(3.7)}") # truncates14print(f"float(5) = {float(5)}") # adds .015print(f"str(42) = '{str(42)}'") # to string16print(f"bool(0) = {bool(0)}") # 0 is False17print(f"bool(1) = {bool(1)}") # non-zero is True18print(f"bool('') = {bool('')}") # empty string is False19print(f"bool('hi') = {bool('hi')}") # non-empty is Trueoutputage is <class 'int'> height is <class 'float'> name is <class 'str'> active is <class 'bool'> int(3.7) = 3 float(5) = 5.0 str(42) = '42' bool(0) = False bool(1) = True bool('') = False bool('hi') = True
age ← 40, height ← 1.75, name ← Alice, active ← True
1age→ 40 = 402height→ 1.75 = 1.753name→ Alice = "Alice"4active→ True = True56# Check types7print(f"age is {type(age40)}")8print(f"height is {type(height1.75)}")9print(f"name is {type(nameAlice)}")10print(f"active is {type(activeTrue)}")1112# Type conversions13print(f"\nint(3.7) = {int(3.7)}") # truncates14print(f"float(5) = {float(5)}") # adds .015print(f"str(42) = '{str(42)}'") # to string16print(f"bool(0) = {bool(0)}") # 0 is False17print(f"bool(1) = {bool(1)}") # non-zero is True18print(f"bool('') = {bool('')}") # empty string is False19print(f"bool('hi') = {bool('hi')}") # non-empty is Trueoutputage is <class 'int'> height is <class 'float'> name is <class 'str'> active is <class 'bool'> int(3.7) = 3 float(5) = 5.0 str(42) = '42' bool(0) = False bool(1) = True bool('') = False bool('hi') = True
Use type() to inspect, int(), float(), str(), bool() to convert.
Exercise: numeric_ops.py
Explore useful numeric operations: abs, round, divmod, power