Python Conditional Statements: Complete Beginner's Guide (Chapter 5)
Vishal Yadav | Course Instructor
Imagine a website deciding whether to allow a user to log in, an ATM checking if your account has sufficient balance, or an e-commerce platform applying a discount based on your purchase amount. All of these systems rely on one fundamental programming concept: decision-making.
In Python, decision-making is achieved through conditional statements. Conditional statements allow a program to evaluate conditions and execute different blocks of code depending on whether those conditions are true or false.
In this chapter, you will learn how to use if, if-else, if-elif-else, and nested conditions. You will also explore practical, real-world examples that demonstrate how conditional logic powers modern applications.
Key Takeaway: Conditional statements enable programs to make intelligent decisions based on data and user input.
What Are Conditional Statements?
Conditional statements are used to execute specific code blocks only when certain conditions are met.
A condition is an expression that evaluates to either True or False.
For example:
age = 20
print(age >= 18)
Output:
True
Since the condition is true, a program can take a specific action.
The if Statement
The if statement is the simplest form of conditional statement. It executes a block of code only when a condition evaluates to True.
Syntax
if condition:
# code to execute
Basic Example
age = 20
if age >= 18:
print("You are eligible to vote.")
Output:
You are eligible to vote.
Example with User Input
marks = int(input("Enter your marks: "))
if marks >= 40:
print("You passed the exam.")
Example Output:
Enter your marks: 65
You passed the exam.
Understanding Indentation
Python uses indentation (spaces) to define code blocks.
age = 20
if age >= 18:
print("Adult")
print("Eligible")
Both print statements belong to the if block because they are indented.
Important: Proper indentation is mandatory in Python and replaces the need for curly braces used in other programming languages.
The if-else Statement
The if-else statement provides an alternative path when a condition evaluates to False.
Syntax
if condition:
# code if true
else:
# code if false
Basic Example
age = 16
if age >= 18:
print("You can vote.")
else:
print("You cannot vote yet.")
Output:
You cannot vote yet.
Even or Odd Number Checker
number = int(input("Enter a number: "))
if number % 2 == 0:
print("Even Number")
else:
print("Odd Number")
Example Output:
Enter a number: 12
Even Number
Password Validation Example
password = input("Enter password: ")
if password == "python123":
print("Access Granted")
else:
print("Access Denied")
Example Output:
Access Granted
The program executes only one block depending on the condition result.
The if-elif-else Statement
When multiple conditions need to be checked, Python provides the if-elif-else structure.
Syntax
if condition1:
# code block
elif condition2:
# code block
else:
# code block
The program evaluates conditions from top to bottom and executes the first matching block.
Grade Calculator Example
marks = int(input("Enter marks: "))
if marks >= 90:
print("Grade A")
elif marks >= 75:
print("Grade B")
elif marks >= 60:
print("Grade C")
elif marks >= 40:
print("Grade D")
else:
print("Fail")
Example Output:
Enter marks: 82
Grade B
Traffic Light Example
signal = input("Enter signal color: ")
if signal == "red":
print("Stop")
elif signal == "yellow":
print("Get Ready")
elif signal == "green":
print("Go")
else:
print("Invalid Signal")
Example Output:
Enter signal color: green
Go
Best Practice: Arrange conditions from most specific to most general for accurate results.
Nested Conditions
A nested condition is an if statement placed inside another if statement.
Nested conditions are useful when decisions depend on multiple levels of criteria.
Syntax
if condition1:
if condition2:
# code block
Basic Nested Condition Example
age = 25
has_license = True
if age >= 18:
if has_license:
print("You can drive.")
Output:
You can drive.
Online Exam Eligibility System
registered = True
fee_paid = True
if registered:
if fee_paid:
print("Eligible for Exam")
else:
print("Pay Exam Fee")
else:
print("Registration Required")
Output:
Eligible for Exam
Bank Loan Approval Example
salary = 50000
credit_score = 750
if salary >= 40000:
if credit_score >= 700:
print("Loan Approved")
else:
print("Credit Score Too Low")
else:
print("Insufficient Salary")
Output:
Loan Approved
Using Logical Operators in Conditions
Conditional statements often work together with logical operators.
AND Operator Example
age = 20
has_id = True
if age >= 18 and has_id:
print("Entry Allowed")
Output:
Entry Allowed
OR Operator Example
is_admin = False
is_manager = True
if is_admin or is_manager:
print("Access Granted")
Output:
Access Granted
NOT Operator Example
blocked = False
if not blocked:
print("Login Successful")
Output:
Login Successful
Real-World Decision Making Examples
Conditional statements power countless applications in the real world.
Example 1: ATM Withdrawal System
balance = 10000
withdraw_amount = 3000
if withdraw_amount <= balance:
print("Transaction Successful")
else:
print("Insufficient Balance")
Output:
Transaction Successful
Example 2: E-Commerce Discount Calculator
purchase_amount = 6000
if purchase_amount >= 5000:
print("20% Discount Applied")
else:
print("No Discount")
Output:
20% Discount Applied
Example 3: Movie Ticket Eligibility
age = 15
if age >= 18:
print("Adult Ticket")
else:
print("Child Ticket")
Output:
Child Ticket
Example 4: Employee Bonus Calculator
years_of_service = 6
if years_of_service >= 5:
print("Bonus Eligible")
else:
print("Bonus Not Eligible")
Output:
Bonus Eligible
Mini Project: Smart Login System
This project combines multiple conditional concepts into a practical application.
username = input("Enter username: ")
password = input("Enter password: ")
if username == "admin":
if password == "python123":
print("Login Successful")
else:
print("Incorrect Password")
else:
print("Invalid Username")
Example Output:
Enter username: admin
Enter password: python123
Login Successful
Common Beginner Mistakes
Using = Instead of ==
Incorrect:
if age = 18:
Correct:
if age == 18:
Incorrect Indentation
Incorrect:
if age >= 18:
print("Adult")
Correct:
if age >= 18:
print("Adult")
Overusing Nested Conditions
Too many nested conditions can make code difficult to read. Whenever possible, use logical operators to simplify conditions.
Chapter Summary
Conditional statements are one of the most powerful features in Python. They allow programs to analyze information, evaluate conditions, and make intelligent decisions.
- if executes code when a condition is true.
- if-else provides an alternative path when a condition is false.
- if-elif-else handles multiple conditions efficiently.
- Nested conditions allow multi-level decision-making.
- Real-world applications such as login systems, banking software, and e-commerce platforms heavily rely on conditional logic.
Next Chapter: Loops in Python – Learn how to automate repetitive tasks using for loops, while loops, break, continue, and nested loops.
Vishal Yadav
A specialist dedicated to publishing high-quality, readable insights on technology, leadership, and digital growth.