-
Notifications
You must be signed in to change notification settings - Fork 0
/
基本语句
352 lines (287 loc) · 5.88 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#######################################
###############基本语句################
#######################################
赋值
>>> nudge=1
>>> wink=2
>>> A,B=nudge,wink # 元组
>>> A,B
(1, 2)
>>> [C,D]=[nudge,wink] # 列表
>>> C,D
(1, 2)
>>> nudge,wink = wink, nudge # 元组:交换值
>>> nudge,wink
(2, 1)
表达式
print语句
>>> print 'hello world'
hello world
>>> 'hello world'
'hello world'
>>> import sys
>>> sys.stdout.write('hello world\n')
hello world
if语句
>>> if 1:
... print 'true'
...
...
true
>>> if not 1:
... print 'true'
... else:
... print 'false'
...
false
>>>
>>> choice='ham'
>>> print{'spam':1.25, # 一个基于字典的'switch'
... 'ham':1.99, # 默认用has_key()条件
... 'eggs': 0.99,
... 'bacon': 1.10}[choice]
>>> x='killer rabbit'
>>> if x=='oger': # 复合语句 首行以":"缩进语句
... print "how's jessica?" # 一个或多个首行缩进的嵌套语句,这些缩进的语句称之为块(block)或套(suite)
... elif x=='bugs':
... print "what's up doc?"
... else:
... print 'run away!run away!'
...
run away!run away!
----语句的界定----
>>> if (a==b and # 一句语句两行 可以用括号)
... c==d):
... print 'haha'
...
haha
>>> a=1
>>> b=1
>>> c=2
>>> d=2
>>> if a==b \ # 一句语句两行 可以用\
... and c==d:
... print 'ok'
...
ok
>>> x=1;y=2;print x #不是一句简单句
>>> 2<3,3<2
(True, False)
>>> 2 or 3
2
>>> 2 or 3, 4 or 5 # 如果真则返回左边的操作符
(2, 4)
>>> [] or 3
3
>>> [] or []
[]
while循环
一般格式
while <条件>: # 循环测试
<语句1> # 循环体
else: # 可选的else
<语句2> # 如果不是因为break而退出循环运行
>>> while 1:
... print 'Type Ctrl-C to stop me!'
Type Ctrl-C to stop me!
Type Ctrl-C to stop me!
Type Ctrl-C to stop me!
Type Ctrl-C to stop me!
Type Ctrl-C to stop me!
Type Ctrl-C to stop me!
Type Ctrl-C to stop me!^C
>>> x= 'spam'
>>> while x:
... print x
... x=x[1:] # 从x中取出第一个字符
...
spam
pam
am
m
>>> a=0;b=10
>>> while a < b:
... print a,
... a=a+1
...
0 1 2 3 4 5 6 7 8 9
----break continue pass else循环----
break 跳出最近的包围它的循环(通过整个循环语句)
continue 跳至最近的包围它的循环顶部(到循环的首行)
pass 什么都不做:只是一个占位的空语句。
else循环 运行并且只有在循环正常退出的情况下运行。
一般循环格式
while<条件测试>:
<语句>
if<条件测试>:break #现在跳出循环,忽略else
if<条件测试>:continue #现在转到循环顶部
else:
<语句> #如果我们没遇上break
>>> x=10
>>> while x>0 :
... x=x-1
... if x%2 != 0: continue
... print x,
...
8 6 4 2 0
>>> x=10
>>> a=1
>>> tmp=1
>>> while x>0: #阶乘
... a=a*tmp
... tmp=tmp+1
... x=x-1
...
>>> a
3628800
whwu@master:~/script/python/example> cat charpter3_exam1.py
#!/usr/bin/env python
# Fibonacci series up to n
a = int(input('number:')) #a的数字类型是 整形 如果变成float 则会有小数
x=a/2
while x>1:
if a%x == 0:
print a,'has factor',x
break
x=x-1
else:
print a,'is prime'
whwu@master:~/script/python/example> ./charpter3_exam1.py
number:39
39 has factor 13
whwu@master:~/script/python/example> ./charpter3_exam1.py
number:37
37 is prime
----for循环----
for<目标> in <对象>: # 该目标赋以对象的项
<语句> # 重复循环体:使用目标
else:
<语句> # 如果我们没遇上break
>>> for x in ["spam","eggs","ham"]:
... print x,
...
spam eggs ham
>>> sum=0
>>> for x in [1,2,3,4]:
... sum=sum+x
...
>>> sum
10
>>> prod=1
>>> for item in [1,2,3,4]:
... prod=prod*item
...
>>> prod
24
for循环也适用于字符串和元组
>>> for x in S:
... sys.stdout.write('%s' %x)
...
lumberjack
>>> for i in range(6):
... for j in range(i):
... print '*',
... print
...
*
* *
* * *
* * * *
* * * * *
>>> import sys
>>> for i in range(6):
... for j in range(i):
... sys.stdout.write('*') #标准输出
... print
...
*
**
***
****
*****
>>> items=["aaa",111,(4,5),2.01]
>>> tests=[(4,5),3.14]
>>> for key in tests: # 所有的键
... for item in items: # 所有的项
... if item == key: # 检查是否匹配
... print key,"was found"
... break
... else: # 嵌套的else循环是重要的,它与内部for循环首行的缩进处于同一列,所以它
... print key,"not found!" # 它是与内部循环关联的(而不是if或外部的for)
...
(4, 5) was found
3.14 not found!
--in的操作--
>>> for key in tests:
... if key in items:
... print key,"was found"
... else:
... print key,"not found!"
...
(4, 5) was found
3.14 not found!
--求交集--
>>> seq1="spam"
>>> seq2="scam"
>>> res=[] # 开始为空
>>> for x in seq1: # 扫描第一个序列
... if x in seq2: # 是否为公共项?
... res.append(x) # 向结尾添加结果
...
>>>
>>> res
['s', 'a', 'm']
>>>
--range和计数循环--
>>> range(5),range(2,5),range(0,10,2)
([0, 1, 2, 3, 4], [2, 3, 4], [0, 2, 4, 6, 8])
>>> x='spam'
>>> for item in x: # 简单的循环
... print item,
...
s p a m
>>> i=0
>>> while i < len(x): # while循环
... print x[i],
... i=i+1
...
s p a m
----python读取和写入数据----
一、用Python创建一个新文件,内容是从0到9的整数, 每个数字占一行
>>> f=open('f.txt','w')
>>> for i in range(0,10):
... f.write(str(i)+'\n')
...
>>> f.close()
whwu@master:~/script/python> cat f.txt
0
1
2
3
4
5
6
7
8
9
二、文件内容追加,从0到9的10个随机整数:
>>> import random
>>> f=open('f.txt','a')
>>> for i in range(0,10):
... f.write(str(random.randint(0,9)))
...
>>> f.write('\n')
>>> f.close()
>>>
whwu@master:~/script/python> cat f.txt
0
1
2
3
4
5
6
7
8
9
3314668039