If Conditions
-
Conditional statements allow your Python program to make decisions based on whether a condition is True or False. In this lesson, we focus on the
ifstatement to execute specific code only when a condition is met. You’ll learn the syntax ofif, how it controls the flow of execution, and see practical examples.
What is a Conditional Statement?
A conditional statement in Python is used to make decisions in a program.
It allows the program to execute different blocks of code based on conditions (True or False).In simple words:
“If a condition is true, do this. Otherwise, do something else.”Why Do We Use Conditional Statements?
Conditional statements are used to:
Make decisions in programs
Control the flow of execution
Perform different actions for different inputs
Build real-world logic (login systems, grading, validations)
Without conditions:
A program runs line by line with no decision-making.
With conditions:
A program becomes intelligent and dynamic.
Real-Life Examples
Types of Conditional Statements in Python
Python supports the following conditional statements:
if
if-else
if-elif-else
Nested if
Short-hand if
Short-hand if-else (Ternary Operator)
Basic Syntax of Conditional Statements
General Syntax
if condition:
statement(s)
Important Rules
Condition must return True or False
Colon : is mandatory
Indentation (spacing) is compulsory in Python
Key Points to Remember
Conditional statements control program flow
Conditions return Boolean values (True / False)
Indentation is mandatory in Python
elif avoids multiple if statements
Short-hand improves readability for simple logic
Where Conditional Statements Are Used?
Login & authentication systems
Form validation
Grading systems
Menu-driven programs
Decision-making logic in real applications
What is the if Statement in Python?
The if statement is a decision-making statement in Python.
It allows a program to execute a block of code only when a specified condition is True.If the condition evaluates to False, the code inside the if block is skipped.
Why Use the if Statement?
The if statement is used to:
Make decisions in a program
Compare values using conditions
Control the execution flow
Implement real-world logic such as eligibility, validation, and access control
Syntax of if Statement (Detailed)
Basic Syntax
if condition:
statement(s)
Syntax Explanation
Indentation in Python
Python uses indentation instead of braces {} to define blocks of code.
Rules:
Indentation is mandatory
Usually 4 spaces are used
All statements under if must have the same indentation level
Wrong indentation causes an IndentationError
Using Variables in if Condition
Variables store values that can be tested using conditions.
Checking Age Eligibility Using if Statement
This program checks whether a person is eligible to vote using an if condition and a variable.
age = 20
if age >= 18:
print("You are eligible to vote")
Explanation
age is a variable
Condition: age >= 18
If condition is True, the message is printed
Multiple Statements Inside if
An if block can contain more than one statement.
All statements must be properly indented.
Executing Multiple Statements Using if Condition
This program executes multiple lines of code when the condition becomes True.
marks = 75
if marks >= 60:
print("Result: Pass")
print("Grade: First Class")
print("Congratulations!")
Explanation
Condition: marks >= 60
Since the condition is True:
All three print statements are executed
If condition is False, none of the statements run
Using Comparison Operators in if
Common operators used in conditions:
Comparing Two Numbers Using if Statement
This program compares two numbers using variables and prints a message if the condition is True.
a = 10
b = 5
if a > b:
print("a is greater than b")
Using Boolean Variables in if
Boolean variables store True or False.
Checking Login Status Using Boolean Variable
This program checks whether the user is logged in using a Boolean variable.
is_logged_in = True
if is_logged_in:
print("Welcome! You are logged in.")
Nested Logic (Concept)
Even inside an if block, Python allows additional logic.
if condition:
statement1
statement2
All statements execute together when the condition is True.
Flow of Execution (How Python Works)
Python reads the condition
Condition is evaluated (True or False)
If True → execute indented block
If False → skip the block
Program continues