Open essepuntato opened 4 years ago
my_set = set({'Frodo', 'Sam', 'Pippin', 'Merry', 'Bilbo'})
after my_set.remove('Bilbo')
--> my_set = set({'Frodo', 'Sam', 'Pippin', 'Merry'})
after my_set.add('Galadriel')
--> my_set = set({'Frodo', 'Sam', 'Pippin', 'Merry', 'Galadriel'})
after my_set.update(set({'Saruman', 'Frodo', 'Gandalf'}))
--> my_set = set({'Frodo', 'Galadriel', 'Gandalf', 'Sam', 'Saruman', 'Pippin', 'Merry'})
my_set = set({'Frodo', 'Sam', 'Pippin', 'Merry', 'Bilbo'})
my_set.remove("Bilbo")
my_set.add("Galadriel")
my_set.update(set({"Saruman", "Frodo", "Gandalf"}))
print(my_set)
#will print
{'Frodo', 'Saruman', 'Merry', 'Sam', 'Pippin', 'Gandalf', 'Galadriel'}
The status of my_set will be: my_set = {'Pippin', 'Sam', 'Galadriel', 'Gandalf', 'Frodo', 'Merry', 'Saruman'}
lotr_set = set({'Frodo', 'Sam', 'Merry', 'Pippin', 'Bilbo'})
lotr_set.remove('Bilbo')
# current state {'Merry', 'Frodo', 'Sam', 'Pippin'}
lotr_set.add('Galadriel')
# current state {'Sam', 'Pippin', 'Galadriel', 'Merry', 'Frodo'}
more_set = set()
lotr_set.add('Saruman')
lotr_set.add('Frodo')
lotr_set.add('Gandalf')
# create new set to add
lotr_set.update(more_set)
print(lotr_set)
# output {'Sam', 'Galadriel', 'Pippin', 'Frodo', 'Saruman', 'Merry', 'Gandalf'}
# 'Frodo' is not repeated because sets do not allow repetitions
After my_set.remove('Bilbo')
the set contains 'Merry', 'Frodo', 'Pippin', 'Sam'
after my_set.add('Galadriel')
the set contains 'Merry', 'Frodo', 'Pippin', 'Sam', 'Galadriel'
after my_set.update(set({"Saruman", "Frodo", "Gandalf"}))
the set contains 'Merry', 'Frodo', 'Pippin', 'Sam', 'Galadriel', 'Saruman', 'Gandalf'
my_set = {'Bilbo', 'Frodo', 'Sam', 'Pippin', 'Merry'}
my_set.remove('Bilbo')
#after this statement the string "Bilbo" is removed from the set
my_set.add("Galadriel")
#after this statement the string "Galadriel" is add to the list, but we don't know in what position
my_set.update({"Saruman", "Frodo", "Gandalf"})
#after this statement all the elements of a new set, if they aren't already in my_set,
#are included in, but we don't know in what order the elements are added
my_set=set()
my_set.add("Bilbo")
my_set.add("Frodo")
my_set.add("Sam")
my_set.add("Pippin")
my_set.add("Merry")
my_set.remove("Bilbo")
my_set.add("Galadriel")
my_set.update(set({"Saruman", "Frodo", "Gandalf"}))
#Output = "Saruman" ,"Galadriel", "Frodo", "Sam", "Merry","Gandalf", "Pippin"
Starting Set: my_set= set() my_set.add("Bilbo") my_set.add("Frodo") my_set.add("Sam") my_set.add("Merry") my_set.add("Pippin")
my_set.remove("Bilbo"): my_set = {'Pippin', 'Sam', 'Frodo', 'Merry'} my_set.add("Galadriel"):my_set = {'Frodo', 'Merry', 'Pippin', 'Sam', 'Galadriel'} my_set.update(set({"Saruman", "Frodo", "Gandalf"})): my_set = {'Frodo', 'Saruman', 'Merry', 'Pippin', 'Sam', 'Galadriel', 'Gandalf'}
char_set = {'Frodo', 'Merry', 'Sam', 'Bilbo', 'Pippin'}
char_set.remove("Bilbo") --> char_set = {'Sam', 'Frodo', 'Merry', 'Pippin'}
char_set.add("Galadriel") --> char_set = {'Sam', 'Frodo', 'Galadriel', 'Merry', 'Pippin'}
char_set.update(set({"Saruman", "Frodo", "Gandalf"})). --> char_set = {'Sam', 'Galadriel', 'Saruman', 'Frodo', 'Merry', 'Gandalf', 'Pippin'}
# The 'update' operation excludes any duplicate item, this is the reason why 'Frodo' appears only once.
# Consider the set created in the first exercise, stored in the variable my_set.
# Describe the status of lotr_set after the execution of each of the following
# operations: lotr_set.remove("Bilbo"), lotr_set.add("Galadriel"),
# lotr_set.update(set({"Saruman", "Frodo", "Gandalf"})).
lotr_set.remove("Bilbo") # "Bilbo will be removed from the set
# lotr_set contains: {'Pippin', 'Merry', 'Sam', 'Frodo'}
lotr_set.add("Galadriel") # "Galadriel" will be added to the set
# lotr_set contains: {'Pippin', 'Merry', 'Sam', 'Frodo', 'Galadriel'}
lotr_set.update(set({"Saruman", "Frodo", "Gandalf"})) # specified set will be added to the variable lotr_set, with the exception of duplicate items
# lotr_set contains {'Pippin', 'Merry', 'Sam', 'Frodo', 'Galadriel', 'Saruman', 'Gandalf'}
my_set.remove("Bilbo") # my_set = ({"Frodo", "Sam", "Pippin", "Mary"}) my_set.add("Galadriel") # my_set = ({"Frodo", "Galadriel", "Sam", "Pippin", "Mary"}) my_set.update(set({"Saruman", "Frodo", "Gandalf"})) # my_set = ({"Frodo", "Sam", "Pippin", "Mary", "Saruman", "Gandalf"})
{'Gandalf', 'Frodo', 'Saruman', 'Sam', 'Merry', 'Pippin', 'Galadriel'}`
my_set.remove("Bilbo")
my_set={"Pippin", "Sam", "Frodo", "Merry"}
my_set.add("Galadriel")
my_set={"Pippin", "Sam", "Frodo", "Merry", "Galadriel"}
my_set.update(set({"Saruman", "Frodo", "Gandalf"}))
my_set={"Pippin", "Sam", "Frodo", "Merry", "Galadriel", "Saruman", "Gandalf"}
When I ran these all in PythonTutor it would switch the order of the objects. Sets are unordered so it doesn't really matter I just wondering how it decides where to put them.
my_set = set()
my_set.add("Bilbo")
my_set.add("Frodo")
my_set.add("Sam")
my_set.add("Pippin")
my_set.add("Merry")
my_set.remove("Bilbo")
# the element "Bilbo" is removed from my_set
# output: {'Sam', 'Frodo', 'Pippin', 'Merry'}
my_set.add("Galadriel")
# the element "Galadriel" is added to my_set
# output: {'Sam', 'Galadriel', 'Frodo', 'Pippin', 'Merry'}
my_set.update(set({"Saruman", "Frodo", "Gandalf"}))
# a second set is added to "my_set"
# output: {'Gandalf', 'Saruman', 'Sam', 'Frodo', 'Pippin', 'Merry', 'Galadriel'}
# "Frodo" is not added because it is already present in my_set
`my_set = set(({'Frodo', 'Sam', 'Pippin', 'Merry', 'Bilbo'})) my_other_set = set(({"Saruman", "Frodo", "Gandalf"}))
my_set.remove("Bilbo")
my_set.add("Galadriel")
my_set.update(my_other_set)
`
my_set = set({'Frodo', 'Sam', 'Pippin', 'Merry', 'Bilbo'})
my_set.remove("Bilbo") # removes item "Bilbo" from set"
my_set.add("Galadriel") # adds "Galadriel" to the set
my_set.update(set({"Saruman", "Frodo", "Gandalf"})) # adds elements from another set to my_set
# Final status: {'Pippin', 'Sam', 'Frodo', 'Saruman', 'Merry', 'Gandalf', 'Galadriel'} - these are the elements contained in the set.
# They are printed in random order because the order of the items in a set is not relevant. Since sets don't allow multiple
# identical items, the item "Frodo" appears just once.
my_set = set() my_set.add("Bilbo") my_set.add("Frodo") my_set.add("Pippin") my_set.add("Marry") print(my_set) {'Pippin', 'Marry', 'Frodo', 'Bilbo'} my_set.remove("Bilbo") print(my_set) {'Pippin', 'Marry', 'Frodo'} my_set.add("Galadriel") print (my_set) {'Galadriel', 'Pippin', 'Marry', 'Frodo'} my_set.update(set({"Saruman", "Frodo", "Gandalf"})) print(my_set) {'Galadriel', 'Pippin', 'Marry', 'Frodo', 'Gandalf', 'Saruman'}
LOTR_set = set ()
LOTR_set.add("Bilbo")
LOTR_set.add("Frodo")
LOTR_set.add("Sam")
LOTR_set.add("Pippin")
LOTR_set.add("Merry")
print(LOTR_set) #returns {'Bilbo', 'Pippin', 'Frodo', 'Merry', 'Sam'}
LOTR_set.remove("Bilbo") #Python will remove the item "Bilbo"
print(LOTR_set) #returns {'Pippin', 'Frodo', 'Merry', 'Sam'}
LOTR_set.add("Galadriel") #Python will add the item "Galadriel" to the set
print(LOTR_set) #returns {'Galadriel', 'Pippin', 'Frodo', 'Merry', 'Sam'}
LOTR_set.update((set({"Saruman", "Frodo", "Gandalf"}))) #Python will update the set by includin another set which includes items "Saruman", "Frodo" and "Gandalf"
print(LOTR_set) #returns {'Galadriel', 'Gandalf', 'Saruman', 'Pippin', 'Frodo', 'Merry', 'Sam'}
lotr_set=set() lotr_set.add("Bilbo") lotr_set.add("Frodo") lotr_set.add("Sam") lotr_set.add("Pippin") lotr_set.add("Merry")
lotr_set.remove("Bilbo") lotr_set.add("Galadriel") lotr_set.update( set({"Saruman", "Frodo", "Gandalf"}))
lotr_set = {"Pippin", "Merry", "Frodo", "Galadriel", "Gandalf", "Sam", "Saruman"}
Consider the set created in the first exercise, stored in the variable
my_set
. Describe the status of my_set
after the execution of each of the following operations:my_set.remove("Bilbo")
, my_set.add("Galadriel")
,my_set.update(set({"Saruman", "Frodo", "Gandalf"}))
.