Decisions with if

Introduction

Programs often need to choose: pass or fail, even or odd, allowed or not. An if statement checks a condition and runs a block of code only when the condition is True.

Up to now, every line of a program has run. Real programs must choose: is this password right, did this student pass, is the cart empty? Almost all program logic is built from small choices like these.

How it works

if and else

n = int(input())
if n % 2 == 0:
    print(n, "is even")
else:
    print(n, "is odd")

Input used: 7

Open in compiler

Step through line 2. The calculation box shows n % 2 == 0 → 7 % 2 == 0 → False, and the explanation says the block is skipped, so the next line to run is the one under else:. This example was recorded with the input 7; type another number into the Input box and run it again.

Comparisons give True or False:

OperatorMeaning
==equal to
!=not equal to
< and >less than, greater than
<= and >=less or equal, greater or equal

Combining conditions

age = 20
has_id = True
if age >= 18 and has_id:
    print("You can vote")
Open in compiler

and needs both sides to be True; or needs at least one; not flips True and False. The calculation box shows age >= 18 and has_id → 20 >= 18 and True → True.

Syntax

if marks >= 80:
    grade = "A+"
elif marks >= 40:
    grade = "C"
else:
    grade = "F"

Example

marks = int(input())
if marks >= 80:
    grade = "A+"
elif marks >= 70:
    grade = "A"
elif marks >= 60:
    grade = "B"
elif marks >= 40:
    grade = "C"
else:
    grade = "F"
print("Grade:", grade)

Input used: 73

Open in compiler

Python checks each condition from the top and runs only the first block whose condition is True; the rest are skipped. With 73 marks, marks >= 80 is False and marks >= 70 is True, so the grade is A. (This is a simplified grading scale for practice.)

Line by line

  1. marks = int(input()) reads the marks, which are 73 in this recording.
  2. marks >= 80 is False, so that block is skipped.
  3. marks >= 70 is True, so grade becomes "A".
  4. The remaining tests are not even looked at. This is why elif marks >= 60 does not need to say "and less than 70".
  5. print shows the grade.

Expected result

With this input:

73

it prints:

Grade: A

In the real world

Checkout pages charge delivery only below a certain total, and apps show different screens to users who are logged in. When you read a program, try saying each condition aloud as a question.

Common mistakes

  • Writing = instead of == in a condition: if n = 5: is a SyntaxError.
  • Forgetting the colon : at the end of if, elif and else.
  • Putting conditions in the wrong order: if marks >= 40 came first, 95 marks would get a C.

Best practices

  • Test the most specific case first.
  • Give a long condition a name, such as can_vote = age >= 18 and has_id.
  • Keep each branch short.

Summary

if, elif and else choose which block runs, and only the first true branch does. Comparisons give True or False, and and, or and not combine them.

Practice

Read a year and print whether it is a leap year: divisible by 4, but not by 100 unless it is also divisible by 400. Test it with 2024, 1900 and 2000.

Quick check

1. What does 10 != 10 give?
2. With marks = 85, which grade does the grade example print?
3. When is (a > 0 or b > 0) True?

Key takeaways

  • Only the first true branch of a chain runs.
  • = stores a value and == compares two values.
  • The order of the tests matters.

Practice problems for this lesson

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.