Sets & Dictionaries
Python
Sets
Sets do not contain duplicates
Sets are immutable
Union of two sets
Sets have no order
Sets do not support indexing
Examples
Boolean operations on sets (I)
A
B
Boolean operations on sets (II)
A
B
Boolean operations on sets (III)
A
B
Boolean operations on sets (IV)
A
B
Boolean operations on sets (V)
Boolean operations on sets (VI)
https://blog.hubspot.com/website/python-set
https://intellipaat.com/blog/tutorial/python-tutorial/data-structures-with-python-cheat-sheet/
Towards Dictionaries
45 | “Coding” | 4.5 | 7 | 89 |
0 | 1 | 2 | 3 | 4 |
Integer �Indices
Dictionaries
dic = {"NUM":45, 1000:"coding", 2000:4.5, 3.4:7, "XXX":89}
45 | “Coding” | 4.5 | 7 | 89 |
“NUM” | 1000 | 2000 | 3.4 | “XXX” |
keys of different types
Each element is a key:value pair, and elements are separated by commas
key
value
Values of different types
Dictionaries
Output: {'a': 2, 'b': 2}
dic2 = {"a":1, "a":2, "b":2}
print(dic2)
The element “a”:2 will override the element “a”:1
because only ONE element can have key “a”
Dictionaries
Output:
{'first': {1: 1}, 'second': {2: 'a'}}
{'first': [1, 2, 3], 'second': {2: 'a'}}
def func1(d):
d["first"] = [1, 2, 3]
dic = {"first":{1:1}, "second":{2:"a"}}
print(dic)
func1(dic)
print(dic)
Dictionaries
Output:
dic = {"first": 1, "second": 2, "third": 3}
for i in dic:
print(i)
How to get the values?
first
second
third
ONLY the keys will be returned.
Dictionaries
Output:
dic = {"first": 1, "second": 2, "third": 3}
for i in dic:
print(dic[i])
1
2
3
Values can be
accessed via indexing!
Adding Elements to a Dictionary
Output:
dic = {"first": 1, "second": 2, "third": 3}
print(dic)
dic["fourth"] = 4
print(dic)
{'first': 1, 'second': 2, 'third': 3}
{'first': 1, 'second': 2, 'third': 3, 'fourth': 4}
Adding Elements to a Dictionary
Output:
dic = {"first": 1, "second": 2, "third": 3}
print(dic)
dic[”second"] = 4
print(dic)
{'first': 1, 'second': 2, 'third': 3}
{'first': 1, 'second’: 4, 'third': 3}
If the key already exists, �the value will be overridden
Deleting Elements to a Dictionary
Output:
dic = {"first": 1, "second": 2, "third": 3}
print(dic)
dic["fourth"] = 4
print(dic)
del dic["first"]
print(dic)
{'first': 1, 'second': 2, 'third': 3}
{'first': 1, 'second': 2, 'third': 3, 'fourth': 4}
{'second': 2, 'third': 3, 'fourth': 4}
Deleting Elements to a Dictionary
Output:
dic = {"first": 1, "second": 2, "third": 3}
print(dic)
dic["fourth"] = 4
print(dic)
dic.pop(“first”)
print(dic)
{'first': 1, 'second': 2, 'third': 3}
{'first': 1, 'second': 2, 'third': 3, 'fourth': 4}
{'second': 2, 'third': 3, 'fourth': 4}
Dictionary Functions
Function | Description |
dic.clear() | Removes all the elements from dictionary dic |
dic.copy() | Returns a copy of dictionary dic |
dic.items() | Returns a list containing a tuple for each key-value pair in dictionary dic |
dic.get(k) | Returns the value of the specified key k from dictionary dic |
dic.keys() | Returns a list containing all the keys of dictionary dic |
dic.pop(k) | Removes the element with the specified key k from dictionary dic |
Dictionary Functions
Function | Description |
dic.popitem() | Removes the last inserted key-value pair in dictionary dic |
dic.values() | Returns a list of all the values in dictionary dic |