Using If-Else and Elif Statements

Tutorial 1 of 5

Using If-Else and Elif Statements in Python

1. Introduction

In this tutorial, we aim to understand the importance of If-Else and Elif statements in Python. These are fundamental elements in Python programming that allow us to control the flow of our program based on certain conditions.

By the end of this tutorial, you will learn:
- What are If, Else and Elif statements
- How to use them effectively
- Write basic Python programs using these statements

Prerequisites: Basic understanding of Python programming language.

2. Step-by-Step Guide

If Statement

In Python, the if statement is used for conditional execution of code. If the condition is true, the block of code under it is executed.

if condition:
    # code to be executed if condition is True

Else Statement

The else statement is used to execute a block of code if the condition in the if statement is false.

if condition:
    # code to be executed if condition is True
else:
    # code to be executed if condition is False

Elif Statement

The elif statement allows you to check multiple expressions for True and execute a block of code as soon as one of the conditions evaluates to True.

if condition1:
    # code to be executed if condition1 is True
elif condition2:
    # code to be executed if condition2 is True
else:
    # code to be executed if both conditions are False

Best Practices and Tips:
- Keep your conditions simple and readable.
- Use parentheses () to group conditions, when necessary.
- Use elif statements effectively to avoid unnecessary checks.

3. Code Examples

Example 1:

temperature = 20

if temperature < 0:
    print("It's freezing!")
else:
    print("It’s not freezing.")

# Output: It’s not freezing.

In this example, the condition temperature < 0 is False, hence the code under else is executed.

Example 2:

temperature = 40

if temperature < 0:
    print("It's freezing!")
elif 0 <= temperature <= 30:
    print("The weather is normal.")
else:
    print("It's hot!")

# Output: It's hot!

In this example, the condition temperature < 0 is False, hence the elif condition is checked. The elif condition is also False, hence the code under else is executed.

4. Summary

We have learned about the use of If-Else and Elif statements in Python. These statements help us to control the flow of our program based on certain conditions.

Next, you should learn about loops (like for and while) in Python. Here are some resources for that:
- Python For Loops
- Python While Loops

5. Practice Exercises

Exercise 1: Write a program that prints "Good Morning!" if the time is less than 12, "Good Afternoon!" if the time is between 12 and 17, and "Good Evening!" if the time is more than 17.

Solution:

time = 15

if time < 12:
    print("Good Morning!")
elif 12 <= time < 17:
    print("Good Afternoon!")
else:
    print("Good Evening!")

# Output: Good Afternoon!

Exercise 2: Write a program that prints "Leap Year" if the given year is a leap year, "Not a Leap Year" otherwise. (Hint: A year is a leap year if it is divisible by 4 but not by 100, or if it is divisible by 400).

Solution:

year = 2000

if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
    print("Leap Year")
else:
    print("Not a Leap Year")

# Output: Leap Year

For further practice, try to solve problems from the "Conditionals and Loops" section on websites like HackerRank or LeetCode.