Python Exercise: Lists, Sets, and Tuples (Beginner Friendly)


List Exercises:
Create and Access List
Create a list called
fruits
containing "apple", "banana", "cherry", and "date".Print the second item in the list.
Change the third item to "blueberry" and print the updated list.
List Operations
Given the list
numbers = [1, 2, 3, 4, 5]
:Add the number 6 to the end of the list
Insert the number 0 at the beginning of the list
Remove the number 3 from the list
Print the final list
List Slicing
Given
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
:Create a new list containing elements from index 2 to 5
Create another list containing every alternate element starting from index 0
Print both new lists
List Methods
Create a list
colors = ["red", "green", "blue"]
Use list methods to:
Add "yellow" to the list
Remove "green" from the list
Reverse the order of the list
Print the final list
List Length and Checking
Create a list
pets = ["dog", "cat", "fish", "bird"]
Print the number of items in the list
Check if "hamster" is in the list and print the result
Check if "cat" is in the list and print the result
Set Exercises:
Create and Access Set
Create a set called
animals
containing "dog", "cat", "rabbit", and "hamster".Add "parrot" to the set.
Remove "rabbit" from the set.
Print the final set.
Set Operations
Given two sets:
set1 = {1, 2, 3, 4, 5} set2 = {4, 5, 6, 7, 8}
Find and print the union of both sets
Find and print the intersection of both sets
Find and print the difference between set1 and set2
Set Methods
Create a set
prime_numbers = {2, 3, 5, 7}
Use set methods to:
Add 11 to the set
Remove 3 from the set
Check if 5 is in the set
Print the final set and the result of the membership check
Frozen Set
Create a regular set
colors = {"red", "green", "blue"}
Convert it to a frozen set
Try to add "yellow" to the frozen set (catch the error if it occurs)
Print the frozen set
Set Length and Clearing
Create a set
vowels = {'a', 'e', 'i', 'o', 'u'}
Print the number of elements in the set
Clear all elements from the set
Print the empty set
Tuple Exercises:
Create and Access Tuple
Create a tuple called
days
containing "Monday", "Tuesday", "Wednesday", "Thursday", "Friday".Print the third item in the tuple.
Try to change the second item to "Sunnyday" (note what happens)
Tuple Unpacking
Create a tuple
student = ("John Doe", 25, "Computer Science")
Unpack the tuple into three variables:
name
,age
, andmajor
Print each variable separately
Tuple Concatenation
Given two tuples:
tuple1 = (1, 2, 3) tuple2 = (4, 5, 6)
Create a new tuple that combines both tuples
Create another tuple that repeats tuple1 three times
Print both new tuples
Tuple Methods
Create a tuple
numbers = (5, 2, 8, 2, 7, 2, 9, 2)
Use tuple methods to:
Count how many times 2 appears in the tuple
Find the index of the first occurrence of 7
Print both results
Tuple Conversion
Create a list
cities = ["Paris", "London", "Tokyo"]
Convert the list to a tuple
Try to append "New York" to the tuple (note what happens)
Print the tuple
Answers:
List Answers:
fruits = ["apple", "banana", "cherry", "date"] print(fruits[1]) # banana fruits[2] = "blueberry" print(fruits) # ['apple', 'banana', 'blueberry', 'date']
numbers = [1, 2, 3, 4, 5] numbers.append(6) numbers.insert(0, 0) numbers.remove(3) print(numbers) # [0, 1, 2, 4, 5, 6]
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] slice1 = letters[2:6] # ['c', 'd', 'e', 'f'] slice2 = letters[::2] # ['a', 'c', 'e', 'g'] print(slice1, slice2)
colors = ["red", "green", "blue"] colors.append("yellow") colors.remove("green") colors.reverse() print(colors) # ['blue', 'red', 'yellow']
pets = ["dog", "cat", "fish", "bird"] print(len(pets)) # 4 print("hamster" in pets) # False print("cat" in pets) # True
Set Answers:
animals = {"dog", "cat", "rabbit", "hamster"} animals.add("parrot") animals.remove("rabbit") print(animals) # {'dog', 'cat', 'hamster', 'parrot'}
set1 = {1, 2, 3, 4, 5} set2 = {4, 5, 6, 7, 8} print(set1.union(set2)) # {1, 2, 3, 4, 5, 6, 7, 8} print(set1.intersection(set2)) # {4, 5} print(set1.difference(set2)) # {1, 2, 3}
prime_numbers = {2, 3, 5, 7} prime_numbers.add(11) prime_numbers.remove(3) check = 5 in prime_numbers print(prime_numbers, check) # {2, 5, 7, 11} True
colors = {"red", "green", "blue"} frozen_colors = frozenset(colors) try: frozen_colors.add("yellow") # This will raise an AttributeError except AttributeError: print("Cannot modify a frozen set") print(frozen_colors) # frozenset({'red', 'green', 'blue'})
vowels = {'a', 'e', 'i', 'o', 'u'} print(len(vowels)) # 5 vowels.clear() print(vowels) # set()
Tuple Answers:
days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday") print(days[2]) # Wednesday # days[1] = "Sunnyday" # This will raise a TypeError
student = ("John Doe", 25, "Computer Science") name, age, major = student print(name) # John Doe print(age) # 25 print(major) # Computer Science
tuple1 = (1, 2, 3) tuple2 = (4, 5, 6) combined = tuple1 + tuple2 # (1, 2, 3, 4, 5, 6) repeated = tuple1 * 3 # (1, 2, 3, 1, 2, 3, 1, 2, 3) print(combined, repeated)
numbers = (5, 2, 8, 2, 7, 2, 9, 2) print(numbers.count(2)) # 4 print(numbers.index(7)) # 4
cities = ["Paris", "London", "Tokyo"] cities_tuple = tuple(cities) # cities_tuple.append("New York") # This will raise an AttributeError print(cities_tuple) # ('Paris', 'London', 'Tokyo')
Subscribe to my newsletter
Read articles from Arzath Areeff directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Arzath Areeff
Arzath Areeff
I co-founded digizen.lk to promote online safety and critical thinking. Currently, I’m developing an AI app to fight misinformation. As Founder and CEO of ideaGeek.net, I help turn startup dreams into reality, and I share tech insights and travel stories on my YouTube channels, TechNomad and Rz Omar.