Introduction
Welcome to PyVerse! Today we'll learn how Python does math and makes decisions using operators and expressions. If you've ever solved 2 + 3 × 4 or asked "Is 10 bigger than 7?", you already know the idea. Let's turn those into code!
1) What is an expression?
- An expression is a piece of code that Python can evaluate to a value.
- It's made of values (like numbers or text), variables (like x or name), and operators (like +, -, and).
- Example: 2 + 3 is an expression. So is name + "!" or (5 > 2).
2) Arithmetic operators (for numbers)
- + add
- - subtract
- * multiply
- / divide (always gives a decimal, even if it looks whole)
- // floor division (whole number result)
- % remainder (what's left after division)
- ** exponent (power)
Try these:
print(7 + 3) # 10
print(7 - 3) # 4
print(7 * 3) # 21
print(7 / 3) # 2.3333333333333335
print(7 // 3) # 2
print(7 % 3) # 1
print(2 ** 5) # 323) Order of operations (who goes first?)
- Parentheses go first: ( )
- Then exponents: **
- Then multiply and divide: * / // %
- Then add and subtract: + -
- Use parentheses to make your meaning clear.
print(2 + 3 * 4) # 14 (multiply first)
print((2 + 3) * 4) # 20 (parentheses first)4) Variables and assignment
- Use = to store a value in a variable.
- You can update variables too.
- Shortcuts: +=, -=, *=, /= update and reassign in one step.
x = 10
x = x + 5
print(x) # 15
x += 5 # same as x = x + 5
print(x) # 20
x *= 2
print(x) # 405) Comparison operators (they give True or False)
- == equal to
- != not equal to
- > greater than
- < less than
- >= greater than or equal to
- <= less than or equal to
print(5 == 5) # True
print(5 != 3) # True
print(7 > 10) # False
print(8 <= 8) # True6) Logical operators (combine True/False)
- and: both sides must be True
- or: at least one side is True
- not: flips True to False and vice versa
age = 14
is_teen = age >= 13 and age <= 19
print(is_teen) # True
has_ticket = True
is_vip = False
can_enter = has_ticket or is_vip
print(can_enter) # True
is_raining = False
print(not is_raining) # True7) Strings and operators
- You can join strings with + (called concatenation).
- You can repeat strings with *.
print("Hello" + " " + "World") # Hello World
print("Ha" * 3) # HaHaHaBe careful: you can't add a number to a string directly. Convert it first.
name = "Sam"
score = 15
print(name + " scored " + str(score) + " points!") # Sam scored 15 points!8) Mini exercise: Quick practice (5–7 minutes)
Calculations
- Set width = 8 and height = 3. Make area using * and print it.
- What is the remainder when 29 is divided by 5? Use % and print it.
Order matters
- Print the results of 2 + 3 * 4 and (2 + 3) * 4.
Check conditions
- Let temp = 24. Make a variable comfy that is True if temp is between 20 and 30 (inclusive). Print comfy.
Strings
- Let name = "Ava" and stars = 4. Print "Ava earned 4 stars!" using + and str().
Sample answers (peek only after you try!)
width = 8
height = 3
area = width * height
print(area) # 24
print(29 % 5) # 4
print(2 + 3 * 4) # 14
print((2 + 3) * 4) # 20
temp = 24
comfy = (temp >= 20) and (temp <= 30)
print(comfy) # True
name = "Ava"
stars = 4
print(name + " earned " + str(stars) + " stars!")9) Summary
- Expressions combine values, variables, and operators to make new values.
- Arithmetic operators do math; comparison operators make True/False; logical operators combine True/False.
- Use parentheses to control the order of operations.
- Strings use + to join and * to repeat; convert types when needed with str() or int().
You're now ready to write smarter code that calculates and makes decisions!