Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 02

Simple if Statement — Conditional Branching

Use Python simple if statements for one-time decisions. Compare values, validate input, and run blocks only when conditions are true.

Beginner25 min

Conditional (Selection / Branching) statements

The purpose of conditional statements is to perform a certain operation only once, depending on condition evaluation.

In other words: perform X-Operation when true, or Y-Operation when false.

Python classifies conditional statements into 4 types:

  1. Simple if statement
  2. if..else statement
  3. if..elif..else statement
  4. match..case statement

This lesson covers simple if — multiple independent if blocks, each evaluated separately.

Syntax

python
if test_condition:
    block_of_statements

Each if runs its block only when its condition is true. Other if blocks are still checked independently.

Example 1 — Biggest of two numbers

python
a = float(input("Enter first value:"))
b = float(input("Enter second value:"))

if a > b:
    print("Bigger {},{} value is {}".format(a, b, a))
if b > a:
    print("Bigger {},{} value is {}".format(a, b, b))
if a == b:
    print("Both are equal")

print("program execution is completed")

Example 2 — Biryani token check

python
kot = input("Do you have chicken biryani token(yes/no):")

if kot.lower() == "yes":
    print("Go and sit in table")
    print("wait for biryani")
    print("eat biryani")

if kot.lower() == "no":
    print("Go to another restaurant")

Example 3 — Biggest of three numbers

python
a = float(input("Enter Value of a:"))
b = float(input("Enter Value of b:"))
c = float(input("Enter Value of c:"))

if (a >= b) and (a >= c):
    print("Big({},{},{})={}".format(a, b, c, a))
if (b >= a) and (b >= c):
    print("Big({},{},{})={}".format(a, b, c, b))
if (c >= a) and (c >= b):
    print("Big({},{},{})={}".format(a, b, c, c))

Example 4 — Digit name and classification

python
d = int(input("Enter a Digit:"))

if d == 0:
    print("{} is ZERO".format(d))
if d == 1:
    print("{} is ONE".format(d))
if d == 2:
    print("{} is TWO".format(d))
if d == 3:
    print("{} is THREE".format(d))
if d == 4:
    print("{} is FOUR".format(d))
if d == 5:
    print("{} is FIVE".format(d))
if d == 6:
    print("{} is SIX".format(d))
if d == 7:
    print("{} is SEVEN".format(d))
if d == 8:
    print("{} is EIGHT".format(d))
if d == 9:
    print("{} is NINE".format(d))

if (d < 0) and d not in [-1, -2, -3, -4, -5, -6, -7, -8, -9]:
    print("{} is -VE Number".format(d))
if d in [-1, -2, -3, -4, -5, -6, -7, -8, -9]:
    print("{} is -Ve Digit".format(d))
if d not in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] and d > 0:
    print("{} is NUMBER".format(d))

print("Program execution completed")

When to use simple if

Use multiple simple if blocks when more than one condition can be true at the same time (for example, printing both "biggest" and "equal" checks separately).

For mutually exclusive choices, prefer if..else or if..elif..else.

Knowledge check

Q1: Does a simple if stop other if blocks from running?
Answer: No — each if is evaluated independently.

Next: File 03 — if..else Statement

Loading playground…