A. Introduction
Every December, millions of kids are working on their Christmas wish list. They may add some items each day, day after day... they might accidentally add the same item twice or even ten times just because they want to show Santa how much they'd love to have that... Of course, Santa understands if a kid adds a toy ten times to their list, that does not mean he has to bring them ten of that toy. So how would Santa's list look like?
{Jeans, Car Legos, Car Legos, an iPad, Car Legos, Xbox, a baseball bat} -> {Jeans, an iPad, Car Legos, Xbox, a baseball bat}
B. A set is a list of "things" that do not contain duplicates. The order of the items does not matter. You can add, change, or sort the list. You can also merge two lists into one. The "things" can be numbers or strings but not other sets. For instance,
A = {1, 2, 3}
A = set('qwerty')
print(A)
will print {'e', 'q', 'r', 't', 'w', 'y'} as the output since set A was changed from {1, 2, 3}.
A = {1, 2, 3}
B = {3, 2, 3, 1}
print(A == B)
will print True because duplicates will be removed automatically and A and B are equal sets.
You can get the number of elements in the set using the function len.
You can add new elements to the set using the function add.
You can do more using the functions listed here.
C Try this:
Given a list of integers, count how many distinct numbers it has. This task can be solved in a single line.
Create your Christmas wish list and add 4 more things.
Compare your wish list with a friend's. What are the items that you both like?
AAAAAAAAAAA
a = {'3', '6', '9', '12'} b = {'2','4','6','8'} print(a|b) wishlist = {"dog toy", "pencils","candy"} wishlist.add ("Ipad charger") wishlist.add ("hot hands") wishlist.add ("homework pass") wishlist.add ("beanbag chair") print(wishlist) friend_wishlist_except_i_have_no_friends = {"world domination", "salt", "beanbag chair", "pillow", "hot hands"} print(friend_wishlist_except_i_have_no_friends & wishlist)
#Task 1 x = {20,2360,236,'jamestown'} y = {69,236,'Nashvile','Jamestown'} print(x ^ y) #Task 2 Christmas_List = {'boring_book', 'eeeeeee', 'something_lol'} #idk if this is how you want me to do this Other_Things = {'sad_apple', 20, 'happy_jolly_banana', 209} print("\n\n") print(Christmas_List | Other_Things) #Task 3 MyWish = {'Eat', 'sadApples', 'the bIg sad', 'Tree'} YourWishExceptYourNotGoingToGetItLol = {'sadApples', 'the big sad'} print("\n\n") print(MyWish ^ YourWishExceptYourNotGoingToGetItLol)