<jupyter_start><jupyter_text>SetsSets are - along with dictionaries, lists and tuples - container-like objects. Sets are efficient to store a grouping (or set) of unique values, with no duplicates. Like dictionaries, sets are efficient to check if a value is or is not contained.<jupyter_code>brands = {"BMW", "Mercedes", "Renault", "Audi", "VW"}
print(brands)
type(brands)<jupyter_output><empty_output><jupyter_text>1. Operations<jupyter_code>print("BMW" in brands)
print("Tesla" in brands)<jupyter_output>True
False<jupyter_text>2. Methods<jupyter_code>#add a value
brands.add("Porsch")
print(brands)
#join two sets together
places1 = {"Paris", "Marseilles", "London", "New York", "Chicago"}
places2 = {"Paris", "Singapore", "Barcelona"}

union = places1.union(places2)
print(union)
#intersection 
inter = places1.intersection(places2)
print(inter)
#difference
diff = places1.difference(places2)
print(diff)

diff = places2.difference(places1)
print(diff)
#Check if all elements are in other set
print({"Paris", "London"}.issubset(places1))
print({"Paris", "London"}.issubset(places2))
#remove an element from a set
places1.remove("Chicago")
print(places1)
#.remove() will throw an error if the element you are trying to remove doesn't exist in the set
places1.remove("San Francisco")
#remove and return an arbitrary set element.
#this is arbitrary given that sets have no order
lucky = places1.pop()
print(lucky)
print(places1)<jupyter_output>New York
{'London', 'Marseilles', 'Paris'}<jupyter_text>3. Sets are iterable<jupyter_code>countries = {"France", "Germany", "UK", "Spain", "Italy"}

for country in countries: 
    print(country)
#Because sets are iterable, you can pass sets to the enumerate() function
for i, country in enumerate(countries): 
    print(i, country)<jupyter_output>0 Germany
1 Spain
2 Italy
3 France
4 UK<jupyter_text>4. Type castingYou can convert sets to lists, strings to sets, sets to strings, tuples to sets... even the keys of a dictionary!<jupyter_code>#lists to sets
a = set([1,2,3,4,3,2,1])
print(a)
#sets to lists
b = list(countries)
print(b)
#strings to sets
letters = set("Cats are sophisticated, dogs are not")
print(letters)
#dictionary keys (not values) to sets
card = {"name":"David", "age":27, "happy":True}
print(set(card))<jupyter_output>{'name', 'happy', 'age'}
{'fraction_non_alphanumeric': 0.13209181463837158, 'fraction_numerical': 0.015158077089649198, 'mean_word_length': 6.598684210526316, 'pattern_counts': {'":': 3, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '443652', 'license': 'MIT', 'n_tokens_mistral': 817, 'n_tokens_neox': 764, 'n_words': 250, 'path': '14 - Sets.ipynb', 'repo_name': 'dschenck/Python-crash-course', 'size': '2309'}