forked from ganml/dcpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython101.py
More file actions
325 lines (271 loc) · 6.16 KB
/
python101.py
File metadata and controls
325 lines (271 loc) · 6.16 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
import math
pi = 3.1415926
Pi = 3.141592653589793
print(pi)
print(Pi)
import keyword
print(keyword.kwlist)
bA = True
iB = 2024
fC = 3.14
sD = 'data'
type(bA)
type(iB)
type(fC)
type(sD)
x, y = 1, 2
x
y
x, y = y, x
print(x)
print(y)
z = 3.14
x, y, z = 1, 2, 3
print(x)
print(y)
print(z)
x, y, z = y, z, x
print(x)
print(y)
print(z)
s1 = "a string"
len(s1)
s1[0:2]
s1[-1]
s1[-3:-1]
s1[-3:]
# string operators and functions
print("a " + "string")
print("a" * 5)
print("string"[0:3])
print("str" in "string")
print("string".replace("s", "S"))
print("1,2,3".split(","))
print("{} and {} are the results".format(1, 2))
print(",".join(["1", "2", "3"]))
# data structures
lst1 = ["a", 1, True]
print(lst1)
print(lst1[0])
lst1.append(3.14)
print(lst1)
lst1.insert(0, 3.14)
print(lst1)
print(lst1[0]) # get the first element
print(lst1[-1]) # get the last element
print(lst1[1:3]) # get the second and the third elemnts
print(lst1[-3:]) # get the last three elements
print(lst1[1:]) # get all elements except the first one
print(lst1[:3]) # get the first three elements
print(lst1[::-1]) # get elements with indices -1, -2, ...
print(lst1[0:3:2]) # get elements 0, 2
print(lst1[-2:-4:-2]) # get element -2
lst1[0:2] = [-1, -2]
print(lst1)
t1 = (1, 3)
t2 = ("a", True, 1)
print(t1)
print(t2)
print(t1[1])
print(t2[1:3])
m1 = {"a" : 1, "b" : 2}
print(m1)
print(m1["a"])
print(m1["b"])
print(m1.keys())
print(m1.values())
print(m1.items())
print(list(m1.keys()))
print(list(m1.values()))
print(list(m1.items()))
s1 = set()
s1.add(1)
s1.add("a")
s2 = {1, "a", "b", True, False}
print(s1)
print(s2)
s2.discard(2)
s2.remove(2)
# arithmetic operators
print("2 + 3 =", 2 + 3)
print("2 - 3 =", 2 - 3)
print("2 * 3 =", 2 * 3)
print("2 / 3 =", 2 / 3)
print("2 ** 3 =", 2 ** 3)
print("3.5 % 0.6 =", 3.5 % 0.6)
print("3.5 // 0.6 =", 3.5 // 0.6)
# assignment operators
x = 3.5
print("x =", x)
x += 0.6
print("x += 0.6:", x)
x -= 0.6
print("x -= 0.6:",x)
x *= 0.6
print("x *= 0.6:",x)
x /= 0.6
print("x /= 0.6:",x)
x %= 0.6
print("x %= 0.6:",x)
x //= 0.6
print("x //= 0.6:",x)
# comparison operators
print("1 == 2:", 1 == 2)
print("1 != 2:", 1 != 2)
print("1 < 2:", 1 < 2)
print("1 > 2:", 1 > 2)
print("1 <= 2:", 1 <= 2)
print("1 >= 2:", 1 >= 2)
print("3.5 % 0.6 > 0.5:", 3.5 % 0.6 > 0.5)
print("3.5 % 0.6 > 0.5:", 3.5 % 0.6 > 0.5 + 1e-10)
# logical operators
print("1 < 2 and 1 > 2: ", 1 < 2 and 1 > 2)
print("1 < 2 or 1 > 2: ", 1 < 2 or 1 > 2)
print("not 1 < 2:", not 1 < 2)
# identity and membership operators
x = 1
y = [1, 2, 3]
z = [1, 2, 3]
print("x is y:", x is y)
print("x is not y:", x is not y)
print("y is z:", y is z)
print("x in y:", x in y)
print("x not in y:", x not in y)
print("y in z", y in z)
# control statements and loops
x = 1
if x > 0:
print("x is positive")
y = 2
if x > y:
print("x is greather than y")
else:
print("x is not greater than y")
if y > 2:
print("y is greater than 2")
elif y == 2:
print("y is equal to 2")
else:
print("y is less than 2")
# for loop to calculate 1 + 2 + ... + 10
dSum = 0
for i in range(1, 11):
dSum += i
print(dSum)
# while loop to calculate 1 + 2 + ... + 10
dSum = 0
i = 0
while i < 10:
i += 1
dSum += i
print(dSum)
# nest if in for loop
dSum = 0
for i in range(1, 1001):
dSum += i
if i % 100 == 0:
print("Iteration {}: {}".format(i, dSum))
# while loop with break
dSum = 0
i = 0
while True:
i += 1
dSum += i
if i >= 10:
break
print(dSum)
# for loop with continue
dSum = 0
for i in range(1, 11):
if i % 2 == 1:
continue
dSum += i
print(dSum)
# function
def sqrt(x):
if x < 0:
return("{} is negative".format(x))
else:
return(math.sqrt(x))
print(sqrt(-1))
print(sqrt(2))
# lambda function
add = lambda x, y: x+y
print(add(1,2))
x = [(lambda x: math.sqrt(x))(i) for i in range(1,11)]
print(x)
# read and write files
import os
fi = open(os.path.join(r"c:\users\gjgan\documents\researchu\book\dcpython\code", "file.txt"), "r")
content = fi.read()
fi.close()
print(content)
with open(os.path.join(r"c:\users\gjgan\documents\researchu\book\dcpython\code", "file.txt"), "r") as fi:
content = fi.read()
print(content)
with open(os.path.join(r"c:\users\gjgan\documents\researchu\book\dcpython\code", "file.txt"), "r") as fi:
content = fi.readlines()
print(content)
with open(os.path.join(r"c:\users\gjgan\documents\researchu\book\dcpython\code", "integers.txt"), "w") as fi:
fi.write(",".join([str(i) for i in range(1,6)]))
fi.write("\n")
fi.write(",".join([str(i) for i in range(6,11)]))
# error handling
x = -2
try:
print(math.sqrt(x))
except ValueError as e:
print(str(e))
def sqrt2(x):
if not isinstance(x, (int, float)):
raise ValueError("{} is not numeric".format(x))
elif x < 0:
raise ValueError("{} is negative".format(x))
else:
return(math.sqrt(x))
for x in [-2, 2, "a"]:
try:
print(sqrt2(x))
except ValueError as e:
print(str(e))
# object-oriented programming
from abc import abstractmethod
class Algorithm:
def __init__(self, name):
self.name = name
@abstractmethod
def fit(self):
pass
class Kmean(Algorithm):
def fit(self):
return("Clustering by kmeans")
class Kmode(Algorithm):
def fit(self):
return("Clustering by kmodes")
alg1 = Kmean("algorithm 1")
alg2 = Kmode("algorithm 2")
print(alg1.fit())
print(alg2.fit())
print(alg1.name)
print(alg2.name)
# code optimization
def fibo(n):
if n == 1 or n == 2:
return(1)
else:
return(fibo(n-1) + fibo(n-2))
import time
beg = time.time()
print(fibo(40))
end = time.time()
print("Execution time: {:.4f} seconds".format(end - beg))
import fibonacci
beg = time.time()
print(fibonacci.fibo(40))
end = time.time()
print("Execution time: {:.4f} seconds".format(end - beg))