In: Computer Science
2. Write a function that takes a tuple as an argument and returns the tuple sorted.
3. Write a statement that creates a dictionary containing the following key-value pairs:
'a' : 1
'b' : 2
'c' : 3
4. After the following code executes, what elements will be members of set3, ?
set1 = set([10, 20, 30, 40])
set2 = set([40, 50, 60])
set3 = set1.union(set2)
set4 = set1.intersection(set2)
set5= set1.difference(set2)
Question 1:
Given the list values = [], write code that fills the list with
each set of numbers below. Note: use loops if it is necessary
1 2 3 4 5 6 7 8 9 10
0 2 4 6 8 10 12 14 16 18 20
1 4 9 16 25 36 49 64 81 100
0 0 0 0 0 0 0 0 0 0
1 4 9 16 9 7 4 9 11
0 1 0 1 0 1 0 1 0 1
0 1 2 3 4 0 1 2 3 4
Code:
#Given the list values = []
values=[]
#code that fills the list with each set of numbers below:
#Sequence: 1 2 3 4 5 6 7 8 9 10
for i in range(1,11):
values.append(i)
#Sequence: 0 2 4 6 8 10 12 14 16 18 20
for i in range(0,21,2):
values.append(i)
#Sequence: 1 4 9 16 25 36 49 64 81 100
for i in range(1,11):
values.append(i*i)
#Sequence: 0 0 0 0 0 0 0 0 0 0
for i in range(1,11):
values.append(0)
#Sequence: 1 4 9 16 9 7 4 9 11
values=values+[1,4,9,16,9,7,4,9,11]
#Sequence: 0 1 0 1 0 1 0 1 0 1
values=values+[0, 1, 0, 1, 0, 1, 0, 1, 0, 1]
#Sequence: 0 1 2 3 4 0 1 2 3 4
for i in range(0,2):
for j in range(0,5):
values.append(j)
Question 2:
Write a function that takes a tuple as an argument and returns the
tuple sorted.
Code:
#Define a function that takes a tuple as an argument
#and returns the tuple sorted.
def function(tup):
return tuple(sorted(tup))
Question 3:
Write a statement that creates a dictionary containing the
following key-value pairs:
'a' : 1
'b' : 2
'c' : 3
Code:
dictionary={'a':1,'b':2,'c':3}
Question 4:
After the following code executes, what elements will be members of
set3, ?
set1 = set([10, 20, 30, 40])
set2 = set([40, 50, 60])
set3 = set1.union(set2)
set4 = set1.intersection(set2)
set5= set1.difference(set2)
The elements which
are the members of set3 are:
{50, 20, 40, 10, 60, 30}
This is because we are storing the results of union of sets set1 and set2, so basically it will returns all the elements from set1 and set2
Please check the compiled program and its output for your reference:
Question-1:
Output:
Question-2:
Output:
Question-3:
Output:
Question-4:
Output:
(Feel free to drop me a comment, If you need any help)
Hope this Helps!!!
Please upvote as well, If you got the answer?
If not please comment, I will Help you with that...