Vinay Garg Assistant Professor

Lesson 13: Decision making: if, elif and else

A program that always does the same thing is not much of a program. if runs a block when a condition is true, elif adds more cases, and else catches everything left over. Indentation is what marks the block.

A dark code window showing an if condition and a printed grade

In short

  • `if` runs its block only when the condition is True; `else` runs when no earlier branch matched.
  • `elif` adds another case, and Python checks the branches in order, stopping at the first true one.
  • The colon and the indentation are part of the syntax, not decoration.
  • Empty values are false: `0`, `""`, `[]`, `{}` and `None` all behave like False in a condition.

1

branch that runs per if statement

Python docs

4

spaces per indent level, the usual convention

PEP 8

The idea

A conditional statement lets a program choose between paths. In Python there are three parts: if for the first test, elif for further tests, and else for whatever is left.

The condition after if is any expression that produces True or False — usually a comparison from lesson 9.

The program

# decide.py — one grade from a mark

marks = 82

if marks >= 90:
    grade = "A"
elif marks >= 75:
    grade = "B"
elif marks >= 60:
    grade = "C"
else:
    grade = "D"

print("Marks:", marks, "Grade:", grade)

if marks >= 40:
    print("Pass")
else:
    print("Fail")

How it works, step by step

  1. Python tests marks >= 90. For 82 it is false, so the indented line under it is skipped.
  2. It moves to the first elif and tests marks >= 75. That is true, so grade = "B" runs.
  3. Every remaining branch is skipped, even though 82 is also at least 60. Only one branch of an if/elif/else ever runs.
  4. The second statement is independent: it starts again with its own if.

What it prints

Marks: 82 Grade: B
Pass

Indentation is the syntax

Most languages mark a block with braces. Python uses indentation. The colon at the end of the if line opens the block, and every line indented under it belongs to that block. Four spaces is the convention; mixing tabs and spaces is what produces the confusing TabError.

Nested decisions

age = 20
has_id = True

if age >= 18:
    if has_id:
        print("Entry allowed")
    else:
        print("Bring an ID")
else:
    print("Too young")

Nesting works, but three levels is usually a sign to combine conditions or split the work into a function. The same logic written flat is if age >= 18 and has_id:.

Truthiness

You can put a value directly in a condition. Python treats these as false: False, None, 0, 0.0, "", [], {} and set(). Everything else is true. So if name: means "if name is not empty", which is shorter and still clear.

Common mistakes

  • Missing colon at the end of the if line: SyntaxError.
  • Using = instead of == inside the condition.
  • Ordering the branches wrongly. Put the strictest test first, or a broad test such as marks >= 40 will swallow the cases after it.

Practice

Try these before moving on. Each one uses only what this lesson covered.

  1. Program 6: check whether a number is even or odd, and print the answer.
  2. Program 7: find the largest of three numbers using if, elif and else.
  3. Write a grade program: 90 and above A, 75 to 89 B, 60 to 74 C, below 60 D.
  4. Check whether a year is a leap year using the rule of 4, 100 and 400.

Sources

Frequently asked questions

What is the difference between if and elif?

if starts a new decision and is always tested. elif is only tested when every earlier condition was false, so exactly one branch of the chain runs.

Do I always need an else?

No. else is only for the case where you want something to happen when nothing matched. An if on its own is perfectly normal.

Why does Python care about indentation?

Indentation defines the block. There are no braces, so the indented lines under an if are the lines that belong to it. This is why the style is enforced by the language rather than by taste.

Can I write an if statement on one line?

Yes: if marks >= 40: print("Pass") is valid for a single statement. It is fine for something trivial, but a block with more than one line needs proper indentation.

Vinay Garg

Vinay Garg

Assistant Professor of Computer Science at Multani Mal Modi College, Patiala. Twenty-five years of teaching web programming, and a habit of building the things I teach about.