-
Notifications
You must be signed in to change notification settings - Fork 0
/
type_lab.py
281 lines (234 loc) · 4.25 KB
/
type_lab.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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#number
print 2 ** 100
print 1.5 * 4
print len(str(2 ** 100000))
3.1415 * 2
print (3.1415 * 2)
import math
print math.pi
print math.sqrt(4)
import random
print random.random()
print random.choice([1, 2, 3, 4])
S = 'Spam'
print len(S)
print S[0]
print S[3]
print S[-1]
print S[-2]
print S[1:3]
print S[:]
print S + 'xyz'
print S * 8
#str only support over rewrite
S = 'z' + S[1:]
print S
strList = "aaa,bbb,cccc,dd"
list = strList.split(',')
print list
print list[1:]
#str method
S = S.upper()
print S
isAl = S.isalpha()
print isAl
isDigit = S.isdigit();
print isDigit
line = 'aaa,bbb,ccc,dd\n'
line = line.rstrip()
print line * 2
#str format
str = '%s,eggs,and %s' %('cluo','chunling')
print str
str = '{0} eggs , and {1} chicken'.format('1','3')
print str
#check method of object
methodlist = dir(str)
print methodlist
print __name__
doc = help(str.replace)
#break line """" <pre></pre>
msg = """ cluo
cluo1
cluo2
cluo3
cluo4"""
fileObj = open('cluo','w')
fileObj.write(msg)
fileObj = open('cluo','r')
arr = []
for eachLine in fileObj:
arr.append(eachLine)
str = 'a'.join(arr)
fileObj = open('cluo','w')
fileObj.write(str)
#regex
import re
match = re.match('Hello\s+(.*)world','Hello python world')
inner = match.group(1);
print inner
match = re.match('/(.*)/(.*)/(.*)','/usr/home/bbq')
groups = match.groups()
print groups
print groups[0:]
#list
l = [123, 'spam', 1.23]
s = 'string'
print len(l)
print len(s)
print l[-1]
print l[0:-1]
print l + [4, 5, 6]
132
#list method
l.append('NI')
print l
l.sort();
print l
l.reverse();
print l
m = [
[1, 2, 3],
[4, 5, 6],
[7, 8 ,9]
]
print m
print m[1];
print m[1][0]
#list parse method (pre result_function)
cols2 = [row[1] for row in m]
print cols2;
cols3 = [row[1] + 1 for row in m]
print cols3
#list parse method (add filter_function) map
cols4 = [row[1] + 1 for row in m if row[1] % 2 == 0]
print cols4
#list numpy filter
diag = [m[i][i] for i in [0, 1 ,2]]
print diag
doubles = [c * 2 for c in 'spam']
print doubles
G = (sum(row) for row in m)
print G
print next(G)
print next(G)
print next(G)
list = [ord(x) for x in 'spaam']
print list
list = {ord(x) for x in 'spaam'}
print list
list = {x:ord(x) for x in 'spaam'}
print list
D = {'food':'spam', 'quantity':4, 'color':'pink'}
print D['food']
print D['quantity'] +1
D = {}
D['name'] = 'Bob'
D['job'] = 'dev'
D['age'] = 40
print D
rec ={
'name':{'first': 'Bob', 'last': 'Smith'},
'job':['dev', 'mgr'],
'age': 40.5
}
print rec['name']
print rec['name']['last']
print rec['job'][-1]
rec['job'].append('janitor')
print rec
print D.keys()
list = D.keys()
list.sort() #return none????
print list
#auto sorted
D ={'b':2,'a':1,'c':3}
for item in D:
print(item,'=>',D[item])
for item in sorted(D): #sorted key
print(item,'=>',D[item])
for c in 'spam':
print(c.upper()),
x = 4
while x > 0:
print('spam!' * x)
x -= 1
#TIME TIMEIT PROFILE
squares = []
for x in [1, 2, 3, 4, 5]:
squares.append(x ** 2)
print squares
squares = [x ** 2 for x in [1, 2, 3, 4 ,5]]
print squares
#key test time timeit profile
if not 'f' in D:
print('missing')
value = D.get('x', 'b')
print value
value = D['x'] if 'x' in D else 0
print value
#tuple
T = (1, 2, 3, 4)
print len(T)
T += (5, 6)
print T
#T appears once
print T.count(4)
print T.index(4)
T = ('spam', 3.0, [11, 22 ,33])
print T[1]
print T[0]
#tuple could not change
#T.append(4)
#print T
f = open('cluo_bb','w')
f.write('Hello\n')
f.write('world\n')
f.close()
f = open('cluo_bb')
content = f.read();
print content
list = content.split()
print list
#dir seek
doc = dir(f)
print doc
help(f.seek)
X = set('spam')
Y = {'h', 'a', 'm'}
print X, Y
#intersection
print X & Y
#union
print X | Y
#diff
print X - Y
list = {x ** 2 for x in [1, 2, 3, 4]}
print list
#decimal fixed precision
import decimal
d = decimal.Decimal('3.141')
print d + 1
#initrialize name obj
X = None
print(X)
L = [None] * 100
print L
print type(L)
#type
# if type(L) == type([]):
# print('yes')
# if type(L) == list:
# print('yes')
# if isinstance(L,list):
# print('yes')
class Worker:
def __int__(self, name, pay):
self.name = name
self.pay = pay
def lastName(self):
return self.name.split()[-1]
def giveRaise(self, percent):
self.pay *= (1.0 + percent)
bob = Worker('bob cluo',5000)
print bob.lastName();