Repository navigation
Expand file tree
/
Copy pathbasic.py
More file actions
263 lines (217 loc) · 5.81 KB
/
Copy pathbasic.py
File metadata and controls
263 lines (217 loc) · 5.81 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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import sys
def divline():
print("\033[32m--------------------------------------D.I.V.I.D.I.N.G.L.I.N.E-----------------------------------------\033[0m")
'''
The following tokens are operators:
+ - * ** / // %
<< >> & | ^ ~
< > <= >= == != <>
The following identifiers are used as reserved words, or keywords of the language, and cannot be used as ordinary identifiers. They must be spelled exactly as written here:
and del from not while
as elif global or with
assert else if pass yield
break except import print
class exec in raise
continue finally is return
def for lambda try
'''
'''
double precision
https://en.wikipedia.org/wiki/Double-precision_floating-point_format
'''
def testFloat():
a = float(1.0 / 81)
b = float(0.0)
for i in range(729):
b += a;
print("%.15g" % (b)); # *NOT* 9.00002288818359 in C/C++
testFloat()
print(complex(1, 1))
divline()
x = int(2) # 00000010
y = int(3) # 00000011
#------------------------------------------------------------------------------
print(x | y) # OR 00000011
print(x ^ y) # XOR 00000001
print(x & y) # AND 00000010
print(x << y) # LS 00010000
print(x >> 1) # RS 00000001
'''
Two's complement
https://en.wikipedia.org/wiki/Two%27s_complement
'''
print(~x) # INV 11111101 <- 11111100 + 1 <- NEG(00000011)
'''
Signed number representations
https://en.wikipedia.org/wiki/Signed_number_representations
'''
n = int(-37)
print(bin(n)) # 10100101
print(n.bit_length())
m = int(-1)
print(bin(m)) # 11111111
print(m.bit_length())
divline()
print(x ** y) # POW
print(x // y) # MOD
divline()
lists = []
for i in range(13):
lists.append(i)
lists.append(12)
lists.append(12)
print(lists)
print(set(lists))
lists.reverse()
print(lists)
lists.sort()
print(lists)
lists.pop()
print(lists)
lists.pop()
print(lists)
print(len(lists))
lists[:] = []
print(lists)
matrix = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
]
print(matrix)
divline()
tup = ('Python', 83, ('Linux', 'KDE'))
print(tup)
print(tup * 3)
divline()
sets = {'Martin Gräßlin', 'David Faure', 'Sebastian Kügler'}
print(sets)
a = set('abracadabra')
b = set('alacazam')
print(a)
print(b)
print(a - b)
print(a | b)
print(a & b)
print(a ^ b)
divline()
dict = {'Name': 'Martin Gräßlin', 'Age': 27, 'Project': 'KWin'}
print(dict)
dict['Age'] = 21
print(dict)
print(dict.has_key('Home'))
dict['Home'] = 'Germany'
print(dict.has_key('Home'))
for k, v in dict.items():
print(k, v)
divline()
if __name__ == "__main__":
print("The if statement is used for conditional execution")
else:
print("Being imported from another module")
divline()
i = 3
while i:
if i == 1:
break
print("The while statement is used for repeated execution as long as an expression is true")
i = i - 1
divline()
def generator(r=3):
for i in range(r):
if i == 0:
continue
print("The for statement is used to iterate over the elements of a sequence (such as a string, tuple or list) or other iterable objec")
yield i * i
for i in generator():
print(i)
divline()
try:
1 / 0
except:
print("The try statement specifies exception handlers and/or cleanup code for a group of statements")
finally:
print("If finally is present, it specifies a ‘cleanup’ handler")
divline()
try:
with open("/dev/input/mice", 'r') as fptr:
pass
except:
print("The with statement is used to wrap the execution of a block with methods defined by a context manager")
divline()
# Replacements for switch statement
c = "c"
if c == 'a':
print("case 'c':")
elif c == 'b':
print("case 'b':")
else:
print("default:")
def func(c=None):
return {
'a': "case 'a':",
'b': "case 'b':",
}.get(c, "default:")
print(func("c"))
divline()
class obj(object):
def __init__(self):
print(sys._getframe().f_code.co_name)
def __del__(self):
print(sys._getframe().f_code.co_name)
obj1 = obj()
del obj1
obj1 = None
divline()
class Father(object):
__mAttr = 100
def __init__(self):
print("\033[31mCalling father constructor\033[0m")
def __del__(self):
print("\033[31mCalling father destructor\033[0m")
def fatherMethod(self):
print("\033[31mCalling father method\033[0m")
def setAttr(self, attr):
self.__mAttr = attr
def getAttr(self):
print("\033[31mCalling father getAttr %d\033[0m" % (self.__mAttr))
def myMethod(self):
print("\033[31mCalling father method\033[0m")
class Mother(object):
__mAttr = 101
def __init__(self):
print("\033[31mCalling mother constructor\033[0m")
def __del__(self):
print("\033[31mCalling mother destructor\033[0m")
def motherMethod(self):
print("\033[31mCalling mother method\033[0m")
def setAttr(self, attr):
self.__mAttr = attr
def getAttr(self):
print("\033[31mCalling mother getAttr %d\033[0m" % (self.__mAttr))
def myMethod(self):
print("\033[31mCalling mother method\033[0m")
class Child(Father, Mother):
def __init__(self, a=0, b=0):
self.a = a
self.b = b
print("Calling child constructor")
def __add__(self, r):
return Child(self.a + r.a, self.b + r.b)
def __str__(self):
return 'Child (%d, %d)' % (self.a, self.b)
def childMethod(self):
print("Calling child method")
def myMethod(self):
print("Calling child method")
c = Child()
c.childMethod()
c.fatherMethod()
c.setAttr(300)
c.getAttr()
c.myMethod()
c1 = Child(2, 10)
c2 = Child(5, -2)
print(c1 + c2)