Python Practice Worksheet
Sets, Functions, and Lambda Functions
Part A: Practice with Python Sets (10 Questions)
1. Create a set from a list with duplicate elements. Print the set.
2. Create a set of 3 animals. Add one more and remove one using .add() and .remove().
3. Ask the user to enter a city name and check if it's in your predefined set of cities.
4. Given two sets of subjects selected by two students, find all subjects either of them selected
(union).
5. From the same sets, find common subjects both students selected (intersection).
6. Find subjects selected by student A but not by student B (difference).
7. Find subjects that are in one of the sets but not both (symmetric difference).
8. Let A = {1, 2, 3} and B = {1, 2, 3, 4}. Check if A is a subset of B and if B is a superset of A.
9. Ask the user to enter names of friends (with possible duplicates). Use a set to remove duplicates.
10. Create a set of 5 elements. Print its length, then clear the set and print it again.
Part B: Practice with Functions and Lambda Functions (5 Questions)
1. Write a function that takes a list of ages and uses filter with lambda to return only ages 60 and
above.
2. Write a function that takes a list of numbers and returns a list of squares of even numbers using
map and lambda.
3. Create a function that takes a list of salaries and returns a new list with 10% bonus added using
lambda.
4. Given a list of tuples like [('Alice', 20), ('Bob', 15)], write a function to sort it by the second value
using lambda.
5. Write a function that takes a score and returns 'Fail' (<40), 'Pass' (40-74), or 'Distinction' (75+)
using lambda inside the function.