Introduction
Conditional statements are used to make decisions in a program. They help a program execute different blocks of code depending on whether a condition is True or False.
What Are Conditional Statements in Python?
Conditional statements are used to execute a block of code based on a particular condition.
For example, if a student's mark is greater than or equal to 35, the student passes. Otherwise, the student fails.
Python uses keywords like if, elif, and else to make decisions.
Basic Syntax of Conditional Statements
The basic syntax of an if statement is:
if condition:
statements
elif condition:
statements
else:
statements
Explanation:
-
if: Checks the first condition. -
elif: Checks another condition if the previous condition is False. -
else: Executes when all the above conditions are False. - Indentation: Defines the block of code that belongs to each condition.
Types of Conditional Statements in Python
Python mainly provides four types of conditional statements.
1. if Statement
The if statement executes a block of code only when the given condition is True.
Example:
age = 20
if age >= 18:
print("You are eligible to vote")
Output:
You are eligible to vote
Here, the condition age >= 18 is True, so the message is printed.
2. if-else Statement
The if-else statement is used when we have two possible outcomes.
- If the condition is True, the if block executes.
- If the condition is False, the else block executes.
Example:
age = 16
if age >= 18:
print("You are eligible to vote")
else:
print("You are not eligible to vote")
Output:
You are not eligible to vote
Here, the condition is False, so the else block is executed.
3. if-elif-else Statement
The if-elif-else statement is used when we need to check multiple conditions.
Python checks the conditions one by one. The first condition that becomes True will be executed.
Example:
mark = 85
if mark >= 90:
print("Grade A")
elif mark >= 75:
print("Grade B")
elif mark >= 50:
print("Grade C")
else:
print("Fail")
Output:
Grade B
Here, the mark is 85, so the second condition is True and Grade B is printed.
4. Nested if Statement
A nested if statement means writing an if statement inside another if statement.
It is useful when we need to check one condition inside another condition.
Example:
age = 20
has_id = True
if age >= 18:
if has_id:
print("You can enter")
else:
print("ID is required")
else:
print("You are not eligible")
Output:
You can enter
Here, the first condition checks the age. If the age is 18 or above, the second condition checks whether the person has an ID.
Real-Life Example of Conditional Statements
Let's understand conditional statements with a simple example of checking whether a number is positive, negative, or zero.
num = 10
if num > 0:
print("Positive number")
elif num < 0:
print("Negative number")
else:
print("Zero")
Output:
Positive number
This program checks the number and displays the appropriate result.
Why Are Conditional Statements Important?
Conditional statements are important because:
- They help programs make decisions.
- They allow different actions based on conditions.
- They make programs more interactive.
- They help solve real-world problems.
- They control the flow of program execution.
Top comments (0)