Write simple Python program to demonstrate use of
conditional statements :
i) if
ii) if…else
iii) Nested if
iv) Switch case
i) if
Statement
Problem Statement: Determine if a person is eligible to vote based on their age.
age = 20
if age >= 18:
print("You are eligible to vote.")
Output
You are eligible to vote.
Explanation: The if
statement checks if the person’s age is 18 or older. If the condition is true, it prints that the person is eligible to vote.
ii) if…else
Statement
Problem Statement: Determine whether a number is even or odd.
number = 15
if number % 2 == 0:
print("The number is even.")
else:
print("The number is odd.")
OUTPUT
The number is odd.
Explanation: The if
statement checks if the number is divisible by 2 (i.e., even). If the condition is true, it prints that the number is even; otherwise, it prints that the number is odd.
iii) Nested if
Statement
Problem Statement: Determine the category of a person based on their age.
age = 25
if age >= 18:
if age >= 60:
print("You are a senior citizen.")
else:
print("You are an adult.")
else:
print("You are a minor.")
OUTPUT:
You are an adult.
Explanation: The outer if
checks if the person is 18 or older. If true, a nested if
checks if the person is 60 or older, categorizing them as a senior citizen; otherwise, they are categorized as an adult. If the outer if
condition is false, the person is categorized as a minor.
iv) Simulating a Switch Case
(Using if…elif…else
)
Problem Statement: Simple calculator that performs basic arithmetic operations based on user input.
operation = 'multiply'
a = 5
b = 3
if operation == 'add':
result = a + b
print(f"The result of addition is: {result}")
elif operation == 'subtract':
result = a - b
print(f"The result of subtraction is: {result}")
elif operation == 'multiply':
result = a * b
print(f"The result of multiplication is: {result}")
elif operation == 'divide':
if b != 0:
result = a / b
print(f"The result of division is: {result}")
else:
print("Division by zero is not allowed.")
else:
print("Invalid operation.")
OUTPUT:
The result of multiplication is: 15
Explanation: Python does not have a built-in switch case
statement like some other languages. However, you can simulate it using if…elif…else
. This code snippet performs different arithmetic operations based on the value of the operation
variable.
NOTE:
An f-string, or formatted string literal, is a way to embed expressions inside string literals, using curly braces {}
. By prefixing the string with an f
or F
, you can include variables or expressions directly inside the string, and Python will evaluate them and insert their values.
result = 15
print(f"The result of multiplication is: {result}")
f-string: The string is prefixed with f
, which tells Python to interpret the string as an f-string.
Curly Braces {}
: Inside the string, {result}
is a placeholder where the value of the variable result
will be inserted.
Evaluation: Python evaluates the expression inside the curly braces and replaces {result}
with the actual value of result
(in this case, 15
).
Output: The resulting string that gets printed will be "The result of multiplication is: 15"
Summary
if
Statement: Used for a single condition.if…else
Statement: Used when you need to handle two conditions, one being the opposite of the other.- Nested
if
: Used for more complex, hierarchical conditions. Switch Case
Simulation: Python usesif…elif…else
as a substitute for the traditional switch-case structure found in other languages.