-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheigth.py
76 lines (55 loc) · 1.7 KB
/
eigth.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
64
65
66
67
68
69
70
71
72
73
74
75
76
def fun(a):
if a > 30:
return 3
else:
print(a)
return a + fun(a + 3)
print(fun(25))
# example of a dictionary with a list of values for the key
school_class = {}
while True:
name = input("Enter the student's name (or type exit to stop): ")
if name == 'exit':
break
score = int(input("Enter the student's score (0-10): "))
if name in school_class:
school_class[name] += (score,)
else:
school_class[name] = (score,)
for name in sorted(school_class.keys()):
adding = 0
counter = 0
for score in school_class[name]:
adding += score
counter += 1
print(name, ":", adding / counter)
# d3 from d1 and d2
d1 = {'Adam Smith':'A', 'Judy Paxton':'B+'}
d2 = {'Mary Louis':'A', 'Patrick White':'C'}
d3 = {}
for item in (d1, d2):
d3.update(item)
print(d3)
# you can convert sequences into tuples
l = ["car", "Ford", "flower", "Tulip"]
t = tuple(l)
print(t)
# you can also convert a key-value pairs of a sequence into a dictionary
colors = (("green", "#008000"), ("blue", "#0000FF"))
# convert tuple to dictionary - approach 1 - think about the cons for this approach . . .
colDict = {}
for item in colors:
if item not in colDict.keys():
colDict[item[0]] = item[1]
# convert tuple to dictionary - approach 2
colDict = dict(colors)
print(colDict)
# The items method returns a tuple of the key value pairs inside the dictionary
colors = {
"white" : (255, 255, 255),
"grey" : (128, 128, 128),
"red" : (255, 0, 0),
"green" : (0, 128, 0)
}
for col, rgb in colors.items():
print(col, ":", rgb)