New! Explore our Programming Academy and AI Tutor - learn to code from scratch, free to start. Explore Now
Programming Python Conditionals and Loops

Conditionals and Loops

🐍 Python

Master if/elif/else and for/while loops in Python.

Lesson 2 of 7 Tutorial
0/7 completed

Conditionals and Loops

If/Elif/Else

score = 75

if score >= 90:
    grade = "A"
elif score >= 80:
    grade = "B"
elif score >= 70:
    grade = "C"
else:
    grade = "F"

print(f"Grade: {grade}")

For Loop

for i in range(5):
    print(i)   # 0, 1, 2, 3, 4

fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)

While Loop

count = 0
while count < 5:
    print(count)
    count += 1

Example

# Grade Calculator
scores = [95, 67, 82, 45, 73, 88, 52]

for score in scores:
    if score >= 90:
        grade = "A"
    elif score >= 80:
        grade = "B"
    elif score >= 70:
        grade = "C"
    elif score >= 60:
        grade = "D"
    else:
        grade = "F"
    print(f"Score {score} = Grade {grade}")

# Count passing and failing
passing = sum(1 for s in scores if s >= 50)
failing = len(scores) - passing
print(f"
Passing: {passing}, Failing: {failing}")

Exercises

Exercise 1. Sum Numbers 1 to 5 Medium
Use a for loop with range(1, 6) to add up the numbers 1 to 5 and print the total.
The printed total must be exactly 15.
total = 0
# your loop here
print(total)
Expected Output
15

Lesson Nav

Lesson sections