-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSets&Dictionaries.py
More file actions
95 lines (72 loc) · 2.08 KB
/
Copy pathSets&Dictionaries.py
File metadata and controls
95 lines (72 loc) · 2.08 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# Sets Dictionaries start from line 35
#It can be initiated with curly brackets or the set() class constructor
#It is an unordered collection of unique elements
set() # => set()
# set(3, 2, 3, 'a', 'b', 'a') # => TypeError: set expected at most 1 argument, got 6
set([3, 2, 3, 'a', 'b', 'a']) # => {3, 'b', 'a', 2}
# pop
s = {1, 2, 3}
s.pop() # => 1
print(s) # => {2, 3}
# remove
s = {1, 2, 3}
s.remove(3)
print(s) # => {1, 2}
# discard
s = {1, 2, 3}
s.discard(3)
print(s) # => {1, 2}
# clear
s = {1, 2, 3}
s.clear()
print(s) # => set()
# del
s = {1, 2, 3}
del s
# print(s) # => NameError: name 's' is not defined
sentence = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
unique_consonants = {c.lower() for c in sentence if c not in "aeiou ,."}
print(unique_consonants)
# => {'l', 'r', 'm', 's', 'd', 't', 'c', 'i', 'a', 'g', 'u', 'e', 'b', 'o', 'p', 'h'}
##########################################################
#DICTIONARIES
# keys must be in string format
# keys must be unique
# values can be any data type
# dictionaries are enclosed in curly brackets
# dictionaries are unordered
{ "key1": "value1", "key2": "value2" }
my_dict = {"key": 1, "key": 2}
my_dict["key2"] = "value2"
print(my_dict) # => {'key': 2, 'key2': 'value2'}
# creating dictionaries using dict() function
dict(x=1, y=2)
# => {'x': 1, 'y': 2}
# pop
my_dict = {"key": 1, "key": 2}
my_dict.pop("key")
print(my_dict) # => {'key': 2}
# del
my_dict = {"key": 1, "key": 2}
del my_dict
# print(my_dict) # => NameError: name 'dict' is not defined
# get
my_dict = {"key": 1, "key": 2}
my_dict.get("key")
# => 1
my_dict.get("key2")
# => None
# clear
my_dict = {"key": 1, "key": 2}
my_dict.clear()
print(my_dict) # => {}
dog = "cuddly"
dict_map = {
"hungry": "Refilling food bowl.",
"thirsty": "Refilling water bowl.",
"playful": "Playing tug-of-war.",
"cuddly": "Snuggling.",
}
# Remember that a dictionary's .get() method lets us set a default value!
owner = dict_map.get(dog, "Reading newspaper.")
print(owner) # => "Snuggling."