Conditional Statements in Python

Ever wondered how software knows what to do based on your input? For example:

  • An ATM shows a message if you enter the wrong PIN.
  • A grading app gives you a grade based on your score.

This is all possible through Conditional Statements — and Python makes it super simple!

Conditional Statements in python

What are Conditional Statements?

In Python, conditional statements allow your program to make decisions — run certain blocks of code only if a condition is met.

It’s like giving your code a brain — “If this is true, do this. Otherwise, do something else.”


The if Statement

The simplest form of condition.

age = 20

if age >= 18:
    print("You are eligible to vote.")

How it works:

  • Python checks if age >= 18.
  • If True, it prints the message.
  • If False, it does nothing.

👉 Note: Indentation (4 spaces or a tab) is required after if to tell Python which lines belong inside the condition.


The if-else Statement

Use this when you want to take one action if true, and another if false.

marks = 35

if marks >= 40:
    print("You passed the exam!")
else:
    print("You failed. Try again.")

This gives the program two paths: one for pass, one for fail.


The if-elif-else Ladder

What if there are multiple options? Use elif (short for else if).

score = 85

if score >= 90:
    print("Grade: A")
elif score >= 75:
    print("Grade: B")
elif score >= 60:
    print("Grade: C")
else:
    print("Grade: D")

Python checks conditions top to bottom, and runs the first one that is True.

Also Read: Operators in Python


Operators Used in Conditions

Here are some important operators:

OperatorMeaningExample (a = 10, b = 5)
==Equal toa == bFalse
!=Not equal toa != bTrue
>Greater thana > bTrue
<Less thana < bFalse
>=Greater or equala >= 10True
<=Less or equalb <= 5True

Nested if Statements

You can put one if inside another to check more complex logic.

username = input("Enter your username: ")
password = input("Enter your password: ")

if username == "admin":
    if password == "1234":
        print("Login successful!")
    else:
        print("Incorrect password!")
else:
    print("Username not found!")

Common Mistakes to Avoid

Forgetting Indentation:

if age >= 18:
print("Adult")   # ❌ Error!

✔️ Correct:

if age >= 18:
    print("Adult")

Using = Instead of ==

if x = 5:    # ❌ Wrong (assignment, not comparison)

✔️ Correct:

if x == 5:   # ✅

Mini Project

Project: Even or Odd Checker

Let’s apply what you’ve learned.

Task:

  1. Ask the user for a number
  2. Check if it’s even or odd
  3. Print the result
num = int(input("Enter a number: "))

if num % 2 == 0:
    print("It is an Even number.")
else:
    print("It is an Odd number.")

💡 % is the modulus operator. It gives the remainder. If the remainder is 0, the number is even.


Summary

StatementPurpose
ifRun code only when condition is true
if-elseDo one thing if true, another if false
if-elif-elseChoose between multiple conditions
nested ifRun conditions inside other conditions

Project Challenge 1: Simple Grading System

Objective:

Create a Python program that takes a student’s marks (0–100) as input and displays the corresponding grade based on this scale:

MarksGrade
90 – 100A+
75 – 89A
60 – 74B
40 – 59C
0 – 39F

Hints:

  • Use input() and int() to take marks
  • Use if-elif-else to determine the grade
  • Make sure to handle invalid inputs like marks above 100 or below 0 (optional challenge)

Sample Output:

Enter your marks: 82  
You got Grade A

Project Challenge 2: Basic Salary Tax Calculator

Objective:

Create a program that calculates the income tax based on salary input using these slabs:

Salary Range (₹)Tax Rate
0 – 2,50,000No Tax
2,50,001 – 5,00,0005%
5,00,001 – 10,00,00010%
Above 10,00,00020%

Hints:

  • Ask user to input their annual salary
  • Use if-elif-else to check which range it falls into
  • Use multiplication (*) to calculate tax
  • Print the tax amount

Sample Output:

Enter your annual salary: 750000  
You need to pay75000.0 as income tax.

What’s Next?

In the next post, we’ll learn about the Loops in Python

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Translate »
Scroll to Top