Control flow allows your program to make decisions and execute different paths of code based on conditions.
Python uses the keywords if, elif (short for else-if), and else to control decision making:
age = 18
if age >= 21:
print("You can enter the club and buy drinks.")
elif age >= 18:
print("You can enter the club but cannot buy drinks.")
else:
print("You are too young to enter the club.")- In Python, code blocks are defined by indentation (typically 4 spaces).
- The colon (
:) at the end of conditional lines is required.
You can combine multiple conditions using logical operators:
and: True if both conditions are True.or: True if at least one condition is True.not: Reverses the boolean value of a condition.
has_id = True
is_vip = False
if has_id and (age >= 18 or is_vip):
print("Welcome!")In Python, values can evaluate to True or False when used in a conditional statement.
NoneFalse0(integer) or0.0(float)- Empty sequences or collections:
"",[],(),{},set()
Any other value in Python is considered Truthy.
name = input("Enter your name: ")
if name: # Checks if name is not an empty string
print(f"Hello, {name}!")
else:
print("You didn't enter a name.")Ternary operators provide a shorthand way to write simple if-else statements.
Syntax: value_if_true if condition else value_if_false
x = 10
status = "even" if x % 2 == 0 else "odd"
print(status) # Output: evenSimilar to switch-case statements in other languages, match statements offer structural pattern matching.
command = "start"
match command:
case "start":
print("Starting the system...")
case "stop" | "halt":
print("Stopping the system...")
case _:
print("Unknown command.") # Default wildcard pattern- Write a program that takes a score (0–100) and prints grades: A (>=90), B (>=80), C (>=70), and F (otherwise).
- Use a ternary operator to assign "allowed" if
ageis >= 18, otherwise "denied".