Multi Branches in Python

When writing Python code, you often need to make decisions based on multiple conditions. This is typically done using multi-branch conditional statements with if, elif, and else. These statements allow your code to execute different blocks of code based on various conditions.

Basic Structure

The basic structure of multi-branch conditional statements in Python looks like this:

if condition1:
    # Block of code to execute if condition1 is true
elif condition2:
    # Block of code to execute if condition2 is true
elif condition3:
    # Block of code to execute if condition3 is true
else:
    # Block of code to execute if none of the above conditions are true

Example

Let’s consider a practical example where we determine the grade of a student based on their score:

def determine_grade(score):
    if score >= 90:
        grade = 'A'
    elif score >= 80:
        grade = 'B'
    elif score >= 70:
        grade = 'C'
    elif score >= 60:
        grade = 'D'
    else:
        grade = 'F'
    return grade

# Example usage
score = 85
print(f"The grade for a score of {score} is {determine_grade(score)}")

Explanation

  1. Function Definition:
    • We define a function determine_grade that takes a parameter score.
  2. First Condition (if score >= 90):
    • If the score is 90 or higher, the grade is set to ‘A’.
  3. Second Condition (elif score >= 80):
    • If the first condition is false, the program checks if the score is 80 or higher. If true, the grade is set to ‘B’.
  4. Third Condition (elif score >= 70):
    • If the second condition is also false, the program checks if the score is 70 or higher. If true, the grade is set to ‘C’.
  5. Fourth Condition (elif score >= 60):
    • If the third condition is false, the program checks if the score is 60 or higher. If true, the grade is set to ‘D’.
  6. Default Case (else):
    • If none of the above conditions are true, the grade is set to ‘F’.
  7. Return Statement:
    • The function returns the determined grade.

Another Example: Weather Advice

Here’s another example that provides advice based on the weather conditions:

def weather_advice(weather):
    if weather == 'sunny':
        advice = "It's a sunny day! Wear sunglasses and apply sunscreen."
    elif weather == 'rainy':
        advice = "It's raining outside. Don't forget your umbrella."
    elif weather == 'snowy':
        advice = "It's snowy and cold. Wear a warm coat and boots."
    elif weather == 'windy':
        advice = "It's windy. A light jacket should be enough."
    else:
        advice = "Weather is unpredictable. Be prepared for anything."
    return advice

# Example usage
current_weather = 'rainy'
print(weather_advice(current_weather))

Explanation

  1. Function Definition:
    • We define a function weather_advice that takes a parameter weather.
  2. First Condition (if weather == 'sunny'):
    • If the weather is sunny, the advice is to wear sunglasses and apply sunscreen.
  3. Second Condition (elif weather == 'rainy'):
    • If the first condition is false and the weather is rainy, the advice is to take an umbrella.
  4. Third Condition (elif weather == 'snowy'):
    • If the second condition is false and the weather is snowy, the advice is to wear a warm coat and boots.
  5. Fourth Condition (elif weather == 'windy'):
    • If the third condition is false and the weather is windy, the advice is to wear a light jacket.
  6. Default Case (else):
    • If none of the above conditions are true, the advice is to be prepared for any weather.
  7. Return Statement:
    • The function returns the weather advice.