About These Assignments
This page contains 30+ practice assignments covering for each loop iteration in Python. You'll work with lists, tuples, dictionaries, sets, strings, and advanced iteration techniques.
š¢ Beginner
š Intermediate
š“ Advanced
š” Tip: Try each problem yourself first. Click "Show Solution" only after attempting.
1. Iterating Over Lists 5 Questions
šÆ Learning Goal: Master list iteration using for each loop.
Beginner Q1: Print List Elements
Iterate over a list of fruits and print each fruit name.
š” Solution:
fruits = ["apple", "banana", "cherry", "date", "elderberry"]
for fruit in fruits:
print(fruit)
for fruit in fruits:
print(fruit)
š Explanation: The for loop iterates through each element in the list.
Beginner Q2: Sum of List
Calculate the sum of all numbers in a list using a for each loop.
š” Solution:
numbers = [10, 20, 30, 40, 50]
total = 0
for num in numbers:
total += num
print(f"Sum: {total}")
total = 0
for num in numbers:
total += num
print(f"Sum: {total}")
š Explanation: Accumulates the sum by iterating through each element.
Intermediate Q3: Find Maximum
Find the maximum value in a list without using max().
š” Solution:
numbers = [34, 78, 12, 99, 56, 23]
max_val = numbers[0]
for num in numbers:
if num > max_val:
max_val = num
print(f"Maximum: {max_val}")
max_val = numbers[0]
for num in numbers:
if num > max_val:
max_val = num
print(f"Maximum: {max_val}")
š Explanation: Compares each element to find the maximum.
Intermediate Q4: Square Each Element
Create a new list containing squares of each element from the original list.
š” Solution:
numbers = [1, 2, 3, 4, 5]
squares = []
for num in numbers:
squares.append(num ** 2)
print(squares)
squares = []
for num in numbers:
squares.append(num ** 2)
print(squares)
š Explanation: Uses list append within the loop to build a new list.
Advanced Q5: Filter Even Numbers
Create a new list containing only even numbers from the original list.
š” Solution:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
evens = []
for num in numbers:
if num % 2 == 0:
evens.append(num)
print(evens)
evens = []
for num in numbers:
if num % 2 == 0:
evens.append(num)
print(evens)
š Explanation: Uses conditional check to filter elements.
2. Iterating Over Tuples 3 Questions
šÆ Learning Goal: Master tuple iteration using for each loop.
Beginner Q6: Print Tuple Elements
Iterate over a tuple of colors and print each color.
š” Solution:
colors = ("red", "green", "blue", "yellow")
for color in colors:
print(color)
for color in colors:
print(color)
š Explanation: Tuples are iterable just like lists.
Intermediate Q7: Tuple of Numbers - Sum and Product
Calculate the sum and product of numbers in a tuple.
š” Solution:
nums = (2, 3, 4, 5)
total = 0
product = 1
for n in nums:
total += n
product *= n
print(f"Sum: {total}, Product: {product}")
total = 0
product = 1
for n in nums:
total += n
product *= n
print(f"Sum: {total}, Product: {product}")
š Explanation: Iterates through tuple to compute sum and product.
Advanced Q8: Tuple Unpacking in Loop
Iterate over a list of tuples and unpack each tuple into variables.
š” Solution:
students = [("Alice", 25), ("Bob", 22), ("Charlie", 28)]
for name, age in students:
print(f"{name} is {age} years old")
for name, age in students:
print(f"{name} is {age} years old")
š Explanation: Tuple unpacking in the for loop makes code cleaner.
3. Iterating Over Dictionaries 4 Questions
šÆ Learning Goal: Master dictionary iteration using for each loop.
Beginner Q9: Print Dictionary Keys and Values
Iterate over a dictionary and print each key and value.
š” Solution:
person = {"name": "John", "age": 30, "city": "New York"}
for key, value in person.items():
print(f"{key}: {value}")
for key, value in person.items():
print(f"{key}: {value}")
š Explanation: items() returns key-value pairs for iteration.
Intermediate Q10: Sum of Dictionary Values
Calculate the sum of all values in a dictionary.
š” Solution:
scores = {"math": 90, "science": 85, "english": 88, "history": 92}
total = 0
for score in scores.values():
total += score
print(f"Total: {total}")
total = 0
for score in scores.values():
total += score
print(f"Total: {total}")
š Explanation: values() returns an iterable of dictionary values.
Advanced Q11: Find Key with Maximum Value
Find the key with the maximum value in a dictionary.
š” Solution:
scores = {"math": 90, "science": 85, "english": 88, "history": 92}
max_key = ""
max_val = -1
for key, value in scores.items():
if value > max_val:
max_val = value
max_key = key
print(f"Highest: {max_key} with {max_val}")
max_key = ""
max_val = -1
for key, value in scores.items():
if value > max_val:
max_val = value
max_key = key
print(f"Highest: {max_key} with {max_val}")
š Explanation: Tracks key with maximum value during iteration.
4. Iterating Over Sets 2 Questions
šÆ Learning Goal: Master set iteration using for each loop.
Beginner Q12: Print Set Elements
Iterate over a set of numbers and print each element.
š” Solution:
numbers = {10, 20, 30, 40, 50}
for num in numbers:
print(num)
for num in numbers:
print(num)
š Explanation: Sets are iterable, but order is not guaranteed.
Intermediate Q13: Set Operations in Loop
Find the sum and average of elements in a set.
š” Solution:
nums = {5, 10, 15, 20, 25}
total = 0
for n in nums:
total += n
avg = total / len(nums)
print(f"Sum: {total}, Average: {avg}")
total = 0
for n in nums:
total += n
avg = total / len(nums)
print(f"Sum: {total}, Average: {avg}")
š Explanation: Iterates through set to compute statistics.
5. Iterating Over Strings 3 Questions
šÆ Learning Goal: Master string iteration using for each loop.
Beginner Q14: Print Each Character
Iterate over a string and print each character.
š” Solution:
word = "Python"
for char in word:
print(char)
for char in word:
print(char)
š Explanation: Strings are iterable character by character.
Intermediate Q15: Count Vowels
Count the number of vowels in a string.
š” Solution:
text = "Hello World"
vowels = "aeiouAEIOU"
count = 0
for char in text:
if char in vowels:
count += 1
print(f"Vowels: {count}")
vowels = "aeiouAEIOU"
count = 0
for char in text:
if char in vowels:
count += 1
print(f"Vowels: {count}")
š Explanation: Iterates through string and checks for vowels.
6. Advanced Iteration 3 Questions
šÆ Learning Goal: Master advanced for each loop techniques.
Advanced Q16: Nested Iteration
Iterate over a list of lists and print each element.
š” Solution:
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
for row in matrix:
for element in row:
print(element, end=' ')
print()
for row in matrix:
for element in row:
print(element, end=' ')
print()
š Explanation: Nested for loops to iterate through 2D structure.
Advanced Q17: enumerate() Usage
Use enumerate() to get index and value while iterating.
š” Solution:
fruits = ["apple", "banana", "cherry"]
for index, fruit in enumerate(fruits):
print(f"{index}: {fruit}")
for index, fruit in enumerate(fruits):
print(f"{index}: {fruit}")
š Explanation: enumerate() provides index along with value.
Advanced Q18: zip() for Parallel Iteration
Iterate over two lists in parallel using zip().
š” Solution:
names = ["Alice", "Bob", "Charlie"]
scores = [85, 92, 78]
for name, score in zip(names, scores):
print(f"{name}: {score}")
scores = [85, 92, 78]
for name, score in zip(names, scores):
print(f"{name}: {score}")
š Explanation: zip() pairs elements from multiple iterables.
Try It Yourself!
Use the interactive editor below to test your solutions or write your own for each loop code.
Python Code Editor
========================================
FOR EACH LOOP PRACTICE
========================================
Fruits:
- apple
- banana
- cherry
Person Details:
name: Alice
age: 25
city: NYC
Characters in 'Python':
P y t h o n
ā Write your for each loop solutions here!
FOR EACH LOOP PRACTICE
========================================
Fruits:
- apple
- banana
- cherry
Person Details:
name: Alice
age: 25
city: NYC
Characters in 'Python':
P y t h o n
ā Write your for each loop solutions here!
š Related Tutorials
š For Each Loop
Learn about for each loop in Python
š For Loop
Learn about for loops in Python
š All Loops Assignments
More loop practice problems