🧠 Python Conditional Statements 

 

Overview

Conditional statements in Python are used to control the flow of a program by executing different blocks of code based on specific conditions.
They help your program “make decisions” — just like humans do.

For example:

age = 18
if age >= 18:
   print("Eligible to vote")

 

 

Uses of Conditional Statements

Python’s conditional statements are widely used for:
a. Decision Making — To execute certain code only if a condition is met.
b. Validation — To verify user inputs like login credentials.
c. Program Control — To control loops or terminate code early based on conditions.
d. Dynamic Output — To produce different results based on inputs or data.

 

Types of Conditional Statements

A. if Statement

The if statement is used to execute a block of code only if the given condition is True.

Conditional Statements in Python - GeeksforGeeks

Syntax:

if condition:
   statement(s)

 

Example:

num = 10
if num > 0:
   print("Positive number")
 

 

B. if...else Statement

This statement executes one block if the condition is True, and another block if it is False.

Syntax:

if condition:
   statement(s) when True
else:
   statement(s) when False
 

Example:

age = int(input("Enter your age: "))
if age >= 18:
   print("You are an adult.")
else:
   print("You are a minor.")
 

C. if...elif...else Ladder

When you have multiple conditions, use elif (short for else if).

Syntax:

if condition1:
   statement1
elif condition2:
   statement2
elif condition3:
   statement3
else:
   statementN
 

Example:

marks = int(input("Enter your marks: "))

if marks >= 90:
   print("Grade A")
elif marks >= 75:
   print("Grade B")
elif marks >= 50:
   print("Grade C")
else:
   print("Fail")
 

D. Nested if Statement

An if statement can exist inside another if statement.
This is useful when multiple layers of conditions are needed.

Example:

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

if num >= 0:
   if num == 0:
       print("Number is Zero")
   else:
       print("Number is Positive")
else:
   print("Number is Negative")
 

 

Comparison & Logical Conditions

You can combine multiple conditions using logical operators.

OperatorMeaningExample
andTrue if both conditions are trueage > 18 and city == "Delhi"
orTrue if at least one is truemarks > 90 or grade == "A"
notReverses the conditionnot(is_raining)

Example:

age = 25
city = "Dehradun"
if age > 18 and city == "Dehradun":
   print("Eligible for registration")
 

 

Short-Hand if Statements

For simple one-line conditions, Python allows a short-hand syntax.

Example:

x = 5
if x > 0: print("Positive")
 

Short-hand if...else:

x = -3
print("Positive") if x > 0 else print("Negative")
 

 

Practical Examples

🧮 Example 1: Even or Odd

num = int(input("Enter a number: "))
if num % 2 == 0:
   print("Even number")
else:
   print("Odd number")
 

💰 Example 2: Simple Tax Calculator

income = float(input("Enter your income: "))

if income <= 250000:
   print("No tax")
elif income <= 500000:
   print("5% tax")
elif income <= 1000000:
   print("20% tax")
else:
   print("30% tax")
 

🧑‍🎓 Example 3: Student Result

marks = int(input("Enter marks: "))

if marks >= 90:
   print("Excellent")
elif marks >= 75:
   print("Very Good")
elif marks >= 50:
   print("Pass")
else:
   print("Fail")
 

Practice Tasks

Positive-Negative-Zero Checker
→ Take an integer input and print whether it’s positive, negative, or zero.

Leap Year Checker
→ Input a year and check if it’s a leap year using nested if.

Largest of Three Numbers
→ Take three numbers and print which one is the largest.

Login System (Basic)
→ Input username & password, and check if they match stored values.