<jupyter_start><jupyter_text>Chapter 7 - SetsThis chapter will introduce a different kind of container: **sets**. Sets are lists with no duplicate entries. You might wonder why we need different kinds of containers. We will postpone that discussion until chapter 8.**At the end of this chapter, you will be able to:*** create a set* add items to a set* extract/inspect items in a set**If you want to learn more about these topics, you might find the following links useful:*** [Python documentation](https://docs.python.org/3/tutorial/datastructures.htmlsets)* [A tutorial on sets](https://www.learnpython.org/en/Sets) If you have questions about this chapter, please send an email to <EMAIL>  7.1 How to create a setIt's quite simple to create a set<jupyter_code>a_set = {1, 2, 3}
a_set
empty_set = set() # you have to use set() to create an empty set!
print(empty_set)<jupyter_output><empty_output><jupyter_text>* Sets are surrounded by curly brackets and the elements in the set are separated by commas* A set can be empty (use set() to create it)* Sets do not allow **duplicates*** sets are unordered (the order in which you add items is not important)* A set can **only contain immutable objects** (for now that means only **strings** and **integers** can be added)* A set can not contain **mutable objects**, hence no lists or sets Please note that sets do not allow **duplicates**. In the example below, the integer **1** will only be present once in the set.<jupyter_code>a_set = {1, 2, 1, 1}
print(a_set)<jupyter_output><empty_output><jupyter_text>Please note that sets are **unordered**. This means that it can occur that if you print a set, it looks different than how you created it<jupyter_code>a_set = {1, 3, 2}
print(a_set)<jupyter_output><empty_output><jupyter_text>Please note that sets can **only contain immutable objects**. Hence the following examples will work, since we are adding immutable objects<jupyter_code>a_set = {1, 'a'}
print(a_set)<jupyter_output><empty_output><jupyter_text>But the following example will result in an error, since we are trying to create a set with a **mutable object**<jupyter_code>a_set = {1, []}<jupyter_output><empty_output><jupyter_text>7.2 How to add items to a setthe most common way of adding an item to a set is by using the **add** method. The **add** method takes one positional argument, namely what you are going to add to the set and it returns None.<jupyter_code>a_set = set()
a_set.add(1)
print(a_set)
a_set = set()
a_set = a_set.add(1)
print(a_set)<jupyter_output><empty_output><jupyter_text>7.3 How to extract/inspect items in a set Since sets are **unordered**, you can **not** use an index to extract an element from a set.<jupyter_code>a_set = set()
a_set.add(1)
a_set.add(2)
a_set[0]<jupyter_output><empty_output><jupyter_text>Sets can be used to carry out mathematical set operations like union, intersection, difference and symmetric difference. Please take a look at [this website](https://www.programiz.com/python-programming/set) if you prefer a more visual and more complete explanation. Here we explain the two most common methods.<jupyter_code>set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}<jupyter_output><empty_output><jupyter_text>The **union** method takes one positional argument (a set) and returns the union of two sets.<jupyter_code>help(set.union)
set1.union(set2)<jupyter_output><empty_output><jupyter_text>The **intersection** method takes one positional argument (a set) and returns a new set containing only the intersection of the two sets.<jupyter_code>help(set.intersection)
set1.intersection(set2)<jupyter_output><empty_output><jupyter_text>7.4 Using built-in functions on setsThe same range of functions that operate on lists also work with sets. We can easily get some simple calculations done with these functions:<jupyter_code>nums = {3, 41, 12, 9, 74, 15}
print(len(nums)) # number of items in a set
print(max(nums)) # highest value in a set
print(min(nums)) # lowest value in a set
print(sum(nums)) # sum of all values in a set<jupyter_output><empty_output><jupyter_text>7.5 An overview of set operationsThere are many more operations which we can perform on sets. Here is an overview of some of them.In order to get used to them, please call the **help** function on each of them (e.g. help(set.union)). This will give you the information about the positional argument, keyword arguments, and what is returned by the method.<jupyter_code>a = {1, 2, 3}
b = 4
c = {4, 5, 6}

#do some operations 
a.add(b)        # Add item b to a
a.update(c)     # Add the elements of set c to a
a.pop()         # Remove and return an arbitrary set element.
a.remove(b)     # Remove b from set a<jupyter_output><empty_output><jupyter_text>You can use the **dir** built-in function to see an overview of all set methods:<jupyter_code>dir(set)<jupyter_output><empty_output><jupyter_text>7.6 Set exercises Please create an empty set and use the **add** method to add four items to it: 'a', 'set', 'is', 'born' Please use a built-in method to **count** how many items your set has How would you **remove** one item from the set? Please check which items are in both sets?<jupyter_code>set_1 = {'just', 'some', 'words'}
set_2 = {'some', 'other', 'words'}
# your code here<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09941298996402197, 'fraction_numerical': 0.012876349176292368, 'mean_word_length': 5.356197352587245, 'pattern_counts': {'":': 0, '<': 58, '<?xml version=': 0, '>': 58, 'https://': 3, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '657562', 'license': 'Apache-2.0', 'n_tokens_mistral': 1672, 'n_tokens_neox': 1618, 'n_words': 706, 'path': 'Chapters/Chapter 7 - Sets.ipynb', 'repo_name': 'unmeshvrije/python-for-beginners', 'size': '5290'}