In: Computer Science
Exercise 3
Step 1:
When you read Storing Data Using Sets, you learned that Python's set type allows us to create mutable collections of unordered distinct items. The items stored in a set must be immutable, so sets can contain values of type int, float or str, but we can't store lists or sets in sets. Tuples are immutable, so we can store tuples in sets. Try this experiment, which creates a set containing the points (1.0, 2.0), (4.0, 6.0) and (10.0, -2.0). What is displayed when points is evaluated? Write the question and the answer in lab11.py using python comments. >>> points = {(1.0, 2.0), (4.0, 6.0), (10.0, -2.0)} >>> points We can also initialize the set this way. Try this experiment. What is displayed when points is evaluated? Write the question and the answer in lab11.py using python comments. >>> point1 = (1.0, 2.0) >>> point2 = (4.0, 6.0) >>> point3 = (10.0, -2.0) >>> points = {point1, point2, point3} >>> points We could instead start with an empty set, and call the add method to initialize it, one point at a time. Try this experiment. What is displayed when points is evaluated? Write the question and the answer in lab11.py using python comments. >>> points = set() >>> points.add(point1) >>> points.add(point2) >>> points.add(point3) >>> points
Step 2:
What happens if we try to insert a point that is already in the set? Try this experiment: >>> points.add(point2) >>> points How many copies of point (4.0, 6.0) are in the set? Write the question and the answer in lab11.py using python comments.
Step 3:
Can individual points in the set be retrieved by specifying an index (position)? Try this experiment. What is displayed when points[0] is evaluated? Write the question and the answer in lab11.py using python comments. >>> points[0]
Step 4:
We can use a for loop to iterate over all the points in the set. What is displayed when this loop is executed? Write the question and the answer in lab11.py using python comments. >>> for point in points: ... print(point) ...
Answer:
lab11.py
#step 1
#case 1- When Point is evaluated it gives
point={(1.0,2.0),(4.0,6.0),(10.0,-2.0)}
#Ans: When point is evaluated it gives
#{(1.0, 2.0), (10.0, -2.0), (4.0, 6.0)}
#case 2-
point1=(1.0,2.0)
point2=(4.0,6.0)
point3=(10.0,-2.0)
points={point1,point2,point3}
#Ans: When point is evaluated it gives
#{(1.0, 2.0), (10.0, -2.0), (4.0, 6.0)}
#case 3-
points = set()
points.add(point1)
points.add(point2)
points.add(point3)
#Ans: When point is evaluated it gives
#{(1.0, 2.0), (10.0, -2.0), (4.0, 6.0)}
#step2
points.add(point2)
#Ans: When point is evaluated it gives
#{(1.0, 2.0), (10.0, -2.0), (4.0, 6.0)}
#Only one copy of (4.0, 6.0) is there in the set
#step3
#Can individual points in the set be retrieved by specifying an index (position)? Try this experiment. What is displayed when points[0] is evaluated?
#Ans: When point[0] is evaluated it gives the following error message
#Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
#TypeError: 'set' object is not subscriptable
#step4
#We can use a for loop to iterate over all the points in the set. What is displayed when this loop is executed?
for point in points:
print(point)
#Ans: When Loop is evaluated it gives
#(1.0, 2.0)
#(10.0, -2.0)
#(4.0, 6.0)
Below is the Screenshot from the python Interpreter for the same queries: