
👉Tutorial-2: Python For & While Loops
👉Tutorial-3: Python Break, Continue, and Pass Statements
👉Tutorial-4: Python OOPs Concepts
👉Tutorial-5: Polymorphism in Python with Examples
👉Tutorial-6: Mutable vs Immutable Objects in Python
👉Python Conditional Statements: IF…Else, ELIF & Switch Case
👉What are Conditional Statements in Python?
Python Conditional Statements: Conditional statements in Python are used to execute different code blocks based on whether a Boolean condition evaluates to True or False. These statements allow developers to create decision-making logic in Python programs.
Python IF Statement
Python Conditional Statements
An if statement is used to execute a block of code only if the specified condition is True. If the condition is False, the block is skipped.
Python Conditional Statements IF Syntax:
python
if condition:
# Code block for true condition
Python Conditional Statements IF Example:
python
def main():
x, y = 2, 8
if x < y:
st = “x is less than y”
print(st)
if __name__ == “__main__”:
main()
👉Explanation:
Python Conditional Statements
- x and y are initialized as 2 and 8 respectively.
- The if condition (x < y) is true, so the message “x is less than y” is printed.
👉Python IF…Else Statement
The else block is used to execute code when the if condition evaluates to False.
👉Syntax:
python
if condition:
# Code block for true condition
else:
# Code block for false condition
👉Example:
python
def main():
x, y = 8, 4
if x < y:
st = “x is less than y”
else:
st = “x is greater than y”
print(st)
if __name__ == “__main__”:
main()
👉Explanation:
- Since x < y is False, the else block executes, printing “x is greater than y”.
👉Python ELIF Statement
The elif condition is used when there are multiple possible conditions.
👉Syntax:
python
if condition1:
# Code block for condition1
elif condition2:
# Code block for condition2
else:
# Code block for all other conditions
👉Example:
python
def main():
x, y = 8, 8
if x < y:
st = “x is less than y”
elif x == y:
st = “x is same as y”
else:
st = “x is greater than y”
print(st)
if __name__ == “__main__”:
main()
👉Explanation:
- The first condition (x < y) is False.
- The elif condition (x == y) is True, so the message “x is same as y” is printed.
👉Python Inline Conditional Statement
Python also allows writing conditional statements in a concise way using inline syntax.
👉Syntax:
python
variable = value_if_true if condition else value_if_false
👉Example:
python
def main():
x, y = 10, 8
st = “x is less than y” if x < y else “x is greater than or equal to y”
print(st)
if __name__ == “__main__”:
main()
👉Explanation:
- Since x > y, the output is “x is greater than or equal to y”.
👉Python Nested IF Statement
A nested if statement is an if statement inside another if statement.
👉Example:
python
total = 100
country = “AU”
if country == “US”:
if total <= 50:
print(“Shipping Cost is $50”)
elif total <= 100:
print(“Shipping Cost is $25”)
elif total <= 150:
print(“Shipping Cost is $5”)
else:
print(“FREE”)
elif country == “AU”:
if total <= 50:
print(“Shipping Cost is $100”)
else:
print(“FREE”)
👉Explanation:
- Based on the country value, the appropriate shipping cost is calculated.
👉Switch Case in Python
Python does not have a direct switch statement, but similar functionality can be achieved using dictionaries.
👉Example:
python
def SwitchExample(argument):
switcher = {
0: “This is Case Zero”,
1: “This is Case One”,
2: “This is Case Two”
}
return switcher.get(argument, “nothing”)
if __name__ == “__main__”:
argument = 1
print(SwitchExample(argument))
👉Explanation:
- The switcher.get() method efficiently mimics a switch-case structure in Python.
👉Summary
- Python conditional statements allow you to control program flow using if, else, and elif.
- The else block handles conditions that fail.
- The elif block is used for additional conditions.
- Python’s concise inline syntax makes conditions easier to manage.
- Nested if statements help in multi-level decision-making.
- While Python lacks a direct switch statement, dictionary mapping effectively achieves similar functionality.