Python, from Zero, for AILesson 9 of 89

Module 1 · Getting Python running, and your first working code

Making a decision: if, elif, else

In this lesson

  • 7 min read
  • 7 reading sections
  • Knowledge check
  • Lesson 9 of 10 in this chapter

This chapter’s goalInstall Python on a laptop, a phone or a free browser machine, run the same code both interactively and from a saved file, and predict what a short program prints by naming the type of every value in it and the branch a condition takes.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmWatch1:49Not available
Full audiobookNot availableNot available

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 9Free · no account needed
Lesson steps

The shape

Python
marks = 72

if marks >= 75:
    grade = "distinction"
elif marks >= 60:
    grade = "first"
elif marks >= 40:
    grade = "pass"
else:
    grade = "fail"

print(grade)

Four things are load-bearing here.

The colon ends the condition line. Forget it and you get SyntaxError: expected ':'.

The indentation decides what belongs to the branch. Four spaces, consistently. Python does not care whether it is four or two, but it cares that you never change your mind inside one file, and mixing tabs with spaces produces TabError at a line that looks identical to the one above it. Set your editor to insert spaces when you press Tab and forget about it.

elif means “otherwise, if” and only one branch ever runs. The moment a condition is true, Python executes that block and skips the rest of the chain.

else has no condition. It catches everything left.

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Stuck on something?Ask Divya, your AI tutor

How to cite: Addaly (2026) ‘Making a decision: if, elif, else’, in Python, from Zero, for AI. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/python-for-ai/if-and-branching (Accessed: 21 November 2026).