-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathList vs Tuple vs Dictionary vs Set.py
63 lines (48 loc) · 1.79 KB
/
List vs Tuple vs Dictionary vs Set.py
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
# List [] vs Tuple () vs Dictionary vs Set{}
# List [] – add, remove, and change data ['apple', 'banana', 'cherry']
# Tuple () – once created, cannot change in size or content i.e. immutable i.e. coordinate(x,y,z) ('apple', 'banana', 'cherry')
# Set {} - is unordered and has no duplicate values {'apple', 'banana', 'cherry'}
# Dictionary{:} keys, not ordered data(aka array,map) {'name':'apple, 'color':'green'}
# Below is an example of a list with square brackets
earth_metals = ['Beryllium', 'Magnesium', 'Calcium', 'Strontium', 'Barium', 'Radium']
earth_metals.sort()
print(earth_metals)
for i in earth_metals:
print(i)
print('\nMixed list examples of int, float, string, bool ')
spam = [444, 3.1415,'hello', True, None, 42]
for i in spam:
print(i)
# Below is an example of a tuple with round brackets
earth_metals = ('Beryllium', 'Magnesium', 'Calcium', 'Strontium', 'Barium', 'Radium')
print('\nExample of tuple ', earth_metals)
try:
earth_metals.sort()
except AttributeError:
print('Error: can not sort immutable tuple.\n')
example = set()
example.add(33)
example.add(False)
example.add(3.1415)
print('Set example',example, type(example))
example.remove or example.discard
example2 = set([28, True, 2.27928, "Helium"])
print(example2, type(example2))
diction = {'userid': 3203, 'message': 'hello world!', 'language': 'English'}
print('\nDictionary example', diction)
#Method 1:
if 'location' in diction:
print(diction['location'])
else:
print('Diction does not contain a location value.')
# Method 2:
# Try:
# Print(diction['location']
# Except KeyError:
# print('Diction does not contain a location value.')
# Method 3:
Loc = diction.get('message', None)
print('example3 ',Loc)
Loc = diction.get(None, 'English')
print('example4 ',Loc +'\n')
#