-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfive.py
More file actions
55 lines (43 loc) · 1.68 KB
/
five.py
File metadata and controls
55 lines (43 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# -----------------------------------------------DICTIONARY & SETS-------------------------------------------------------
d = {} # Empty dictionary
marks = {
"John": 100,
"Shubham": 56,
"Rohan": 23,
0: "John",
}
print(marks, type(marks))
print(marks["John"])
# -----------------------------------------------PROPERTIES OF PYTHON DICTIONARIES-------------------------------------------------------
# 1. It is unordered.
# 2. It is mutable.
# 3. It is indexed.
# 4. Cannot contain duplicate keys.
# -----------------------------------------------DICTIONARY METHODS-------------------------------------------------------
print(marks.items())
print(marks.keys())
print(marks.values())
marks.update({"John": 99, "Renuka": 100})
print(marks)
print(marks.get("John2")) # Prints None
# print(marks["John2"]) # Returns an error KeyError: 'John2'
# -----------------------------------------------SETS IN PYTHON-------------------------------------------------------
e = set() # Dont use s = {} as it will create an empty dictionary
s = {1, 5, 32, 54,5, 5, 5, "John"}
print(type(e))
print(s)
# -----------------------------------------------PROPERTIES OF SETS-------------------------------------------------------
# 1. Sets are unordered => Element's order doesn't matter
# 2. Sets are unindexed => Cannot access elements by index
# 3. There is no way to change items in sets.
# 4. Sets cannot contain duplicate values.
# -----------------------------------------------OPERATIONS ON SETS-------------------------------------------------------
print(s, type(s))
s.add(566)
print(s, type(s))
s.remove(1)
print(s, type(s))
s1 = {1, 45, 6, 78}
s2 = {7, 8, 1, 78}
print(s1.union(s2))
print(s1.intersection(s2))