Question

In: Computer Science

Exercise 3 Step 1: When you read Storing Data Using Sets, you learned that Python's set...

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) ...

Solutions

Expert Solution

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:


Related Solutions

When you read data in (without using getLine) how is the data read. does it read...
When you read data in (without using getLine) how is the data read. does it read spaces and line breakers(\n). Also how can you read a paragraph into a double vector of strings ( into words and strings again without using getLine)
Using a real-world data set that interests you, conduct one of the tests you learned this...
Using a real-world data set that interests you, conduct one of the tests you learned this week (or fit a linear regression model). Make sure to document all steps in the hypothesis testing process, including stating your hypotheses, your code, your output and your findings along with interpretation. You may use data from the MASS library if you wish, or load external data
Using a real-world data set that interests you, conduct one of the tests you learned this...
Using a real-world data set that interests you, conduct one of the tests you learned this week (or fit a linear regression model). Make sure to document all steps in the hypothesis testing process, including stating your hypotheses, your code, your output and your findings along with interpretation. You may use data from the MASS library if you wish, or load external data Please type all answer.
Exercise 3.1.5: Expressing sets in set builder notation. About Express each set using set builder notation....
Exercise 3.1.5: Expressing sets in set builder notation. About Express each set using set builder notation. Then if the set is finite, give its cardinality. Otherwise, indicate that the set is infinite. PLEASE show some explanation how you got answer. Thanks! (a) {-2, -1, 0, 1, 2} (b) { 3, 6, 9, 12, .... } (c) { -3, -1, 1, 3, 5, 7, 9 } (d) { 0, 10, 20, 30, ...., 1000 } Exercise 3.2.4: A subset of a...
Exercise 1. Write an algorithm (pseudocode) to read a set of sales data items from standard...
Exercise 1. Write an algorithm (pseudocode) to read a set of sales data items from standard input and calculate and output their total and their average. Prompt user to enter number of data items. Exercise 2. Create a test data set to verify your algorithm. How many cases are needed? Explain. Write your test data set below for submission to the EOL dropbox upon completion of your lab. Number of items List data items Expected output Case 1: Exercise 3....
Step 1: Read the following questions, and use what you have learned about how the economy...
Step 1: Read the following questions, and use what you have learned about how the economy might impact your business industry, and to summarize your responses in 5 full pages. Consider the concepts and questions below when assessing the economy and the impact it may pose on your business: Describe your ideal capitalist economy. Will these conditions maximize your sustainability and profitability? What indicators would you use to measure goods and services? What happens when the quantity demand is impacted?...
Step 1: Read the following questions, and use what you have learned about how the economy...
Step 1: Read the following questions, and use what you have learned about how the economy might impact your business industry, and to summarize your responses in 5 full pages. Consider the concepts and questions below when assessing the economy and the impact it may pose on your business: Describe your ideal capitalist economy. Will these conditions maximize your sustainability and profitability? What indicators would you use to measure goods and services? What happens when the quantity demand is impacted?...
describe data formatting issues when using and storing data for multiple sources. *pls cite +
describe data formatting issues when using and storing data for multiple sources. *pls cite +
Using R studio 1. Read the iris data set into a data frame. 2. Print the...
Using R studio 1. Read the iris data set into a data frame. 2. Print the first few lines of the iris dataset. 3. Output all the entries with Sepal Length > 5. 4. Plot a box plot of Petal Length with a color of your choice. 5. Plot a histogram of Sepal Width. 6. Plot a scatter plot showing the relationship between Petal Length and Petal Width. 7. Find the mean of Sepal Length by species. Hint: You could...
Consider the following two sample data sets. Set​ 1: 5 3 2 8 6 Set​ 2:...
Consider the following two sample data sets. Set​ 1: 5 3 2 8 6 Set​ 2: 3 12 13 2 7 a. Calculate the coefficient of variation for each data set. b. Which data set has more​ variability? a. The coefficient of variation for set 1 is nothing ​%. ​(Round to one decimal place as​ needed.)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT