MyMisPal
← Python lessons
Lesson 5 of 13

Conditions: if, elif, else

PYTHON
score = 85
if score >= 90:
    print("A")
elif score >= 80:
    print("B")
else:
    print("C or below")

The colon : ends the condition line, and the indentation (4 spaces) marks what belongs inside it. In Python, indentation is part of the language, not decoration.

PYTHON
stock = 3
reorder_level = 5
if stock == 0:
    print("Out of stock!")
elif stock < reorder_level:
    print("Low stock: reorder soon")
else:
    print("Stock OK")
Watch out= stores and == compares. if x = 5: is an error; write if x == 5:.

Exercises

Exercise 1

Read a whole number and print even if it's even, odd if it's odd.

Exercise 2

Read an order amount. 1000 or more gets 10% off, 500 or more gets 5% off, otherwise no discount. Print the amount after discount with two decimals.

Spotted a mistake on this page, or have an idea to improve the site?