Variables, data types, and operators
FREE PREVIEW
Every program stores and transforms data. Variables are how you name that data. Types describe what kind of data it is.
Creating Variables
In Python you do not declare a type — you just assign and Python figures it out.
# A variable is created the moment you assign to it
name = "Sipho"
age = 28
salary = 45000.50
is_employed = True
notes = None
print(name) # Sipho
print(age) # 28
print(salary) # 45000.5
print(is_employed) # True
print(notes) # None
The Five Core Types
str — Text (a string of characters)
city = "Cape Town"
greeting = 'Sawubona'
multiline = """This spans
multiple lines"""
print(type(city)) # <class 'str'>
print(len(city)) # 9
int — Whole numbers (no decimal point)
students = 32
year = 2025
negative = -5
print(type(students)) # <class 'int'>
print(students * 2) # 64
float — Numbers with a decimal point
price = 299.99
vat_rate = 0.15
pi = 3.14159
print(type(price)) # <class 'float'>
print(price * vat_rate) # 44.9985
bool — True or False (capital T and F, always)
is_active = True
has_discount = False
print(type(is_active)) # <class 'bool'>
print(is_active) # True
print(not is_active) # False
# Booleans are actually integers underneath:
print(True + True) # 2
print(True * 100) # 100
NoneType — The absence of a value
result = None
print(type(result)) # <class 'NoneType'>
print(result is None) # True
# None is not the same as 0, False, or an empty string:
print(None == 0) # False
print(None == False) # False
Type Conversion
Convert between types explicitly when you need to:
# String to integer — works if the string contains a number
age_text = "28"
age = int(age_text)
print(age + 2) # 30
# String to float
price_text = "299.99"
price = float(price_text)
print(price * 1.15) # 344.9885
# Number to string (needed for concatenation)
count = 42
label = "Total: " + str(count)
print(label) # Total: 42
# What happens when conversion fails:
bad = int("hello") # ValueError: invalid literal for int()
Always wrap conversions in try/except when the input comes from a user or a file.
Arithmetic Operators
a = 17
b = 5
print(a + b) # 22 — addition
print(a - b) # 12 — subtraction
print(a * b) # 85 — multiplication
print(a / b) # 3.4 — division (ALWAYS returns a float)
print(a // b) # 3 — floor division (drops the decimal)
print(a % b) # 2 — modulo (remainder after division)
print(a ** b) # 1419857 — exponentiation (17 to the power of 5)
Practical example — calculating VAT:
price = 1000
vat = price * 0.15
total = price + vat
print(f"Price: R{price}, VAT: R{vat}, Total: R{total}")
# Price: R1000, VAT: R150.0, Total: R1150.0
Comparison Operators — Always Return a Bool
score = 74
print(score == 74) # True — equal to
print(score != 100) # True — not equal to
print(score > 80) # False — greater than
print(score < 80) # True — less than
print(score >= 74) # True — greater than or equal
print(score <= 50) # False — less than or equal
Logical Operators
age = 25
has_id = True
# and — BOTH must be True
print(age >= 18 and has_id) # True
print(age >= 18 and not has_id) # False
# or — AT LEAST ONE must be True
print(age < 18 or has_id) # True
print(age < 18 or not has_id) # False
# not — inverts the boolean
print(not True) # False
print(not False) # True
Duration: 20 minutes