In Python, conditional statements are used to make decisions in the code based on certain conditions. They allow us to execute different blocks of code depending on whether a specific condition evaluates to True or False.
Types of Conditional Statements in Python
Python has following conditional statements:
if-else statement
If the condition in the if
statement evaluates to true, the code inside the if
block will be executed; otherwise, the code within the else
block will be executed.
Syntax:
if condition:
# Code to execute if condition is True
else:
# Code to execute if condition is False
Example:
num = int(input("Enter a number: "))
if num < 0:
print("Number is negative.")
else:
print("Number is positive.")
elif statement
The elif
statement (short for "else if") is used when there are multiple conditions to evaluate. It is placed between if
and else
.
Syntax:
if condition1:
# Code to execute if condition1 is True
elif condition2:
# Code to execute if condition2 is True
else:
# Code to execute if all above conditions are False
Example:
num = int(input("Enter a number: "))
if num < 0:
print("Number is negative.")
elif num == 0:
print("Number is zero.")
else:
print("Number is positive.")
Nested if statements
if
statements can be nested within other if
, elif
, or else
blocks to evaluate additional conditions.
Syntax:
if condition1:
# Code to execute if condition1 is True
if condition2:
# Code to execute if condition2 is True
elif condition3:
# Code to execute if condition3 is True
else:
# Code to execute if all inner conditions are False
else:
# Code to execute if condition1 is False
Example:
num = int(input("Enter a number: "))
if num < 0:
print("Number is negative.")
elif num > 0:
if num <= 50:
print("Number is between 1 and 50.")
elif num > 50 and num <= 70:
print("Number is between 51 and 70.")
else:
print("Number is greater than 70.")
else:
print("Number is zero.")
Key Points About Conditional Statements
- Indentation Matters:
- Python uses indentation (spaces or tabs) to show which code belongs to a condition.
- Make sure all lines in a block have the same indentation.
- Only One
else
:- You can use many
if
andelif
statements, but only oneelse
is allowed.
- You can use many
- Comparison and Logical Operators:
- Conditions use operators like:
==
(equal),!=
(not equal),<
(less than),>
(greater than).and
,or
,not
to combine conditions.
- Conditions use operators like:
- Order of Checks:
- Python checks conditions one by one. If a condition is true, the following conditions are skipped.
Shorthand If-Else Statements
You can use conditional expressions (shorthand if-else
) to simplify simple conditions into one line.
Syntax:
result = value_if_true if condition else value_if_false
Example:
a = 10
b = 20
print("A is greater") if a > b else print("B is greater")
Output:
B is greater
Multiple Conditions in One Line
You can nest multiple conditions in a single line.
Example:
a = 330
b = 330
print("A") if a > b else print("=") if a == b else print("B")
Output:
=
Comparison with Standard if-else
The shorthand is equivalent to:
if condition:
result = value_if_true
else:
result = value_if_false
Example:
x = 15
y = 10
# Shorthand
result = "X is greater" if x > y else "Y is greater"
# Equivalent Full Form
if x > y:
result = "X is greater"
else:
result = "Y is greater"
print(result)
When to Use Shorthand If-Else
- Use it when:
- The condition and expressions are simple.
- You’re assigning a value or printing a message.
- Avoid it when:
- The logic is complex or spans multiple lines.
👉 Next tutorial: Python Match Case Statements