PYTHON LESSON 8 OF 32

More choices: elif & logic

elif adds another condition. and requires both conditions to be true; or needs at least one; not flips a boolean. Python checks branches from top to bottom and takes the first matching branch.

हिन्दी में समझें

elif दूसरी शर्त जोड़ता है। and में दोनों, or में कम से कम एक शर्त सही चाहिए। not बूलियन उलटता है। Python ऊपर से नीचे पहली सही शाखा चुनता है।

अभ्यास: age को 20 करें ताकि Adult शाखा चले।

Hinglish mein samjhein

elif se aur choices jodo. and mein dono conditions true, or mein ek bhi true ho to chalega. not boolean ulat deta hai. Pehli matching branch chalti hai.

Practice: age 20 karo taaki Adult branch chale.

A small Python example

age = 15
if age < 10:
    print("Junior")
elif age < 18:
    print("Teen")
else:
    print("Adult")

The browser playground runs real Python with Pyodide. Use sample variables instead of interactive input(). Browser package support differs from desktop Python.

Try this example in the Python playground →

Practise this idea

Set age to 20 so the Adult branch runs.

Check answer & explanation
age = 20
if age < 10:
    print("Junior")
elif age < 18:
    print("Teen")
else:
    print("Adult")

20 fails both age < 10 and age < 18. The final else branch therefore labels it Adult.

Stuck in the course? Choose Check answer for either the challenge or quiz. Read the explanation and continue to the next lesson at your own pace.

Check your understanding

What prints for age 15?

  1. Junior
  2. Teen
  3. Adult
Check the quiz answer

Teen

15 is not below 10, but is below 18.

References & corrections

Use the official Python documentation for full language rules. Read how these lessons are made and checked. Found a mistake? Report it to Excel Keys with this lesson URL and a reproducible example.

FROM LEARNING TO BUSINESS RESULTS

Need a dashboard for your team?

Excel Keys designs custom Excel and HTML dashboards around your own KPIs and Excel files. They run offline in the browser, so your data stays on your computer.