Lesson Introduction: Combining Loops with Conditionals - The Power Duo

Greetings, student! Today, we're fusing Python loops and conditionals together. Conditionals empower our code to make decisions, while loops enable the execution of repetitive tasks. Let's master this synergy!

The Basics of Conditions in Loops

Loops, such as for and while, repeat specific tasks, and conditionals — if, elif, and else — guide the path of the code. Combining these constructs equips us with a virtual super robot that performs repeated tasks with decision-making abilities.

Let's consider sending personalized party invitations. In this context, loops go through each guest, and conditionals decide the style of the invitation:

# Invite guests using a loop with a conditional
# Each guest has a name and invitation type - VIP or Regular
guests = [('Alice', 'VIP'), ('Bob', 'VIP'), ('Tom', 'Regular'), ('Jerry', 'Regular')]

for guest in guests:
    if guest[1] == 'VIP':
        print("Dear", guest[0], ", join us for a grand celebration!")
    elif guest[1] == 'Regular':
        print("Hi", guest[0], ", you are invited!")

This code prints:

Dear Alice , join us for a grand celebration!
Dear Bob , join us for a grand celebration!
Hi Tom , you are invited!
Hi Jerry , you are invited!
Working with Conditionals in For Loops

Python’s For Loop iterates over a defined sequence of elements. When we pair a conditional with the loop, the execution adjusts with each iteration based on the condition.

For instance, consider hosting a party. We have a guest_list and an unwanted_list. By pairing a For Loop with a conditional, we can ensure that only welcomed guests gain admission:

# For Loop with a conditional
guest_list = ['Alice', 'Bob', 'Tom', 'Jerry', 'Snow']
unwanted_guests = ['Tom', 'Snow']

for guest in guest_list:
    if guest not in unwanted_guests:
        print("Welcome,", guest, "!")
    else:
        print("Sorry,", guest, ", the party is full.")

The code prints:

Welcome, Alice !
Welcome, Bob !
Sorry, Tom , the party is full.
Welcome, Jerry !
Sorry, Snow , the party is full.
Implementing Conditionals in While Loops

A While Loop continues as long as its condition remains valid. Inserting a conditional within it can alter or halt its iterations based on changing conditions.

Suppose that when an unwanted guest arrives, the doorman closes the gate:

# While Loop with a conditional
guest_list = ['Alice', 'Bob', 'Tom', 'Jerry', 'Snow']
unwanted_guests = ['Tom', 'Snow']
guest_index = 0

while guest_index < len(guest_list):
    if guest_list[guest_index] not in unwanted_guests:
        print("Please come in,", guest_list[guest_index], "!")
    else:
        print("Party Over:", guest_list[guest_index], "showed up!")
        break # This will stop the while loop completely
    guest_index += 1

The code prints:

Please come in, Alice !
Please come in, Bob !
Party Over: Tom showed up!
Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal