Table of contents

if, else, elif : A Comprehensive Guide

Conditional statements are a fundamental aspect of programming. They allow you to execute certain pieces of code based on specific conditions, enabling your programs to make decisions and respond dynamically to different inputs. This comprehensive guide will cover the basics and advanced uses of if, else, and elif statements in Python.

The if statement is used to test a condition. If the condition evaluates to True, the block of code following the if statement is executed.

if condition: # code to execute if condition is true

Syntax

if condition: # code to execute if condition is true

Example

age = 18 if age >= 18: print("You are an adult.")

In this example, the message “You are an adult.” is printed because the condition age >= 18 is True.

The else statement follows an if statement and is executed when the if condition is False.

Syntax

if condition: # code to execute if condition is true else: # code to execute if condition is false

Example

age = 16 if age >= 18: print("You are an adult.") else: print("You are a minor.")

In this example, the message “You are a minor.” is printed because the condition age >= 18 is False.

The elif (short for “else if”) statement allows you to check multiple conditions. It must follow an if statement and precede an else statement (if present).

if condition1: # code to execute if condition1 is true elif condition2: # code to execute if condition2 is true else: # code to execute if both condition1 and condition2 are false

Syntax

if condition1: # code to execute if condition1 is true elif condition2: # code to execute if condition2 is true else: # code to execute if both condition1 and condition2 are false

Example

age = 16 if age >= 18: print("You are an adult.") elif age >= 13: print("You are a teenager.") else: print("You are a child.")

In this example, the message “You are a teenager.” is printed because the condition age >= 13 is True.

You can nest conditional statements inside other conditional statements to create more complex decision-making structures.

Example

age = 20 if age >= 18: if age >= 21: print("You can drink alcohol.") else: print("You are an adult, but you cannot drink alcohol.") else: print("You are a minor.")

In this example, the message “You are an adult, but you cannot drink alcohol.” is printed because age >= 18 is True but age >= 21 is False.

Python supports conditional expressions, also known as the ternary operator, which allow you to write compact if-else statements.

Syntax

value_if_true if condition else value_if_false

Example

age = 16 status = "adult" if age >= 18 else "minor" print(f"You are a(n) {status}.")

In this example, the message “You are a minor.” is printed because the condition age >= 18 is False.

A program that assigns grades based on a score.

score = int(input("Enter your score: ")) if score >= 90: grade = 'A' elif score >= 80: grade = 'B' elif score >= 70: grade = 'C' elif score >= 60: grade = 'D' else: grade = 'F' print(f"Your grade is: {grade}")

A program that suggests activities based on the temperature.

temperature = int(input("Enter the temperature: ")) if temperature > 30: print("It's hot outside, consider going for a swim.") elif temperature > 20: print("The weather is nice, how about a walk in the park?") elif temperature > 10: print("It's a bit chilly, maybe stay indoors and read a book.") else: print("It's cold outside, stay warm and cozy indoors.")

Exercise 1: Age Group Classifier

Write a program that classifies a person into different age groups.

age = int(input("Enter your age: ")) if age < 0: print("Invalid age") elif age <= 12: print("You are a child.") elif age <= 19: print("You are a teenager.") elif age <= 65: print("You are an adult.") else: print("You are a senior citizen.")

Write a program that compares three numbers and prints the largest one.

num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) num3 = int(input("Enter the third number: ")) if num1 >= num2 and num1 >= num3: print(f"The largest number is {num1}") elif num2 >= num1 and num2 >= num3: print(f"The largest number is {num2}") else: print(f"The largest number is {num3}")

In this guide, we’ve explored Python’s conditional statements (if, else, elif), including their syntax, usage, and practical examples. Conditional statements are crucial for controlling the flow of your programs and making decisions based on different conditions. Practice these concepts with the provided examples and exercises to enhance your understanding and programming skills. In the next section, we will delve into loops, which allow you to repeat actions and iterate over sequences. Happy coding!