Conditional Statemants
Double Branch
if condition:
statement
# ...
else:
statement
# ...
Multiple Branches
if condition1:
statement
elif condition2:
statement
elif condition3:
statement
else:
statement
Examples
# Single branch
gpa_str = input("Enter your GPA:")
gpa = float(gpa_str)
if gpa > 3.5:
print(f"GPA: {gpa}")
# Double branch
height_str = input("Enter height (cm):")
height = int(height_str)
if height >= 180:
print("Above average height")
else:
print("Below average height")
# Multiple branches
month_input = input("Enter month number (1-12):")
month = int(month_input)
if month == 1:
print("January")
elif month == 2:
print("February")
elif month == 3:
print("March")
elif month == 4:
print("April")
elif month == 5:
print("May")
elif month == 6:
print("June")
elif month == 7:
print("July")
elif month == 8:
print("August")
elif month == 9:
print("September")
elif month == 10:
print("October")
elif month == 11:
print("November")
else:
print("December")
While Loop
while condition:
statement1
statement2
# ...
The loop terminates when the condition evaluates too False.
Example: Number Guesssing Game
import random
attempts = 0
user_wins = 0
system_wins = 0
while attempts < 5:
print(f"Score - You:System is {user_wins} : {system_wins}")
user_choice = int(input('Choose [0:Rock 1:Scissors 2:Paper]: '))
system_choice = random.randint(0, 2)
if user_choice == 0 and system_choice == 1:
print("You scored!")
attempts += 1
user_wins += 1
elif user_choice == 0 and system_choice == 2:
print("System scored!")
attempts += 1
system_wins += 1
elif user_choice == 1 and system_choice == 0:
print("System scored!")
attempts += 1
system_wins += 1
elif user_choice == 1 and system_choice == 2:
print("You scored!")
attempts += 1
user_wins += 1
elif user_choice == 2 and system_choice == 0:
print("You scored!")
attempts += 1
user_wins += 1
elif user_choice == 2 and system_choice == 1:
print("System scored!")
attempts += 1
system_wins += 1
else:
print("Draw - no points awarded")
print(f"Final Score: {user_wins} : {system_wins}")
if user_wins > system_wins:
print("Congratulations, you win!")
else:
print("System wins this time!")
For Loop
Syntax
for element in iterable:
statement
# ...
The loop variable represents each item in the sequence. The loop iterates through all elements in the container.
Example
university = 'Zhejiang University of Technology'
for character in university:
print(character)
# pass serves as a placeholder when no operation is needed
The range() Function
The range() function generates a sequence of numbers:
range(start, stop, step)
- start: initial value (inclusive)
- stop: final value (exclusive, left-closed right-open interval)
- step: increment value
Practical Applications
# Print numbers 0-9
for i in range(10):
print(i)
# Print even numbers from 0-10
for i in range(0, 11, 2):
print(i)
# Print countdown from 10 to 1
for i in range(10, 0, -1):
print(i)
Loop with Else Clause
Both for and while loops support an optional else block that executes when the loop completes normally (not interrupted by break).
# For-else example
numbers = [3, 7, 11, 14, 17]
target = 10
for num in numbers:
if num == target:
print(f"Found {target}")
break
print(f"Checking {num}")
else:
print(f"{target} not found in the list")
# While-else example
counter = 0
while counter < 5:
print(f"Iteration {counter}")
counter += 1
else:
print("Loop finished without break")
Nested Loops
Loops can be nested within other loops:
# Multiplication table
for i in range(1, 4):
for j in range(1, 4):
print(f"{i} x {j} = {i*j}")
print("---")