Starting Out with Python (4th Edition)
4th Edition
ISBN: 9780134444321
Author: Tony Gaddis
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Expert Solution & Answer
Chapter 9, Problem 20SA
Explanation of Solution
Sets:
- • The set is an unordered collection of unique elements.
- • The set may contain different types of data; it can be numbers, characters, strings, and also class objects.
- • In Python, “set()” method is used to create the sets.
Subset:
- • If two sets are given, then if one set contains some or all the elements of other set, then that set is called the subset of other set.
- • The “issubset()” method can be used to check whether one set is a subset of the other.
Syntax:
In Python, the method “issubset()” can be used to check subset, and it can expressed as follows:
set1.issubset(set2)
Program:
#create the set set1 with five elements
set1 = set([100, 200, 300, 400, 500])
#create the set set2 with three elements
set2 = set([200, 400, 500])
#check whether set1 is subset of set2
print('set1 is subset of set2:',set1.issubset(set2))
#check whether set2 is subset of set1
print('set2 is subset of set1:',set2.issubset(set1))
Explanation of the program:
In the above Python code,
- • The first statement is to create the set “set1” using the “set()” function; the “set1” contains “100”, “200”, “300”, “400”, and “500”.
- • The second statement is to create the set “set2” using the “set()” function; the “set2” contains “200”, “400”, and “500”.
- • The third statement is to check whether “set1” is a subset of “set2”; if the result is “true”, it means “set1” is a subset of “set2”.
- • Here, the result is “false”; thus, “set1” is not a subset of “set2”.
- • The fourth statement is to check whether “set2” is a subset of “set1”; if the result is “true”, it means “set2” is a subset of “set1”.
- • Here, the result is “true”; thus, “set2” is a subset of “set1”...
Expert Solution & Answer
Trending nowThis is a popular solution!
Students have asked these similar questions
IntSet unionWith(const IntSet& otherIntSet) const;
//Unions the current set with the passed set. IntSet::unionWith(const IntSet& otherIntSet){ for (int i = 0; i < otherIntSet.used; ++i) //For each element in the passed set. { add(otherIntSet.data[i]); //Add it to current set. }}
1) set1 = set([10, 20, 30, 40])
set2 = set([40, 50, 60])
set3 = set1.union(set2) set4 = set1.difference(set2)
set5 = set2.difference(set1) set6 = set1.symmetric_difference(set2) print(set3) print(set4) print(set5) print(set6)
What is the output of the print statements?
Write a program that asks the user toenter two non-empty sets and display their intersection and union.
Design the following function in the program:
set_intersection–This function gets the intersection of two given sets.
set_union–This function gets the union of two given sets.
Chapter 9 Solutions
Starting Out with Python (4th Edition)
Ch. 9.1 - An element in a dictionary has two parts. What are...Ch. 9.1 - Which part of a dictionary element must be...Ch. 9.1 - Suppose ' start' : 1472 is an element in a...Ch. 9.1 - Suppose a dictionary named employee has been...Ch. 9.1 - What will the following code display? stuff = {1 :...Ch. 9.1 - Prob. 6CPCh. 9.1 - Suppose a dictionary named inventory exists. What...Ch. 9.1 - What will the following code display? stuff = {1 :...Ch. 9.1 - What will the following code display? stuff = {1 :...Ch. 9.1 - What is the difference between the dictionary...
Ch. 9.1 - Prob. 11CPCh. 9.1 - Prob. 12CPCh. 9.1 - What does the values method return?Ch. 9.2 - Prob. 14CPCh. 9.2 - Prob. 15CPCh. 9.2 - Prob. 16CPCh. 9.2 - After the following statement executes, what...Ch. 9.2 - After the following statement executes, what...Ch. 9.2 - After the following statement executes, what...Ch. 9.2 - After the following statement executes, what...Ch. 9.2 - After the following statement executes, what...Ch. 9.2 - Prob. 22CPCh. 9.2 - After the following statement executes, what...Ch. 9.2 - After the following statement executes, what...Ch. 9.2 - Prob. 25CPCh. 9.2 - Prob. 26CPCh. 9.2 - After the following code executes, what elements...Ch. 9.2 - Prob. 28CPCh. 9.2 - After the following code executes, what elements...Ch. 9.2 - After the following code executes, what elements...Ch. 9.2 - After the following code executes, what elements...Ch. 9.2 - Prob. 32CPCh. 9.3 - Prob. 33CPCh. 9.3 - When you open a file for the purpose of saving a...Ch. 9.3 - When you open a file for the purpose of retrieving...Ch. 9.3 - Prob. 36CPCh. 9.3 - Prob. 37CPCh. 9.3 - Prob. 38CPCh. 9 - You can use the __________ operator to determine...Ch. 9 - You use ________ to delete an element from a...Ch. 9 - The ________ function returns the number of...Ch. 9 - You can use_________ to create an empty...Ch. 9 - The _______ method returns a randomly selected...Ch. 9 - The __________ method returns the value associated...Ch. 9 - The __________ dictionary method returns the value...Ch. 9 - The ________ method returns all of a dictionary's...Ch. 9 - The following function returns the number of...Ch. 9 - Prob. 10MCCh. 9 - Prob. 11MCCh. 9 - This set method removes an element, but does not...Ch. 9 - Prob. 13MCCh. 9 - Prob. 14MCCh. 9 - This operator can be used to find the difference...Ch. 9 - Prob. 16MCCh. 9 - Prob. 17MCCh. 9 - The keys in a dictionary must be mutable objects.Ch. 9 - Dictionaries are not sequences.Ch. 9 - Prob. 3TFCh. 9 - Prob. 4TFCh. 9 - The dictionary method popitem does not raise an...Ch. 9 - The following statement creates an empty...Ch. 9 - Prob. 7TFCh. 9 - Prob. 8TFCh. 9 - Prob. 9TFCh. 9 - Prob. 10TFCh. 9 - What will the following code display? dct =...Ch. 9 - What will the following code display? dct =...Ch. 9 - What will the following code display? dct =...Ch. 9 - What will the following code display? stuff =...Ch. 9 - How do you delete an element from a dictionary?Ch. 9 - How do you determine the number of elements that...Ch. 9 - Prob. 7SACh. 9 - What values will the following code display? (Dont...Ch. 9 - After the following statement executes, what...Ch. 9 - After the following statement executes, what...Ch. 9 - After the following statement executes, what...Ch. 9 - After the following statement executes, what...Ch. 9 - After the following statement executes, what...Ch. 9 - What will the following code display? myset =...Ch. 9 - After the following code executes, what elements...Ch. 9 - Prob. 16SACh. 9 - Prob. 17SACh. 9 - Prob. 18SACh. 9 - After the following code executes, what elements...Ch. 9 - Prob. 20SACh. 9 - Write a statement that creates a dictionary...Ch. 9 - Write a statement that creates an empty...Ch. 9 - Assume the variable dct references a dictionary....Ch. 9 - Assume the variable dct references a dictionary....Ch. 9 - Prob. 5AWCh. 9 - Prob. 6AWCh. 9 - Prob. 7AWCh. 9 - Prob. 8AWCh. 9 - Prob. 9AWCh. 9 - Prob. 10AWCh. 9 - Assume the variable dct references a dictionary....Ch. 9 - Write code that retrieves and unpickles the...Ch. 9 - Course information Write a program that creates a...Ch. 9 - Capital Quiz The Capital Quiz Problem Write a...Ch. 9 - File Encryption and Decryption Write a program...Ch. 9 - Unique Words Write a program that opens a...Ch. 9 - Prob. 5PECh. 9 - File Analysis Write a program that reads the...Ch. 9 - World Series Winners In this chapters source code...Ch. 9 - Name and Email Addresses Write a program that...Ch. 9 - Blackjack Simulation Previously in this chapter...Ch. 9 - Word Index Write a program that reads the contents...
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- 2. Let S = {2, 4, 6} and T = {1, 3, 5}. Use the set-roster notation to write the following set, and indicate the number of elements that are in the set: ▪ TXT ▪ TX Sarrow_forwardThe elements in the set cannot be accessed using indexing and slicing, choose whether this statement is true or false. a. True b. False Clear my choicearrow_forwardThe following code doesn't correctly return the index values of the minimum contiguous subsequence. Please modify it so that it does, with comments if possible. def minSumRec(a, left, right): if left == right: return [a[left], left, right] center = int((left + right) / 2) min_left_sum = minSumRec(a, left, center) min_right_sum = minSumRec(a, center + 1, right) min_left_border_sum, left_border_sum = 1e9, 0 for i in range(center, left - 1, -1): left_border_sum += a[i] if left_border_sum < min_left_border_sum: min_left_border_sum = left_border_sum min_right_border_sum, right_border_sum = 1e9, 0 for i in range(center + 1, right + 1): right_border_sum += a[i] if right_border_sum < min_right_border_sum: min_right_border_sum = right_border_sum min_border_sum = min_right_border_sum + min_left_border_sum minSum = min(min_left_sum[0], min_right_sum[0], min_border_sum) if minSum == min_left_sum: return min_left_sum if minSum == min_right_sum: return min_right_sum return…arrow_forward
- void dict_clear (dict_t* dict); This function clears the dictionary dict, destroying each pair key/value, but does not destroy dict itself (remember that dict was allocated using dict_create, so it will need to be freed at some point, but this is not this function’s job.) This is now the function that frees (destroys) a dictionary: void dict_destroy (dict_t* dict); This operates just as dict_clear, but in addition should free the memory that was allocated during dict_create. After a call to this function, if any other library function receives the pointer dict, the behavior is undefined (most likely, it will crash). This simple function returns the current size of the dictionary: size_t dict_size (const dict_t* dict);arrow_forwardC. _______ the set of all subsets of a set including the empty set and the set itself.arrow_forwardgpacalc.py by using 'sys.argv" ● Use the following dictionary to complete this assignment: {'A':4.0, 'A-':3.66, 'B+':3.33, 'B':3.0, 'B-':2.66, 'C+':2.33, 'C':2.0, 'C-':1.66, 'D+':1.33, 'D':1.00, 'D-':.66, 'F':0.00} ● Create a program, gpacalc.py, that takes four letter grade arguments and prints out the corresponding GPA, to two decimals. Your program should work both in arguments are upper-case and lower-case. The values should be to two decimal places. Your program should print in the form: “My GPA is x” Where x is equal to the GPA calculationarrow_forward
- More hurry _______ the set of all subsets of a set including the empty set and the set itselfarrow_forwardSolution in python apply set and dictionaryarrow_forwarddef city_dict(adict): """ Question 6 -Given a dictionary that maps a person to a list of countries they want to travel to, return a dictionary with the value being the list sorted by the last letter in each countries name. If two countries have the same last letter, sort by the first letter. -For an extra challenge, try doing this in one line (Optional). Args: adict (dict) Returns: dict >>> city_dict({"Pablo": ["Belgium", "Canada", "Germany"], "Athena": ["Italy", "France", "Egypt"], "Liv": ["Japan", "Bolivia", "Greece"]}) {'Pablo': ['Canada', 'Belgium', 'Germany'], 'Athena': ['France', 'Egypt', 'Italy'], 'Liv': ['Bolivia', 'Greece', 'Japan']} >>> city_dict({"Jacob": ["Bahamas", "Brazil", "Chile"], "Lexi": ["Colombia", "Finland", "Panama"], "Emily": ["Ireland", "Russia", "Kenya", "Jordan"]}) {'Jacob': ['Chile', 'Brazil', 'Bahamas'], 'Lexi': ['Colombia', 'Panama', 'Finland'], 'Emily': ['Kenya',…arrow_forward
- Code the following directions in python. 1.Assume the variable dct references a dictionary. Write an if statement that determines whether the key 'Jim' exists in the dictionary. If so, delete 'Jim' and its associated value. 2.Write code to create a set with the following integers as members: 10, 20, 30, and 40. 3.Assume each of the variables set1 and set2 references a set. Write code that creates another set containing all the elements of set1 and set2, and assigns the resulting set to the variable set3.arrow_forward185e def find(self, key): 186e 187 188 Efficiency: 189 Finds and returns a copy of value in the set that matches key. Use: value = source.find(key) 190 191 192 193 Parameters: 194 key - a partial data element (?) 195 Returns: 196 value - a copy of the full value matching key, otherwise None (?) 197 198 199 200 # your code here 201arrow_forwarddef distinct_last_name(full_names): ''' Question 5 You are given a large string that contains a list of full names separated by commas, you are asked to retrieve all distinct last names and store them in a set. Args: full_names (string) Returns: list >>> distinct_last_name('Harry Sharp, Chen Zhou, Tong Zhou, Dey Santanu') {'Sharp, Zhou, Santanu'} ''' # print(distinct_last_name('Harry Sharp, Chen Zhou, Tong Zhou, Dey Santanu'))arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education