← Back to Home

Conditional Statements in Python

By Instructor

Conditional Statements woh hoti hain jo program ko decision lene me help karti hain. They control program execution based on True or False conditions.

This comprehensive guide covers if, if-else, if-elif-else ladders, nested if statements, real-world ATM and Login authentication systems, ternary operators, and match-case statements.

By Instructor8 chaptersPython 3.xBeginner to Advanced
conditions.py
age = 18

if age >= 18:
    print("licence baan jaega")
else:
    print("pehle padh le")
python conditions.py → licence baan jaega
Chapter 1

What are Conditional Statements

Conditional Statements woh hoti hai jo program ko decision lene me help karti hai. Executions dynamic control flow provide karti hain depend karta hai ki specific condition satisfied (True) hai ya unsatisfied (False).

Core Concept

Control flow decision-making relies on comparison operators (==, !=, >, <, >=, <=) and logical operators (and, or, not).

Chapter 2

Simple if & if-else

The if statement outputs results when conditions evaluate to True. An else block handles the fallback when conditions evaluate to False.

Example 1: License Eligibility Check

license_check.py
name = input("enter your name = ")
age = int(input("apni umar btao :) "))

if age >= 18:
    print("licence baan jaega")
else:
    print("pehle padh le jyada ho rha")

Example 2: Equality Comparison

shirt_check.py
ankit = input("what is your colour shirt :) ")

if ankit == "yellow":
    print("aaj basant pachmi hai ")
else:
    print("woh toh waise hi pheni thi ")

Example 3: Even and Odd Check using Modulo %

even_odd.py
number = int(input("Enter a number: "))

if number % 2 == 0:
    print("The number is even")
else:
    print("The number is Odd")
Chapter 3

Multi-Condition if-elif-else Ladder

When evaluating more than two branch conditions, the if-elif-else chain executes sequentially until a matching condition is found.

Example 1: Student Grading System

student_grading.py
marks = int(input("Enter marks: "))

if marks >= 90:
    print("Grade A")
elif marks >= 85:
    print("Grade B")
elif marks >= 75:
    print("Grade C")
elif marks >= 55:
    print("Grade D")
elif marks >= 45:
    print("Grade E")
elif marks >= 35:
    print("Grade F")
else:
    print("Fail")

Example 2: Positive, Negative, or Zero Checker

num_sign.py
num = int(input("Enter a number: "))

if num > 0:
    print("The number is Positive")
elif num < 0:
    print("The number is Negative")
else:
    print("The number is Zero")

Example 3: Simple Calculator App

calculator.py
value = input("Enter operator (+, -, *, /): ")
n1 = int(input("Enter first number: "))
n2 = int(input("Enter second number: "))

if value == "+":
    print("Result:", n1 + n2)
elif value == "-":
    print("Result:", n1 - n2)
elif value == "*":
    print("Result:", n1 * n2)
elif value == "/":
    print("Result:", n1 / n2)
else:
    print("Invalid operator")
Chapter 4

Nested if-else Statements

Nested if-else tab hoti hai jab ek if statement ke andar doosri if statement nested ho.

nested_marks.py
marks = int(input("Enter your marks: "))

if marks >= 35:
    print("You are Pass")
    if marks >= 75:
        print("Grade A")
    else:
        if marks >= 60:
            print("Grade B")
        else:
            if marks >= 50:
                print("Grade C")
            else:
                print("Grade D")
else:
    print("You are Fail")
Chapter 5

Real-World ATM & Auth Systems

1. User Login Authentication

user_login.py
username = input("enter your username: ")
password = int(input("enter your password: "))

if username == "admin":
    if password == 1234:
        print("Login Successful")
    else:
        print("Wrong Password")
else:
    print("Wrong Username")

2. Combined Logical AND Login

logical_login.py
name = input("Enter the name :")
password = input("Enter the password : ")

if name == "suryansh" and password == "1234":
    print("login")
else:
    print("invalid")

3. ATM Withdrawal System

atm_system.py
balance = 5000
pin = 1234

entered_pin = int(input("Enter your PIN: "))

if entered_pin == pin:
    withdraw = int(input("Enter amount to withdraw: "))
    if withdraw <= balance:
        balance = balance - withdraw
        print("Collect Cash")
        print("Remaining Balance:", balance)
    else:
        print("Insufficient balance")
else:
    print("Incorrect PIN")
Chapter 6

Short-Hand Ternary Operator

Ternary Operator if-else ko short single-line format me likhne ke liye use hota hai.

Syntax: value_if_true if condition else value_if_false

Example 1: Age Classification

ternary_age.py
age = 20
result = "Adult" if age >= 18 else "Minor"
print(result)

Example 2: Even or Odd Evaluation

ternary_even.py
number = 10
result = "Even" if number % 2 == 0 else "Odd"
print(result)
Chapter 7

Match-Case Statements

match-case multiple values ko clean way me check karne ke liye use hota hai (similar to switch-case). Python 3.10+ me available hai. case _ default fallback block handle karta hai.

Example 1: Day of Week Switch

match_days.py
day = 2

match day:
    case 1:
        print("Monday")
    case 2:
        print("Tuesday")
    case 3:
        print("Wednesday")
    case 4:
        print("Thursday")
    case 5:
        print("Friday")
    case 6:
        print("Saturday")
    case 7:
        print("Sunday")
    case _:
        print("Invalid day")

Example 2: Command Dispatcher

match_commands.py
command = "start"

match command:
    case "start":
        print("Starting")
    case "stop":
        print("Stopping")
    case "restart":
        print("Restarting")
    case _:
        print("Invalid command")
Chapter 8

Exercises & Summary

Student Subject Average Calculator

student_report.py
student_name = input("enter student name: ")

math_marks = int(input("enter math marks: "))
science_marks = int(input("enter science marks: "))
english_marks = int(input("enter english marks: "))

total_marks = math_marks + science_marks + english_marks
average_marks = total_marks / 3

print("Student Name:", student_name)
print("Total Marks:", total_marks)
print("Average Marks:", average_marks)
Summary Table
ConstructSyntax PatternUse Case
ifif condition:Single true condition check
if-elseif condition: ... else:Binary choice (True / False)
if-elif-elseif c1: ... elif c2: ... else:Multiple ladder conditions
Ternaryx if c else yCompact single-line expression
Match-Casematch val: case x: ... case _:Pattern matching (Python 3.10+)