Python - Conditional Statements

5.
How do you use the elif statement in Python?

The elif statement (short for "else if") is used in Python to check multiple conditions in a series. It comes after an if statement and before an optional else statement. Let's look at an example:

# Example
score = 75

if score >= 90:
    print("Excellent!")
elif 80 <= score < 90:
    print("Very Good.")
elif 70 <= score < 80:
    print("Good.")
elif 60 <= score < 70:
    print("Fair.")
else:
    print("Needs Improvement.")
Good.

In this example, the elif statements allow checking multiple conditions sequentially. If the first condition is not true, it moves to the next one. The last else statement is optional and serves as a catch-all for cases that don't match any previous condition.


6.
Explain the concept of nested if statements in Python.

In Python, nested if statements are used to handle more complex conditional logic. A nested if statement is an if statement inside another if statement. Each if statement is executed based on the condition of the outer if statement being true. Let's look at an example:

# Example
score = 75

if score >= 60:
    print("Passing grade.")
    if score >= 90:
        print("Excellent!")
    elif score >= 80:
        print("Very Good.")
    elif score >= 70:
        print("Good.")
    else:
        print("Fair.")
else:
    print("Failing grade.")
Passing grade.
Good.

In this example, the outer if statement checks if the score is greater than or equal to 60. If true, it enters the block and prints "Passing grade." Then, there is another if statement nested inside, checking for specific grade ranges. This allows for more fine-grained evaluation based on different conditions.


7.
What is the ternary conditional expression, and how is it used in Python?

In Python, the ternary conditional expression is a concise way to write a simple if-else statement. It is also known as the conditional expression or ternary operator. The syntax is:

x if condition else y

Here, x is the value to be returned if the condition is true, and y is the value to be returned if the condition is false.

Let's look at an example:

# Example
age = 20
message = "Teenager" if age >= 13 and age <= 19 else "Not a teenager"
print(message)
Teenager

In this example, if the age is between 13 and 19 (inclusive), the value "Teenager" is assigned to the message variable; otherwise, "Not a teenager" is assigned.


8.
How can you use the pass statement in an if block in Python?

In Python, the pass statement is a no-operation statement. It serves as a placeholder where syntactically some code is required but where no action is desired or necessary.

When using the pass statement within an if block, it allows you to create a valid code structure without any executable statements. This can be useful when you're working on building the structure of your program and want to leave some parts incomplete.

Let's see an example:

# Example
x = 10

if x > 5:
    print("x is greater than 5")
else:
    pass
x is greater than 5

In this example, the if block checks if x is greater than 5, and if true, it prints a message. The else block contains the pass statement, which does nothing. The program runs successfully without any errors.