Skip to the content

else and elif

Python · Unit 2: Conditionals

else is the other path: it runs when the condition of the if is False.

It takes no condition, it lines up with the if and it also ends with :.

age = 15
if age >= 18:
    print("Adult")
else:
    print("Minor")

Prints

Minor

The rest of the explanation is in the lesson, which is part of the full plan.

Exercises in this lesson

You do them in the app, which checks them on the spot and explains why.

  1. 1. Predict the output

    What does this code print?

  2. 2. Complete the code

    Fill in the blank so it says whether the number is even or odd.

  3. 3. Multiple choice

    When does the block of the else run?

  4. 4. Predict the output

    What does this code print?

  5. 5. Complete the code

    Fill in the blank so it prints a single message.

  6. 6. Find the bug

    It should print a single message: "Pricey" if it is over 500, "Normal" if it is over 100 and "Cheap" if not. Which line has the error?

  7. 7. Predict the output

    What does this code print?

  8. 8. Multiple choice

    Jordan scored 100, but this code says "Passed" instead of "Excellent". How do you fix it?

  9. 9. Put the lines in order

    The person types 15. Put the lines in order to sort them: child under 12, teen under 18 and adult at 18 or older.

  10. 10. Predict the output

    What does this code print?

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →