comp-think/2021-2022

Lecture "Organising information: unordered structures", exercise 1

essepuntato opened this issue · 20 comments

Write a code in Python to create a set of the following elements: "​Bilbo"​, "​Frodo"​, "​Sam"​, "​Pippin"​, "​Merry"​.

my_set = {'Bilbo', 'Frodo', 'Sam', 'Pippin', 'Merry'}

my_first_set = {'Bilbo', 'Frodo', 'Sam', 'Pippin', 'Merry'}

my_set = {"Bilbo", "Frodo", "Sam", "Pippin", "Merry"}

mySet = {"Bilbo", "Frodo", "Sam", "Pippin", "Merry"}

my_set = set()
my_set.add('Bilbo')
my_set.add('Frodo')
my_set.add('Sam')
my_set.add('Pippin')
my_set.add('Merry')

otherwise:
my_set = {"Bilbo", "Frodo", "Sam", "Pippin", "Merry"}

Either

lotr_set = set()
lotr_set.add("Bilbo")
lotr_set.add("Frodo")
lotr_set.add("Sam")
lotr_set.add("Pippin")
lotr_set.add("Merry")

or
lotr_set = {"Bilbo", "Frodo", "Sam", "Pippin", "Merry"}

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 = {"Bilbo", "Frodo", "Sam", "Pippin", "Merry"}

image

since it is unordered, I tried to run the code multiple times and I obtained different sets

image

my_list = ["Bilbo", "Frodo", "Sam", "Pippin", "Merry"]
my_set = set()

for item in my_list:
    my_set.add(item)

print(my_set)

A shorter way is the line my_set = set({"​Bilbo"​, "​Frodo"​, "​Sam"​, "​Pippin"​, "​Merry"​})

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)

{'Merry', 'Frodo', 'Sam', 'Bilbo', 'Pippin'}

image

Schermata 2021-11-12 alle 17 40 20

my_set = set({" Bilbo", " Frodo", " Sam", " Pippin", " Merry"})

my_set = {'Bilbo', 'Frodo', 'Sam', 'Pippin', 'Merry'}

the_first_set = set()
the_first_set.add('Bilbo')
the_first_set.add('Frodo')
the_first_set.add('Sam')
the_first_set.add('Pippin')
the_first_set.add('Merry')
print(the_first_set)
my_set = set()
my_set.add('Bilbo')
my_set.add('Frodo')
my_set.add('Sam')
my_set.add('Pippin')
my_set.add('Merry')
print (my_set)

'''
my_first_set = {"Bilbo", "Frodo", "Sam", "Pippin", "Merry"}
#otherwise you can also do it this way
my_second_set = set()
my_second_set.add("Bilbo")
my_second_set.add("Frodo")
my_second_set.add("Sam")
my_second_set.add("Pippin")
my_second_set.add("Merry")
'''