PythonBasicsbeginner

Python If Else Statements with Examples

Learn if, else, elif and nested if in Python with indentation rules, common errors and five small programs with output.

Published
TL;DR – Quick Answer

An if else statement in Python runs one block of code when a condition is True and another block when it is False. You write if condition:, indent the code under it, then write else: for the other case. Use elif to check more conditions in order. Python uses indentation, not curly braces, to mark each block.

On This Page

Programs need to make decisions. Is the number even or odd? Did the student pass? Is the password correct? In Python you make these decisions with if, elif and else.

This page explains the syntax and the indentation rules, then walks through five small programs with their output. You only need Python installed to follow along. If you have not done that yet, see how to install Python on Windows.

The if statement

The simplest decision is "do this only if something is true".

age = 20

if age >= 18:
    print("You can vote")

print("Done")

Output:

You can vote
Done
Part Meaning
if Keyword that starts the decision
age >= 18 The condition. It is either True or False
: The colon ends the condition line. It is required
Indented line The block that runs only when the condition is True

If you change age to 15, the condition is False, so Python skips the indented line and prints only Done. The last print is not indented, so it is outside the if and always runs.

The if else statement

Add else when you want something to happen in the False case too.

number = 7

if number % 2 == 0:
    print(number, "is even")
else:
    print(number, "is odd")

Output:

7 is odd

number % 2 gives the remainder after dividing by 2. An even number leaves remainder 0. else has no condition of its own. It runs whenever the if condition is False.

Indentation rules in Python

Other languages such as C and Java use curly braces { } to group lines into a block. Python uses indentation, which means the spaces at the start of a line.

  • Every line inside a block must have the same indentation.
  • The standard is four spaces per level.
  • if, elif and else of the same decision must start at the same column.
  • A line that goes back to the outer level ends the block.

Here is what happens when you forget to indent:

x = 5
if x > 3:
print("big")

Python stops before running anything. The last line of the error says:

IndentationError: expected an indented block after 'if' statement on line 2

And when one line is indented more than the line above it for no reason:

x = 5
if x > 3:
    print("big")
      print("very big")
IndentationError: unexpected indent

Pro tip: Use an editor such as VS Code or IDLE. When you press Enter after a line ending in :, it indents the next line for you. Do not mix tabs and spaces in one file.

Comparison and logical operators

Conditions are built from these operators.

Operator Meaning Example Result
== Equal to 5 == 5 True
!= Not equal to 5 != 3 True
> < Greater than, less than 7 < 2 False
>= <= Greater or equal, less or equal 18 >= 18 True
and Both must be True 5 > 2 and 3 > 8 False
or At least one must be True 5 > 2 or 3 > 8 True
not Reverses True and False not 5 > 2 False

The elif statement

When there are more than two cases, use elif (short for "else if").

marks = 72

if marks >= 75:
    print("Distinction")
elif marks >= 60:
    print("First class")
elif marks >= 35:
    print("Pass")
else:
    print("Fail")

Output:

First class

Python checks the conditions from top to bottom and runs only the first block whose condition is True. Here 72 >= 75 is False, 72 >= 60 is True, so it prints First class and skips the rest. It never checks marks >= 35, even though that is also true.

This is why the order matters. If you put marks >= 35 first, every student with 35 or more would get Pass.

Nested if in Python

A nested if is an if inside another if. Use it when the second check only makes sense after the first one passes.

username = "admin"
password = "1234"

if username == "admin":
    if password == "1234":
        print("Login successful")
    else:
        print("Wrong password")
else:
    print("Unknown user")

Output:

Login successful

The inner if and else are indented one more level, so they belong to the outer if. Try changing the password to see Wrong password, then the username to see Unknown user.

Do not nest too deeply. Two levels are fine. If you reach four or five, rewrite the logic with elif or and.

5 small programs with output

Type each program into a .py file and run it. Programs that use input() show a sample run, with the value the user typed.

Program 1: positive, negative or zero

n = int(input("Enter a number: "))

if n > 0:
    print(n, "is positive")
elif n < 0:
    print(n, "is negative")
else:
    print(n, "is zero")

Sample run:

Enter a number: -5
-5 is negative

input() always returns text. int() converts that text into a whole number so you can compare it with 0.

Program 2: largest of three numbers

a = 12
b = 45
c = 30

if a >= b and a >= c:
    print("Largest is", a)
elif b >= c:
    print("Largest is", b)
else:
    print("Largest is", c)

Output:

Largest is 45

If a is not the largest, the answer must be b or c, so the elif only has to compare those two. The same problem is solved step by step in find the largest of three numbers.

Program 3: leap year check

year = int(input("Enter a year: "))

if year % 400 == 0:
    print(year, "is a leap year")
elif year % 100 == 0:
    print(year, "is not a leap year")
elif year % 4 == 0:
    print(year, "is a leap year")
else:
    print(year, "is not a leap year")

Sample run:

Enter a year: 1900
1900 is not a leap year
Year Divisible by 400? Divisible by 100? Divisible by 4? Result
2000 Yes - - Leap year
1900 No Yes - Not a leap year
2024 No No Yes Leap year
2026 No No No Not a leap year

The order of checks is what makes this work. More versions are in the leap year program in Python.

Program 4: electricity bill by slab

units = 250

if units <= 100:
    bill = units * 2
elif units <= 200:
    bill = 100 * 2 + (units - 100) * 3
else:
    bill = 100 * 2 + 100 * 3 + (units - 200) * 5

print("Units:", units, "Bill: Rs.", bill)

Output:

Units: 250 Bill: Rs. 750

The rates here are made up for practice: Rs. 2 for the first 100 units, Rs. 3 for the next 100, and Rs. 5 after that. For 250 units the bill is 200 + 300 + 250 = 750.

Program 5: even or odd with a one-line if

Python has a short form called a conditional expression. It picks one of two values in a single line.

n = 15
result = "even" if n % 2 == 0 else "odd"
print(n, "is", result)

Output:

15 is odd

Read it as "result is "even" if the condition is true, else "odd"". Use it only for simple choices between two values. For anything longer, a normal if else is easier to read.

Common mistakes with if else

Mistake Example Fix
Missing colon if x > 3 Write if x > 3:. Python reports SyntaxError: expected ':'
Using = instead of == if x = 5: = stores a value. Use == to compare
Comparing text with a number age = input() then if age >= 18: Convert first: age = int(input()). Otherwise you get a TypeError
Wrong order of elif elif marks >= 35 before elif marks >= 60 Put the strictest condition first
Writing else if else if x > 0: Python uses elif
Uneven indentation 4 spaces on one line, 2 on the next Use 4 spaces for every level

Common mistake: Expecting an empty string or 0 to behave like a normal value. In a condition, 0, "", an empty list [] and None count as False. Everything else counts as True. So if name: means "if name is not empty".

Quick revision

  1. if condition: runs the indented block when the condition is True.
  2. else: runs when the if condition is False. It has no condition.
  3. elif checks more conditions. Only the first True block runs.
  4. Indentation marks the block. Use four spaces.
  5. Combine conditions with and, or and not.
  6. Convert input() with int() before comparing numbers.

Decisions become more useful when you repeat them. The next step is loops, for example checking every number from 1 to 100 for even or odd. After that, learn Python lists so you can store many values and test each one with if.

Frequently Asked Questions

What is the syntax of if else in Python?
Write if, a condition and a colon, then the indented code to run when the condition is True. On a new line at the same level as if, write else and a colon, then the indented code for the False case.
What is elif in Python?
elif is short for else if. It lets you check another condition when the earlier ones were False. Python checks each condition from top to bottom and runs only the first block whose condition is True.
How many spaces should I use for indentation in Python?
Four spaces is the standard style. Python only needs every line in the same block to use the same indentation, but mixing tabs and spaces causes errors, so let your editor insert four spaces.
Can I write if without else in Python?
Yes. else is optional. If the condition is False and there is no else, Python simply skips the block and moves on.
Does Python have a switch statement?
Python 3.10 added the match statement, which works like switch in other languages. For most beginner programs, if, elif and else are enough and easier to read.

Want to Build Your Career in Data Science with AI?

Join CodeBegun and train with working industry engineers — Explore the Data Science program

Apply for Demo Class →
Siva Prasad Galaba
Founder, CodeBegun · Staff Engineer

Founder of CodeBegun. 15+ years building Java systems at companies like Crunchyroll. Teaches Java, Spring Boot and system design the way the industry actually works, and mentors students through projects, mock interviews and placement preparation.

Technically reviewed by CodeBegun Technical TeamLast reviewed 5 December 2026 LinkedIn
Chat with us