Lesson 6
18 min
if, elif, else and indentation to make your program take decisions.
Definition
A condition is a statement that lets a programchoose what it runs depending on an expression that is eitherTrueorFalse. In Python, it is written with the keywordsif,elifandelse, and the code involved is grouped in an indented block.
A condition lets you run a block of code only if an expression is true. In Python, you use theifkeyword, followed by a condition, a colon, then anindented block of code (shifted by 4 spaces).
age = 20
if age >= 18:
print("You are an adult.")
print("End of the program")Only the indented line depends on the condition. The last line, which is not indented, runs in every case. Ifagewere 15, Python would simply skip theifblock and display only “End of the program”.

In many languages, a block is delimited with curly braces. In Python, it’sindentation that plays this role. All the lines of the same block must be shifted by the same number of spaces, and the line that introduces the block must end with a colon.
temperature = 30
if temperature > 25:
print("It's hot.") # inside the block
print("Remember to hydrate.") # still inside the block
print("Have a nice day!") # outside the block, runs in every caseelsedefines what happens when the condition is false. It has no condition of its own: it simply covers “all the other cases”.
age = int(input("How old are you? "))
if age >= 18:
print("Access granted.")
else:
print("Access denied.")To test several conditions in a row, you useelif(short for “else if”). Python stops as soon as one condition is true, so the order of the tests matters. You can chain as manyelifas you like.
grade = float(input("Your grade out of 20: "))
if grade >= 16:
print("Very good")
elif grade >= 14:
print("Good")
elif grade >= 10:
print("Pass")
else:
print("Insufficient")You can useand,orandnotto build richer conditions, as well as theinkeyword to check whether an element is present.
day = "Saturday"
hour = 10
if day in ("Saturday", "Sunday") and hour >= 9:
print("The store is open.")
else:
print("The store is closed.")A block can itself contain another condition: these are callednested conditions. Each level adds 4 spaces of indentation. Use this technique in moderation: if your code drifts too far to the right, it can often be simplified withandorelif.
age = 20
has_ticket = True
if age >= 18:
if has_ticket:
print("Welcome to the concert!")
else:
print("You need a ticket.")
else:
print("Adults only.")In Python, some values are considered false in a condition:0, the empty string"", the empty list,NoneandFalse. Everything else is considered true.
name = input("Your name: ")
if name:
print(f"Hello {name}")
else:
print("You didn't type anything.")When you simply want to choose between two values, Python offers a one-line syntax called a conditional expression. It reads almost like a sentence: “value if the condition is true, otherwise another value”.
age = 20
status = "adult" if age >= 18 else "minor"
print(status) # adultif, of theelifand of theelsemust end with:.if age = 18:causes an error; you need to writeif age == 18:.IndentationError.grade >= 10beforegrade >= 16, the second case will never be reached.Good to know
Indentation is not decorative in Python: it defines the blocks of code. Mixing tabs and spaces causes anIndentationError. VS Code uses 4 spaces by default, so don’t change that.
Exercise
Going further
%operator). Then try rewriting the even-or-odd test as a one-line conditional expression.