Lesson 5 — Types
Data Types
Python's built-in types are rich and cover most needs. Understanding how they behave — especially mutability — prevents a whole class of subtle bugs.
Core Built-In Types
PYTHON — Core types
# Numbers:
age = 24 # int — arbitrary precision, no overflow
mark = 74.5 # float — double precision floating point
complex_n = 3 + 4j # complex — rarely used in general programming
# Text:
name = "Thandi" # str — immutable Unicode sequence
char = "A" # str of length 1 (no separate char type)
# Boolean:
active = True # bool — subclass of int! True == 1, False == 0
# Nothing:
result = None # NoneType — the only value of NoneType
# Check types:
print(type(age)) # <class 'int'>
print(type(mark)) # <class 'float'>
print(type(name)) # <class 'str'>
print(isinstance(age, int)) # True
print(isinstance(True, int)) # True — bool IS-A int in Python
Integer Precision
Python integers have arbitrary precision — they never overflow. You can compute 2 ** 1000 and get the exact answer. This is unlike Java's int which overflows at ~2.1 billion.
PYTHON — Arbitrary precision
# No overflow:
print(2 ** 100) # 1267650600228229401496703205376
print(factorial := 1) # walrus operator := assigns and returns
for i in range(1, 21):
factorial *= i
print(f"20! = {factorial}") # exact
String Operations
PYTHON — String operations
s = "Thandi Mokoena"
print(len(s)) # 15
print(s.upper()) # THANDI MOKOENA
print(s.lower()) # thandi mokoena
print(s.split(" ")) # ['Thandi', 'Mokoena']
print(s.replace("a","@")) # Th@ndi Moke@n@
print(s.startswith("T")) # True
print(s.strip()) # removes leading/trailing whitespace
print("Mokoena" in s) # True — membership test
# String slicing:
print(s[0]) # T
print(s[-1]) # a
print(s[0:6]) # Thandi
print(s[7:]) # Mokoena
print(s[::-1]) # anekoM idnahT — reversed
Type Conversion
PYTHON — Conversion
# Explicit conversions:
age_str = "22"
age_int = int(age_str) # "22" → 22
price_str = "250.50"
price = float(price_str) # "250.50" → 250.5
bool_val = bool(0) # 0, "", [], None → False; everything else → True
marks = [65, 72, 80]
marks_str = str(marks) # "[65, 72, 80]"
# Falsy values in Python (evaluate to False in boolean context):
# 0, 0.0, 0j, "", [], {}, set(), None, False
# Everything else is truthy
Practice Task
Your Turn
Create variables of each core type. Use isinstance() to verify each type. Convert a mark stored as a string to float. Write a function that accepts any value and returns a description of its type and whether it is truthy or falsy.
Common Mistakes
- Adding int and str raises TypeError — convert explicitly:
str(n)orint(s). int('74.5')raises ValueError — usefloat()first thenint()if you need truncation.- True and False are capitalised —
trueis a NameError. - None is not False —
None == Falseis False. Test withis None.
Professional Tip
Python's falsy values are a useful feature — if user_input: is cleaner than if user_input != "" and user_input is not None:. Learn them by heart.
Mini Quiz
What is the result of bool(0) in Python?
0, empty strings, empty lists, empty dicts, and None all evaluate to False in a boolean context. This is how Python's truthiness system works.