Everyone Can Learn Program
中文 Start learning
Course 2 · Python BasicsLesson 8 of 14Free to read

elif, and, or, not · Python from Zero

What if there are more than two choices, or several conditions at once?

Course contents

Why you need it

Grades aren't just pass/fail: they're A, B, C or F. Free shipping might need "order over 50 and inside the country". elif handles many choices; and, or, not combine conditions.

The intuition

elif means "else, if...". Python checks the conditions from top to bottom and runs the first one that is True, then skips the rest:

if score >= 80:
    print("A")
elif score >= 50:
    print("C")
else:
    print("F")

A score of 90 is also >= 50, but only "A" prints: the first match wins. So put the strictest test first.

Combining conditions:

  • a and b: True only if both are True.
  • a or b: True if at least one is True.
  • not a: flips True and False.

Definition

elif

elif (else if) adds another condition to an if statement; the first condition that is True has its block run and the rest are skipped.

Logical operator

The logical operators and, or and not combine or invert Boolean values.

Examples

Letter grades

Try it yourself

Try 95, 70, 55 and 20.

score = int(input())
if score >= 80:
    print("A")
elif score >= 65:
    print("B")
elif score >= 50:
    print("C")
else:
    print("F")
Input (one line per input() call)
70
Step by step
  1. L270 >= 80? False: skip.
  2. L470 >= 65? True: this block runs.B
  3. L6The remaining checks are skipped entirely, even though 70 >= 50.

and / or / not

Try it yourself

Change temp and raining and predict the output before running.

temp = 28
raining = False
print(temp > 25 and not raining)
print(temp < 10 or raining)
Step by step
  1. L3temp > 25 is True; not raining is True; True and True → True.True
  2. L4temp < 10 is False; raining is False; False or False → False.False

Common mistakes

Wrong order of elif

If score >= 50 comes before score >= 80, an 85 gets the lower grade. Put the strictest condition first.

`x == 1 or 2`

This doesn't mean "x is 1 or 2". Write both comparisons in full: x == 1 or x == 2.

Quick quiz

1. What does this print?

x = 15
if x > 10:
    print("A")
elif x > 5:
    print("B")
else:
    print("C")
  1. A
    A
  2. B
    A
    B
  3. C
    B
Show the answer

Answer: A

The first True condition wins; the elif is never checked.

2. What does this print?

print(5 > 3 and 2 > 4, 5 > 3 or 2 > 4)
  1. A
    False True
  2. B
    True False
  3. C
    True True
Show the answer

Answer: A

and needs both (one is False → False); or needs one (True).

Practice

3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.

  1. PredictWeather words

    Type exactly what this program prints.

  2. ModifyAdd grade B

    Add a grade B for scores from 65 to 79. The others stay: A for 80+, C for 50–64, F below 50.

  3. WriteLeap year

    Read a year. Print leap if it is a leap year, otherwise common. A year is a leap year if it is divisible by 4 and not by 100, or divisible by 400.

Practise this lesson

Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.

Interactive lessons open in order. If you have not done the earlier ones yet, you start at the course map.

Terms in this lesson

TermChineseDefinition
elifelif 分支An extra condition in an if statement, checked only if the ones above were False.
Logical operator逻辑运算符and, or and not, which combine or invert Boolean values.
First match wins首个匹配生效In an if/elif chain only the first True condition's block runs.