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).
Control flow decision-making relies on comparison operators (==, !=, >, <, >=, <=) and logical operators (and, or, not).
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
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
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 %
number = int(input("Enter a number: "))
if number % 2 == 0:
print("The number is even")
else:
print("The number is Odd")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
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 = 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
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")Nested if-else Statements
Nested if-else tab hoti hai jab ek if statement ke andar doosri if statement nested ho.
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")Real-World ATM & Auth Systems
1. User Login Authentication
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
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
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")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
age = 20 result = "Adult" if age >= 18 else "Minor" print(result)
Example 2: Even or Odd Evaluation
number = 10 result = "Even" if number % 2 == 0 else "Odd" print(result)
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
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
command = "start"
match command:
case "start":
print("Starting")
case "stop":
print("Stopping")
case "restart":
print("Restarting")
case _:
print("Invalid command")Exercises & Summary
Student Subject Average Calculator
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)| Construct | Syntax Pattern | Use Case |
|---|---|---|
if | if condition: | Single true condition check |
if-else | if condition: ... else: | Binary choice (True / False) |
if-elif-else | if c1: ... elif c2: ... else: | Multiple ladder conditions |
| Ternary | x if c else y | Compact single-line expression |
| Match-Case | match val: case x: ... case _: | Pattern matching (Python 3.10+) |