For example, a program can check whether a person is eligible to vote, determine whether a student has passed an exam, or identify whether a number is positive or negative. Python uses conditional statements such as if, else, and elif to perform these tasks.
In this tutorial, you will learn what conditional statements in Python are, how they work, and how to use different types of conditional statements with simple Python examples.
What Are Conditional Statements in Python?
Conditional statements are used to make decisions in a Python program. They allow a program to execute different blocks of code depending on whether a condition is True or False.
For example, you can create a program that checks whether a person is eligible to vote based on their age.
Python provides the following conditional statements:
- if statement
- if-else statement
- if-elif-else statement
- Nested if statement
Let's understand each one with examples.
1. if Statement in Python
The if statement executes a block of code only when the given condition is True. If the condition is false, Python skips that block of code.
Syntax:
if condition:
statements
Example:
age = 20
if age >= 18:
print("You are eligible to vote")Output:
You are eligible to vote
Since the age is 20, which is greater than or equal to 18, the condition is True and the message is displayed.
2. if-else Statement in Python
The if-else statement is used when you want to execute one block of code if the condition is True and another block if it is False.
Syntax:
if condition:
statements
else:
statementsExample:
age = 16
if age >= 18:
print("Eligible to vote")
else:
print("Not eligible to vote")Output:
Not eligible to vote
Here, the age is 16, which is less than 18. Therefore, the condition is False, and Python executes the else block.
3. if-elif-else Statement in Python
The elif keyword means "else if." It is used to check multiple conditions in a Python program.
When the first condition is False, Python checks the next condition. This process continues until a condition becomes True or the else block is executed.
Syntax:
if condition1:
statements
elif condition2:
statements
else:
statementsExample:
marks = 75
if marks >= 90:
print("Grade A")
elif marks >= 75:
print("Grade B")
elif marks >= 50:
print("Grade C")
else:
print("Fail")Output:
Grade B
Here, Python checks each condition until it finds one that is True. Since the marks are 75, the second condition is satisfied, and the program displays Grade B.
4. Nested if Statement in Python
A nested if statement means writing an if statement inside another if statement. It is useful when you need to check multiple related conditions.
Example:
age = 25
has_id = True
if age >= 18:
if has_id:
print("Entry allowed")
else:
print("ID required")
else:
print("Entry not allowed")Output:
Entry allowed
In this example, Python first checks whether the age is 18 or above. If the condition is true, it checks whether the person has a valid ID. Since both conditions are true, the program displays "Entry allowed".
5. Conditional Statements with Logical Operators
You can combine multiple conditions using logical operators such as and, or, and not in Python.
Example Using the and Operator
The and operator returns True only when both conditions are true.
age = 25
if age >= 18 and age <= 60:
print("Eligible")Output:
Eligible
In this example, both conditions must be true for the statement inside the if block to execute.
Example Using the or Operator
The or operator returns True when at least one of the conditions is true.
day = "Sunday"
if day == "Saturday" or day == "Sunday":
print("Weekend")Output:
Weekend
Here, the condition is true because the day is Sunday. Only one condition needs to be true for the or operator to execute the if block.
Example Using the not Operator
The not operator reverses the result of a condition.
is_raining = False
if not is_raining:
print("You can go outside")Output:
You can go outside
6. Short-Hand if Statement in Python
Python allows you to write a simple if statement on a single line. This is called a short-hand if statement. It is useful when the condition and the statement are simple.
Example:
age = 20
if age >= 18: print("Adult")Output:
Adult
7. Conditional Expression (Ternary Operator) in Python
Python also supports conditional expressions, which allow you to write simple if-else logic in a single line. They are commonly known as the ternary operator.
Syntax:
value_if_true if condition else value_if_false
Example:
age = 20
result = "Adult" if age >= 18 else "Minor"
print(result)Output:
Adult
In this example, Python checks whether the age is greater than or equal to 18. If the condition is true, the result becomes "Adult". Otherwise, it becomes "Minor".
Importance of Indentation in Python Conditional Statements
Indentation is very important in Python because it defines which statements belong to a particular conditional block. Unlike some other programming languages, Python uses indentation instead of curly braces to group statements.
Example:
age = 20
if age >= 18:
print("Adult")
print("Eligible")Output:
Adult
Eligible
Both print statements are indented, so they belong to the if block. Incorrect indentation can result in an IndentationError or unexpected program behavior.
Conclusion
Conditional statements are an essential part of Python programming because they help programs make decisions based on different situations. By using if, if-else, if-elif-else, and nested if statements, you can control how your program executes different instructions.
You can also combine conditions using logical operators such as and, or, and not, and simplify your code with short-hand if statements and conditional expressions.
Understanding conditional statements in Python will help you build more interactive, flexible, and intelligent programs. Practice these examples to strengthen your Python programming skills.
