# CodeStepByStep

## set_concepts

Language/Type: Python collections set

 A list has every function that a set has, and more. So why would you use a set rather than a list? (Check all that apply.) You don't need the missing functions for the program you are writing, just the ability to add/remove/search. You do not have enough memory to use a list. You want to avoid duplicates. Sets are more backward-compatible with older versions of Python. You want to be able to search the collection quickly. Using a collection with fewer capabilities builds character and makes your parents proud of you. (order shuffled) A set doesn't have the indexes that a list has. How do you examine every element of a set? Use a for loop over a range, examining each index i. Convert the set into a list, then use a for loop over the list. Call the examine() method on the set. Use [] on each value one at a time. Use a for-each loop over the values in the set. (order shuffled) How do you check whether a set contains a given value? Use the keyword 'in' to test for membership. Print the set and read the output. Add the value, and see if an error is raised. Call the exists() function on the set. Use the [] operator on the set. (order shuffled) What happens if you try to add a value to a set but that value is already present in the set? An error is raised. An error message prints out on the console saying, "bad value." Nothing happens; the new value is not added. The new value is added; it will now occur twice in the set. The value is added, but the set is converted into a list. (order shuffled) How do you merge the contents of two sets? ``` set1 | set2 ``` ``` set1 & set2 ``` ``` set1 ^ set2 ``` ``` set1.add(set2) ``` ``` set1 + set2 ``` (order shuffled) How do you find out what elements are stored in common between two sets? ``` set1 & set2 ``` ``` set1 + set2 ``` ``` set1 | set2 ``` ``` set1 ^ set2 ``` ``` set1.add(set2) ``` (order shuffled)