1/14
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
Comparison Operators
Operators that compare two values and return a Boolean (True or False):
Equal to (==)
Not equal to (!=)
Greater than (>), Less than (<)
Greater than or equal (>=), Less than or equal (<=)
Ex.
x = 5
print(x == 5) # True
print(x != 5) # False
Logical Operators (and, or, not)
Used to combine or negate Boolean expressions:
and: Returns True if both statements are true.
or: Returns True if at least one statement is true.
not: Inverts the Boolean truth value.
Ex.
age = 20
has_id = True
can_enter = (age >= 18) and has_id # True is_blocked = not has_id # FalseOperator Precedence in Logic
The order in which logical evaluations are performed when multiple operators exist in one expression.
Ex.
1. not
2. and
3. or
Conditional Statement
A programming structure that controls program flow based on whether a condition evaluates to True or False.
Ex.
if temperature <= 80
print("It's a hot day!")Head-Body Structure
Python conditional statements use a head (containing the condition/keyword) and a body (containing the code to execute).
Ex.
if score >= 50:
print("You Passed!")
print("Congratulations!")
print("Game Over")if statement
Evaluates a single Boolean expression or variable. Executes the code inside its body only if the condition is True. If False, the body is skipped entirely.
Ex.
day = 2 # Tuesday (0=Mon, 1=Tue, 2=Wed, etc.)
if 0 <= day <= 4:
print("Weekday") # Executes because condition is Trueelse statement
Attached to an if statement to define a backup code block that executes only when the preceding if condition evaluates to False .
Ex.
day = 5 # Saturday
if 0 <= day <= 4:
print("Weekday")
else:
print("Weekend") # Executes because dow is 5 (False condition)elif Statement
Short for "else if". Acts as a bridge between an initial if and a final else to check multiple conditions sequentially.
Ex.
num = 15
if num % 2 == 0:
print("Divisible by 2")
elif num % 3 == 0:
print("Divisible by 3") # Executes, and the rest of the chain is skipped
elif num % 5 == 0:
print("Divisible by 5")Conditional Order
In an if/elif/else chain, order matters because the first True condition catches execution. Programmer best practice requires placing the most specific conditions first.
Ex.
num = 20
# INCORRECT ORDER: num % 2 catches 20 first, so Div5 never runs
# CORRECT ORDER: Specific condition (% 5) before broader condition (% 2)
if num % 5 == 0:
print("Divisible by 5") # Runs first!
elif num % 2 == 0:
print("Even")match Statement
Introduced for structural pattern matching, it compares a single variable or expression against multiple specific case blocks.
Ex.
x = 2
match x:
case 1:
print("One")
case 2:
print("Two") # Matches and prints "Two"
case 3:
print("Three")Match vs. If/Elif/Else
match statements are optimized for comparing exact, discrete values (like specific integers or strings). if/elif/else chains are more flexible and better suited for evaluating value ranges, inequalities (>, <)
Default Case
Written using a single underscore (case _:), this acts as a universal catch-all default case within a match statement.
Ex.
x = 99
match x:
case 1:
print("One")
case 2:
print("Two")
case _:
print("Not 1 or 2") # Catches 99Linked Cases
Groups multiple discrete values into a single case using the vertical bar (|). If the expression matches any of the grouped values, that case block executes.
Ex.
digit = 3
match digit:
case 0 | 1 | 2 | 3 | 4:
print("Round down") # Matches because 3 is in the group
case 5 | 6 | 7 | 8 | 9:
print("Round up")Nested Conditional Statement
Placing one conditional structure (such as an if/elif/else or match) completely inside another. The inner (nested) conditional is only evaluated if the outer conditional's branch executes.
Ex.
is_weekday = True
is_holiday = False
if is_weekday:
# Inner conditional only runs if outer is True
if is_holiday:
print("No school (Holiday)")
else:
print("Go to school")
else:
print("No school (Weekend)")Cases for Nesting
Nesting is primarily used for multi-layered decision-making used for even cases and match statements.