Nested ifs and one-line choices
🌍 In real life. A bus conductor first asks "do you have a ticket?". Only if you do, they ask a second question: "are you under 12?", to check it's the right kind of ticket. The second question lives inside the first.
And some choices are so small they fit in one breath: "tea if it's morning, else coffee".
The idea in one sentence: an if can sit inside another if's block, and a if condition else b picks one of two values in a single line.
Its words:
- nested if: an
ifinside anotherif's block, indented one more step - conditional expression:
value_if_true if condition else value_if_false, which gives back a value
Step by step, for kind = "even" if n % 2 == 0 else "odd" when n is 7:
- Python works out the condition in the middle:
7 % 2 == 0is False. - So it picks the value after
else:"odd". =puts"odd"intokind.
has_ticket = True
age = 10
if has_ticket:
if age < 12:
print("Child ticket")
else:
print("Adult ticket")
else:
print("Buy a ticket first")To pick one of two values, Python has a one-line form, the conditional expression:
value_if_true if condition else value_if_false
In one line: kind = "even" if n % 2 == 0 else "odd"
n = 7
kind = "even" if n % 2 == 0 else "odd"
print(kind)How Python reads them. A nested if is just a block like any other: the inner if is only reached when the outer condition is True, and each else belongs to the if at the same indentation.
A conditional expression is a value, not a block, so it always needs both the if and the else: Python must have something to give back either way. Use it for small choices of a value; use a normal if when you want to run several lines.
A condition doesn't have to be a comparison. if name: uses the rule from Lesson 5.4: an empty string counts as False. It's a short way to check that the user typed something.
name = ""
if name:
print("Hi", name)
else:
print("No name")Mistakes and edge cases.
- A conditional expression without else.
x = "a" if okgivesSyntaxError: expected 'else' after 'if' expression. - An else at the wrong depth. An
elselined up with the outerifbelongs to the outerif, not the inner one. - Nesting too deep. Three or four levels are hard to read.
if has_ticket and age < 12:often says the same thing flat. - Using a one-liner for actions.
print("a") if ok else print("b")works, but a normalifis clearer for running code.
Quiz
What does this print?
x = 4 print("big" if x > 3 else "small")What does this print?
a = 5 if a > 0: if a > 10: print("huge") else: print("small")What does this print?
name = "" if name: print("yes") else: print("no")Which single line means the same as
if x > 0:withif x < 10:inside it?- if x > 0 and x < 10:
- if x > 0 or x < 10:
- if not x:
Which line causes an error?
x = 5 label = "big" if x > 3 print(label)
Coding tasks
Ask for a whole number. Using a one-line conditional expression, make
signhold"positive"if the number is greater than 0, and"not positive"otherwise. Then printsign.Ask for a username, then a password. If the username is
admin, printWelcome!when the password is1234, andWrong passwordotherwise. For any other username, printUnknown user.