Exercise 3
Step 1:
When you read Storing Data Using Sets, you learned that Python'sset type allows us to create mutable collections of unordereddistinct items. The items stored in a set must be immutable, sosets can contain values of type int, float or str, but we can'tstore lists or sets in sets. Tuples are immutable, so we can storetuples in sets. Try this experiment, which creates a set containingthe points (1.0, 2.0), (4.0, 6.0) and (10.0, -2.0). What isdisplayed when points is evaluated? Write the question and theanswer in lab11.py using python comments. >>> points ={(1.0, 2.0), (4.0, 6.0), (10.0, -2.0)} >>> points We canalso initialize the set this way. Try this experiment. What isdisplayed when points is evaluated? Write the question and theanswer 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, andcall the add method to initialize it, one point at a time. Try thisexperiment. What is displayed when points is evaluated? Write thequestion 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 theset? Try this experiment: >>> points.add(point2)>>> points How many copies of point (4.0, 6.0) are in theset? Write the question and the answer in lab11.py using pythoncomments.
Step 3:
Can individual points in the set be retrieved by specifying anindex (position)? Try this experiment. What is displayed whenpoints[0] is evaluated? Write the question and the answer inlab11.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 questionand the answer in lab11.py using python comments. >>> forpoint in points: ... print(point) ...