Q Conditions
Q Conditions
Conditions
If statement
If-else statement
if-elif-else statement
1. Conditional Statements:
Conditional statements, often known as control structures, are an essential component of programming.
They enable you to make code decisions based on predefined conditions
The primary conditional statements in Python are if, elif (short for "else if"), and else.
The purpose of conditional statements in programming is to enable the execution of different code blocks
based on specific conditions. Here are four key purposes of conditional statements:
1. Decision Making: Conditional statements allow a program to make decisions by evaluating conditions. They
help determine which set of instructions to execute depending on whether the conditions are met or not.
2. Control Flow: Conditional statements control the flow of a program, ensuring that the code follows a specific
path or branches based on the given conditions. This helps create responsive and adaptable programs.
3. Error Handling: Conditional statements are used for error handling. They can identify and respond to
exceptional situations, preventing program crashes or unexpected behavior by executing specific error-
handling code.
2. ‘if’ statements:
The if statement is the most basic type of control statement. It accepts a condition and determines
whether it is True or False
If the condition is True, the True piece of code is run; otherwise, the block of code is bypassed, and the
controller proceeds to the next line.
Examples:
x = 10
if x > 5:
temperature = 30
if is_datascience_course:
age = 18
else:
score = 75
passing_score = 70
else:
else:
3. ‘if-else’ statements:
The if-else statement checks the condition and executes the if block of code when the condition is True,
and if the condition is False, it will execute the else block of code.
x = 10
if x > 5:
else:
age = 20
else:
is_datascience_course = True
if is_datascience_course:
else:
num = 7
if num % 2 == 0:
print("Even")
else:
print("Odd")
score = 85
print(f"You {result}.")
4. ‘if-elif-else’ statements:
The if-elif-else condition statement in Python uses elif blocks to link multiple conditions one after the other.
This is useful when you need to check numerous conditions at the same time
We can make a difficult decision with the help of if-elif-else. The elif statement checks each condition one
by one and executes the code if the condition is met.
x = 10
if x > 5:
elif x == 5:
score = 75
print("A")
print("B")
print("C")
hour = 14
print("Good morning")
print("Good afternoon")
else:
print("Good evening")
num = 0
if num > 0:
print("Positive")
print("Negative")
else:
print("Zero")
age = 30
else:
This enables you to design a condition hierarchy in which the inner ‘if-else’ statements are evaluated only
if the outer condition is true.
When you need to test many circumstances in an organized manner, nested if-else statements are useful.
Examples:
x = 10
y = 5
if x > 5:
if y > 5:
else:
else:
is_weekend = False
is_sunny = True
if is_weekend:
if is_sunny:
else:
else:
print("It's a workday.")
is_student = True
is_teacher = False
if is_student:
if is_teacher:
else:
else:
if is_teacher:
print("You are a teacher but not a student.")
else:
is_vip = True
age = 30
if is_vip:
else:
else:
else:
In all these analogies, the `if` statements determine the course of action based on a single condition.
The `if-elif-else` statements help you make choices from a range of options, and the `if-else` statements
provide a simple binary choice.