Python lesson
if and else
70 is more than 50, so the first branch runs and the else is skipped. Change revenue to 20 and you'd see Small order instead.
Try this lesson free →Example
if runs its indented lines only when a comparison is True. else runs when it's False:
revenue = 70
if revenue > 50:
print("Big order")
else:
print("Small order")Big orderKey points
if revenue > 50:A comparison, then a colon. Its indented lines run only if it's True.
else:No comparison needed. It runs when the if was False.
Indent4 spaces tell Python which lines belong to each branch.
Exactly one branch runs: the if or the else, never both.
Practice
1. Build the first line of a test: is revenue over 100?
if revenue > 100:2. Build a test: is the store Lagos?
if store == "Lagos":3. Print Bulk if qty is 3 or more. Otherwise print Single.
qty = 4
if qty >= 3:
print("Bulk")
else:
print("Single")4. Print Huge if revenue is over 100. Otherwise print Normal.
revenue = 140
if revenue > 100:
print("Huge")
else:
print("Normal")5. Print Local if store is Lagos. Otherwise print Ship.
store = "Abuja"
if store == "Lagos":
print("Local")
else:
print("Ship")Learn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.
Try this lesson free →