Set - Scenario Based Python Programs
Set - Scenario Based Python Programs
_______________________________________________________
Grocery Shopping:
1. You're at the grocery store and need to buy items for a recipe. Let A be the set of fruits
available, and B be the set of vegetables. If you need to buy items from both categories,
how would you represent the items you'll purchase using set notation? What's the
intersection of sets A and B?
Program
# Grocery Shopping
fruits = {'apple', 'banana', 'orange', 'kiwi', 'mango'}
vegetables = {'carrot', 'broccoli', 'spinach', 'lettuce', 'tomato'}
# Class Enrollment
math_students = {'Alice', 'Bob', 'Charlie', 'David', 'Eve'}
science_students = {'Alice', 'Frank', 'Grace', 'David', 'Hannah'}
# Players Selection
offense_players = {'Player1', 'Player2', 'Player3', 'Player4', 'Player5'}
defense_players = {'Player3', 'Player4', 'Player6', 'Player7', 'Player8'}
______________________________________________________________________________
Class Enrollment:
2. Suppose you're a school administrator and you need to organize class enrollments. Let A
be the set of students interested in Math, and B be the set of students interested in
Science. How would you represent the students who are interested in either Math or
Science or both using set notation? What's the union of sets A and B?
Program
Explanation
In this program:
● math_students represents the set of students interested in Math.
● science_students represents the set of students interested in Science.
● interested_in_either calculates the union of sets A and B, which represents the students
interested in either Math or Science or both.
The output will display the set of students interested in either Math or Science or both.
__________________________________________________________________________________
3. You're a social media influencer managing two platforms: Instagram and Twitter. Let A
be the set of your Instagram followers, and B be the set of your Twitter followers. If you
want to find out who follows you on both platforms, what operation would you perform
on sets A and B?
Program
Explanation:_
To find out who follows you on both Instagram and Twitter, you would perform the
Program
# Define sets of Instagram and Twitter followers
instagram_followers = {'user1', 'user2', 'user3', 'user4', 'user5'}
twitter_followers = {'user3', 'user4', 'user6', 'user7', 'user8'}
The output will display the set of followers who follow you on both Instagram and Twitter.
__________________________________________________________________________________
4. You're a member of a book club, and the club is selecting books to read for the next
month. Let A be the set of fiction books nominated, and B be the set of non-fiction books
nominated. If you want to ensure variety in the selection, how would you represent the
combined set of nominated books using set notation? What's the complement of set A?
Program
Explanation:-
To represent the combined set of nominated books using set notation, you would perform the
union operation on sets A (fiction books nominated) and B (non-fiction books nominated).
Additionally, to find the complement of set A, you would subtract set A from the set of all
zbooks available.
In this program:
● fiction_books represents the set of nominated fiction books.
● non_fiction_books represents the set of nominated non-fiction books.
● nominated_books calculates the union of sets A and B, representing the combined set
of nominated books.
● all_books represents the set of all available books.
● fiction_books_complement calculates the complement of set A, which is the set of all
available books except the nominated fiction books.
The output will display the combined set of nominated books and the complement of set A (fiction
books).
__________________________________________________________________________________
Team Selection:
5. You're a coach selecting players for a sports team. Let A be the set of players proficient in
offense, and B be the set of players proficient in defense. How would you represent the set
of players who are proficient in offense but not in defense using set notation? What's the
relative complement of set B with respect to set A?
Program
# Define sets of players proficient in offense and defense
offense_players = {'Player1', 'Player2', 'Player3', 'Player4', 'Player5'}
defense_players = {'Player3', 'Player4', 'Player6', 'Player7', 'Player8'}
Explanation
To represent the set of players who are proficient in offense but not in defense using set
notation, you would perform the relative complement operation on set A (players
proficient in offense) with respect to set B (players proficient in defense). Similarly, to
find the relative complement of set B with respect to set A, you would perform the relative
complement operation on set B with respect to set A.
In this program:
The output will display the set of players proficient in offense but not in defense, and the set of players
proficient in defense but not in offense.
__________________________________________________________________________________