If statements allow your program to make decisions based on conditions.
# Basic if statement
age = 18
if age >= 18:
print("You are an adult")
# If-else statement
temperature = 25
if temperature > 30:
print("It's hot outside")
else:
print("It's not too hot")
# If-elif-else statement
score = 85
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
else:
grade = "F"
print(f"Your grade is: {grade}")
# Comparison operators
x = 10
y = 5
print(x == y) # Equal to: False
print(x != y) # Not equal to: True
print(x > y) # Greater than: True
print(x < y) # Less than: False
print(x >= y) # Greater than or equal: True
print(x <= y) # Less than or equal: False
# String comparisons
name1 = "Alice"
name2 = "Bob"
print(name1 == name2) # False
print(name1 < name2) # True (alphabetical order)
# and, or, not operators
has_ticket = True
has_id = True
age = 20
# and operator (both conditions must be True)
can_enter = has_ticket and has_id and age >= 18
print(can_enter) # True
# or operator (at least one condition must be True)
is_weekend = True
is_holiday = False
can_sleep_in = is_weekend or is_holiday
print(can_sleep_in) # True
# not operator (reverses the boolean value)
is_working_day = not (is_weekend or is_holiday)
print(is_working_day) # False
For loops iterate over sequences like lists, strings, or ranges.
# Loop through a list
fruits = ["apple", "banana", "orange"]
for fruit in fruits:
print(f"I like {fruit}")
# Loop through a string
word = "Python"
for letter in word:
print(letter)
# Loop with range()
for i in range(5): # 0, 1, 2, 3, 4
print(f"Count: {i}")
for i in range(1, 6): # 1, 2, 3, 4, 5
print(f"Number: {i}")
for i in range(0, 10, 2): # 0, 2, 4, 6, 8
print(f"Even: {i}")
# Loop with enumerate() to get index and value
colors = ["red", "green", "blue"]
for index, color in enumerate(colors):
print(f"{index}: {color}")
While loops continue as long as a condition is True.
# Basic while loop
count = 0
while count < 5:
print(f"Count is: {count}")
count += 1 # Same as count = count + 1
# User input loop
user_input = ""
while user_input != "quit":
user_input = input("Enter 'quit' to exit: ")
if user_input != "quit":
print(f"You entered: {user_input}")
# Infinite loop with break
while True:
number = int(input("Enter a positive number (0 to quit): "))
if number == 0:
break
if number > 0:
print(f"Square of {number} is {number**2}")
else:
print("Please enter a positive number")
# break - exits the loop completely
for i in range(10):
if i == 5:
break
print(i) # Prints 0, 1, 2, 3, 4
# continue - skips the rest of current iteration
for i in range(10):
if i % 2 == 0: # Skip even numbers
continue
print(i) # Prints 1, 3, 5, 7, 9
# Using break and continue together
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for num in numbers:
if num > 7:
break
if num % 2 == 0:
continue
print(f"Odd number: {num}") # Prints 1, 3, 5, 7
# Nested for loops
for i in range(3):
for j in range(3):
print(f"({i}, {j})")
# Multiplication table
for i in range(1, 4):
for j in range(1, 4):
result = i * j
print(f"{i} x {j} = {result}")
print() # Empty line after each row
# Pattern printing
for i in range(5):
for j in range(i + 1):
print("*", end="")
print() # New line after each row
# Output:
# *
# **
# ***
# ****
# *****
break
and continue
to control loop flow, but don't overuse them as they can make code harder to read.