Lab Exercise - While Loop and For Loop
Lab Exercise - While Loop and For Loop
Objective:
In this lab, you will practice using two types of loops in Python: the while loop and the for loop. By
the end of the exercise, you should be comfortable writing loops to handle repetitive tasks.
A while loop continues to execute as long as a specified condition is True. It is commonly used
when you do not know beforehand how many iterations are required.
# Initialize counter
count = 0
Explanation: This simple loop prints the value of count until it reaches 5.
password = ""
print("Access granted.")
Explanation: The loop continues to prompt the user for the correct password until they enter
"1234".
Example 3: Infinite while loop (with break)
while True:
if answer == "exit":
Explanation: The loop will continue indefinitely until the user types "exit", at which point the loop
terminates using break.
number = 0
number += 1
if number % 2 == 0:
Explanation: The loop increments the number and skips over even numbers, printing only odd
numbers.
counter = 0
counter += 1
i=1
while i <= 3:
j=1
while j <= 2:
j += 1
i += 1
Explanation: This loop prints a combination of i and j values by using nested loops.
# Countdown from 10 to 1
n = 10
while n > 0:
print(n)
n -= 1
print("Liftoff!")
n = 10
sum_of_numbers = 0
while n > 0:
sum_of_numbers += n
n -= 1
keep_running = True
while keep_running:
if choice == "no":
keep_running = False
print("Loop ended.")
Explanation: A flag keep_running controls whether the loop continues or stops based on user
input.
x=0
y=5
x += 1
y -= 1
Explanation: The loop runs as long as both conditions (x < 5 and y > 0) are True.
A for loop is used to iterate over a sequence (like a list, tuple, or string) or other iterable objects.
for i in range(5):
print("Number:", i)
Explanation: This loop prints numbers from 0 to 4 using the range() function.
Example 2: Iterating over a list
# List iteration
print(fruit)
Explanation: This loop iterates over a list of fruits and prints each one.
print(char)
Explanation: enumerate() provides both the index and the value of the list elements.
# Dictionary iteration
# Break example
if number == 5:
print(number)
Explanation: The loop stops when the number reaches 5 due to the break statement.
# Continue example
if number % 2 == 0:
print(number)
Explanation: The loop skips even numbers and prints only the odd ones.
Explanation: This nested loop prints the multiplication table for numbers 1 through 3.
print(i)
print(squares)
Explanation: This uses a for loop in list comprehension to generate a list of squares of numbers
from 1 to 5.
Summary:
• While Loops: Use when the number of iterations is not known beforehand or depends on a
condition.
• For Loops: Use to iterate over sequences or when the number of iterations is known.
This lab provides you with several examples, ranging from beginner to intermediate level, allowing
you to understand loops in various contexts. Feel free to modify the examples and experiment with
different conditions.