-
Notifications
You must be signed in to change notification settings - Fork 0
/
tutorial.py
208 lines (147 loc) · 3.05 KB
/
tutorial.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# tutorial.py
# Data structures
# List/tuple and dics
list_ex = ["Pinapple", "pen", "apple"]
tuple_ex = (1,5)
dict_ex = {"key1": 1, "key2": 2}
# List Comprehensions
squares = []
for x in range(10):
squares.append(x**2)
print(squares)
squares = [x**2 for x in range(10)]
# Functional tools
a = map(lambda x: x*x*x, range(10))
print(a)
# with list comprehension
a = [x*x*x for x in range(10)]
print(a)
zip_list = zip(list_ex, range(3))
print(zip_list)
filtered_list = filter(lambda x: "n" in x, list_ex)
print(filtered_list)
#with list comprehension
filtered_list = [x for x in list_ex if "n" in x]
print(filtered_list)
reduced = reduce(lambda x,y: x+y, [1,2,3,4,5])
print(reduced)
print("Is there any pen in list: " + str(any([x for x in list_ex if "pen" == x])))
print("All elements are pen: " + str(all(["pen" == x for x in list_ex])))
# args and kwargs
def test(*args, **kwargs):
print(args)
print(kwargs)
test("I am an arg", keyword="I am a kwarg")
params = ("I am an arg", )
kparams = {
"keyword" : "I am a kwarg"
}
test(*params, **kparams)
#Default params
def test2(num, mem=[]):
[mem.append(x) for x in range(num)]
print mem
test2(3)
test2(3, [])
test2(3)
def test3(num, mem=None):
if not mem:
mem = []
[mem.append(x) for x in range(num)]
print mem
test3(3)
test3(3, [])
test3(3)
def fib(n, mem={}):
if n <= 2:
return 1
else:
val = mem.get(str(n-1), fib(n-1)) + mem.get(str(n-2), fib(n-2))
mem[str(n)] = val
return val
print(fib(20))
# Classes
class Player(object):
def __init__(self, life, name):
self.life = life
self.name = name
def hit(self, damage):
self.life -= damage
def say_hi(self):
print(self.name)
class Hero(Player):
def __init__(self, sword):
super(Hero, self).__init__(10, "TheHero")
self.sword = sword
def attack(self, enemy):
enemy.hit(self.sword.damage)
class Sword(object):
def __init__(self, damage):
self.damage = damage
weapon = Sword(5)
hero = Hero(weapon)
enemy = Player(15, "Ruggero")
hero.say_hi()
hero.attack(enemy)
print(enemy.life)
class A(object):
def go(self):
print("go A go!")
def stop(self):
print("stop A stop!")
def pause(self):
raise Exception("Not Implemented")
class B(A):
def go(self):
super(B, self).go()
print("go B go!")
class C(A):
def go(self):
super(C, self).go()
print("go C go!")
def stop(self):
super(C, self).stop()
print("stop C stop!")
class D(B,C):
def go(self):
super(D, self).go()
print("go D go!")
def stop(self):
super(D, self).stop()
print("stop D stop!")
def pause(self):
print("wait D wait!")
class E(B,C): pass
a = A()
b = B()
c = C()
d = D()
e = E()
# specify output from here onwards
a.go()
b.go()
c.go()
print(D.mro())
d.go()
e.go()
a.stop()
b.stop()
c.stop()
d.stop()
e.stop()
a.pause()
b.pause()
c.pause()
d.pause()
e.pause()
###############
class M(object):
a = 3
class P(M):
b = 1
print M.a
print P.a
M.a = 4
print P.a
P.a = 5
print M.a