-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest.py
More file actions
314 lines (280 loc) · 5.14 KB
/
test.py
File metadata and controls
314 lines (280 loc) · 5.14 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
x = 1
y = 2 * 3
#if-elif-else语句
x = 4
if x == 1 :
x = 11
elif x == 2 :
x = 22
elif x == 3 :
x = 33
else :
x = 44
endif
print ( x )
x = 1
while x == 1 :
if x == 2 :
x = x + 1
endif
x = 10
endwhile
print ( x )
#for
x = 1
for i = 0 ; i < 5 ; i = i + 1 :
x = x + 1
endfor
print ( x )
#while里面嵌套if
x = 1
while x < 10 :
if x == 1 :
x = x + 2
print ( x )
continue
elif x == 3 :
x = x + 5
print ( x )
else :
x = -10
print ( x )
break
endif
x = x + 1
print ( x )
endwhile
#for里面嵌套if
for x = 1 ; x < 10 ; x = x :
if x == 1 :
x = x + 2
print ( x )
continue
elif x == 3 :
x = x + 5
print ( x )
else :
x = -1
print ( x )
break
endif
x = x + 1
print ( x )
endfor
#双重嵌套的continue和break
x = 2
while x < 5 :
for i = 0 ; i < 4 ; i = i + 1 :
if i < x :
continue
endif
y = i
print ( y )
break
endfor
x = x + 1
print ( x )
if x == 4 :
break
endif
endwhile
#函数+循环(计算乘方)
def pow ( x , n ) :
s = 1
while n > 0 :
s = s * x
n = n - 1
endwhile
return s
enddef
print ( pow ( 2 , 3 ) )
##全局变量 & 局部变量 我换成list
x = [ 1 , 2 ]
y = [ 3 , 4 ]
def fa ( x ) :
print ( x )
print ( y )
enddef
fa ( 4 )
#传递表达式 & 返回表达式
x = 1
y = 2
def fb ( x , y ) :
if x == 5 :
print ( x + y )
endif
return x * y
enddef
a = fb ( 2 * x , 2 * y )
print ( a )
#函数出现在表达式,参数,返回值中
def fs ( x ) :
return x + 1
enddef
def fp ( ) :
return 10
enddef
def fq ( x ) :
return x + fp ( )
enddef
print ( fq ( fs ( fp ( ) * 2 ) ) )
print ( fp ( ) * 2 )
print ( fq ( 1 ) )
#递归(求阶乘)
def fact ( x ) :
if x <= 1 :
return 1
else :
return x * fact ( x - 1 )
endif
enddef
b = fact ( 4 )
print ( b )
#成员函数
# def ga ( x , n ) :
# s = 1
# return s
# enddef
# x = ( 1 + obj1 . obj2 . ga ( 1 , 2 ) . t ) * ( ga ( 1 , 2 ) + 2 )
#下标运算符合成员函数的混合调用
def gb ( x , n ) :
s = 1
return s
enddef
a = gb ( b [ gb ( x , y ) ] , z )
print ( a )
# list 赋值和+ *
print ( [ ] )
x = [ ]
print ( x )
print ( [ [ 1 / 2 , 100 ] , ( 2 , 3 ) , { 4 : 5 } , 4 ] )
x = [ [ 1 / 2 , 100 ] , ( 2 , 3 ) , { 4 : 5 , 6 : 7 } , 4 ]
print ( x )
print ( ( x + [ 2 ] ) * 2 )
print ( x * 3 + [ 2 ] * 3 )
print ( x * 3 * 3 )
# list . 操作 toSet, count(i), copy, append(i), reverse, clear
list = [ 1 , 2 , 3 , 1 , 2 , 3 ]
c = list . copy ( )
print ( c )
set = list . toSet ( )
print ( list )
print ( set )
list . append ( { 1 : 23 } )
print ( list )
list . reverse ( )
print ( list )
print ( list . count ( 1 ) )
print ( list )
list . clear ( )
print ( list )
# list 取下表,越界报错
x = [ 1 , [ 2 , [ 3 , 4 , 5 ] ] , [ 6 , 7 ] ]
print ( x )
x [ 1 + 1 ] = 10 / 20 - 2
print ( x )
# tuple 对内部数据赋值报错
y = ( 1 , { 2 : 2 } , ( 3 , ) , [ 1 , 2 , 3 , 4 ] )
print ( y )
print ( y [ 3 ] )
# dict
x = { " ab " : [ 1 , { 2 : 3 } ] , 1 : ( 3 , 4 ) }
print ( x )
y = { }
print ( y )
print ( x [ " ab " ] )
x [ 1 ] = 0
print ( x )
#string
abc = " abcdefg "
print ( abc [ 2 ] )
#类
#成员变量&成员函数的调用&赋值
class Student ( ) :
def __init__ ( self , name , school ) :
self . __name = name
self . school = school
enddef
def f ( self , x ) :
print ( self . __name )
print ( x )
return 3
enddef
def __f ( self ) :
print ( 1 )
enddef
endclass
bart = Student ( 59 , 12 )
print ( bart . f ( 2 ) )
x = bart . school
print ( x )
bart . school = 87
print ( bart . school )
bart . __f ( )
bart . __name = 23 #访问私有变量报错
#嵌套调用类
#Student类的f()中创建了Dog类的对象并调用该对象的f()函数
class Dog ( ) :
def __init__ ( self , name ) :
self . name = name
enddef
def f ( self ) :
print ( self . name )
enddef
endclass
class Student ( ) :
def __init__ ( self , name , school ) :
self . __name = name
self . school = school
enddef
def f ( self ) :
dog = Dog ( 10 )
dog . f ( )
enddef
endclass
bart = Student ( 59 , 12 )
bart . f ( )
#继承&多态
#Dog类和Cat类均继承自Animal类
#Dog类继承了Animal类的构造函数,Cat类重载了Animal类的构造函数
#Dog类和Cat类继承了Animal类f()函数,各自重载了run()函数
class Animal ( ) :
def __init__ ( self , name ) :
self . name = name
enddef
def f ( ) :
print ( " f() " )
enddef
def run ( self ) :
print ( " animal " )
enddef
endclass
class Dog ( Animal ) :
def run ( self ) :
print ( " dog " )
enddef
endclass
class Cat ( Animal ) :
def __init__ ( self , name , hobby ) :
self . name = name
self . hobby = hobby
enddef
def run ( self ) :
print ( " cat " )
enddef
endclass
animal = Animal ( 10 )
dog = Dog ( 4 )
cat = Cat ( 5 , 6 )
dog . f ( )
cat . f ( )
dog . run ( )
cat . run ( )
print ( dog . name )
print ( cat . hobby )
def run ( animal ) :
animal . run ( )
enddef
run ( animal )
run ( dog )
run ( cat )