Python 3D Academyবাংলা

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 if inside another if'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:

  1. Python works out the condition in the middle: 7 % 2 == 0 is False.
  2. So it picks the value after else: "odd".
  3. = puts "odd" into kind.
has_ticket = True
age = 10
if has_ticket:
    if age < 12:
        print("Child ticket")
    else:
        print("Adult ticket")
else:
    print("Buy a ticket first")
Nesting is useful, but `and` often does the same job in a flatter, easier-to-read way.

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 ok gives SyntaxError: expected 'else' after 'if' expression.
  • An else at the wrong depth. An else lined up with the outer if belongs to the outer if, 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 normal if is clearer for running code.

Quiz

  1. What does this print?

    x = 4
    print("big" if x > 3 else "small")
  2. What does this print?

    a = 5
    if a > 0:
        if a > 10:
            print("huge")
        else:
            print("small")
  3. What does this print?

    name = ""
    if name:
        print("yes")
    else:
        print("no")
  4. Which single line means the same as if x > 0: with if x < 10: inside it?

    • if x > 0 and x < 10:
    • if x > 0 or x < 10:
    • if not x:
  5. Which line causes an error?

    x = 5
    label = "big" if x > 3
    print(label)

Coding tasks

  1. Ask for a whole number. Using a one-line conditional expression, make sign hold "positive" if the number is greater than 0, and "not positive" otherwise. Then print sign.

  2. Ask for a username, then a password. If the username is admin, print Welcome! when the password is 1234, and Wrong password otherwise. For any other username, print Unknown user.