-
Notifications
You must be signed in to change notification settings - Fork 0
/
python基础
334 lines (268 loc) · 7 KB
/
python基础
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
###############################
##########python基础###########
###############################
如何运行python程序
#####交互式#####
whwu@master:~/script/python> python
Python 2.6.6 (r266:84292, Jul 23 2015, 15:22:56)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print 'Hello World!'
Hello World!
>>>
Ctrl-D 退出
#####运行模块文件#####
whwu@master:~/script/python/example> cat spam.py
import sys
print sys.argv
whwu@master:~/script/python/example> python spam.py -i eggs
['spam.py', '-i', 'eggs']
#####Unix脚本#####
whwu@master:~/script/python/example> cat charpter1_exam1.py
#!/usr/bin/env python
print 'the bright side of life.'
whwu@master:~/script/python/example> ./charpter1_exam1.py
the bright side of life.
#####模块文件基础#####
whwu@master:~/script/python/example> cat myfile.py
title="The Meaning of Life"
whwu@master:~/script/python/example> python
Python 2.6.6 (r266:84292, Jul 23 2015, 15:22:56)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import myfile #python寻找文件时,它知道应该包括后缀
>>> print myfile.title
The Meaning of Life
>>>
KeyboardInterrupt
>>>
whwu@master:~/script/python/example> cat charpter1_exam2.py
#!/usr/bin/env python
import myfile
print myfile.title
whwu@master:~/script/python/example> ./charpter1_exam2.py
The Meaning of Life
#######################################
##########python类型与操作符###########
#######################################
#####数字#####
按位运算符是把数字看作二进制来进行计算的。Python中的按位运算法则如下:
按位运算符
whwu@master:~/script/python/example> python
Python 2.6.6 (r266:84292, Jul 23 2015, 15:22:56)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 5&3 #按位与 101 11 相同位仅为个位1 ,故结果为 1
1
>>> 5|3 #按位或 | 举例: 5|3 = 7 解释: 101 11 出现1的位是 1 1 1,故结果为 111
7
>>> 5^3 #按位异或 解释: 101 11 对位相加(不进位)是 1 1 0,故结果为 110
6
>>> ~5 #按位反转 解释: 将二进制数+1之后乘以-1,即~x = -(x+1),-(101 + 1) = -110
-6
>>> 5<<2 #按位左移 解释:101 向左移动2位得到 10100 ,即右面多出2位用0补
20
>>> 5>>2 #按位右移 解释:101 向右移动2位得到 1,即去掉右面的2位
1
>>> import math #应用库函数
>>> pi
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'pi' is not defined
>>> math pi
File "<stdin>", line 1
math pi
^
SyntaxError: invalid syntax
>>> math.pi
3.1415926535897931
>>> abs(-42)
42
>>> 2**4
16
#####字符串####
基本操作
>>> len('avd')
3
>>> 'as'+'234'+'edc'
'as234edc'
>>> 'HaHa!'*4
'HaHa!HaHa!HaHa!HaHa!'
>>> myjob='hacker'
>>> for c in myjob:print c #迭代
...
h
a
c
k
e
r
>>> 'k' in myjob #成员关系
True
>>> s='spam' #索引和分片
>>> s[0],s[-1]
('s', 'm')
>>> s[1:3]
'pa'
>>> s[1:]
'pam'
>>> s[-1:]
'm'
>>> s[:-1]
'spa'
>>> s #修改与格式化
'spam!'
>>> s=s[:4]+'Burger'+s[-1]
>>> s
'spamBurger!'
>>> 'That is %d %s bird!'% (1,'dead' )
'That is 1 dead bird!'
>>> exclamation = "Ni"
>>> "The knights who say %s!" % exclamation
'The knights who say Ni!'
>>> "%d %s %d you" % (1, 'spam', 4)
'1 spam 4 you'
>>> "%s -- %s -- %s" % (42, 3.14159, [1, 2, 3])
'42 -- 3.14159 -- [1, 2, 3]'
###字符串工具###
>>> import string # 标准模块
>>> S="spammify"
>>> string.upper(S) #转化为大写
'SPAMMIFY'
>>> string.find(S,"mm") #返回索引的子串
3
>>> string.atoi("42"),`42` #转化字符串
(42, '42')
>>> string.atoi("42")+1 # 转化为数字
43
>>> "spam"+`42` # 反引号是字符串
'spam42'
###字符串常量变量###
>>> mixed="Guido's" # 单引号在双引号中
>>> mixed
"Guido's"
>>> mixed='Guido"s' # 双引号在单引号中
>>> mixed
'Guido"s'
>>> mixed='Guido\'s' # 反斜线转译符
>>> mixed
"Guido's"
>>> split="This" "is" "concatenated" #合并邻近的字符串常量
>>> split
'Thisisconcatenated'
>>> split="This" " is " "concatenated"
>>> split
'This is concatenated'
####内置模块sys的argv属性####
whwu@master:~/script/python/example> cat echo.py
import sys
print sys.argv[1:]
whwu@master:~/script/python/example> python echo.py -a -b -c
['-a', '-b', '-c']
whwu@master:~/script/python/example> cat echo.py
import sys
print sys.argv
whwu@master:~/script/python/example> python echo.py -a -b -c
['echo.py', '-a', '-b', '-c']
####列表####
基本操作
>>> len([1,2,3]) # 长度
3
>>> [1,2,3]+[4,5,6] #合并
[1, 2, 3, 4, 5, 6]
>>> ['Ni!']*4 #重复
['Ni!', 'Ni!', 'Ni!', 'Ni!']
>>> for x in [1,2,3]:print x, #迭代
...
1 2 3
索引和分片
>>> L=['apple','banana','canada','dog']
>>> L
['apple', 'banana', 'canada', 'dog']
>>> L[2] #从0开始的偏移
'canada'
>>>
>>> L[-1] #负偏移:从右数起
'dog'
>>> L[1:] #分片取得片段
['banana', 'canada', 'dog']
原位改变列表
>>> L=['apple','banana','cat','dog']
>>> L[1]
'banana'
>>> L
['apple', 'banana', 'cat', 'dog']
>>> L[1]='BANANA' #索引赋值
>>> L
['apple', 'BANANA', 'cat', 'dog']
>>> L[0:2]=['eat','more'] #分片赋值:删除0,1并插入
>>> L
['eat', 'more', 'cat', 'dog']
>>> L[1:2]=['AB','BC'] #删除1,插入两项
>>> L
['eat', 'AB', 'BC', 'cat', 'dog']
>>> L.append('eat') # append方法调用
>>> L
['eat', 'AB', 'BC', 'cat', 'dog', 'eat']
>>> L.sort() # 排序列表项
>>> L
['AB', 'BC', 'cat', 'dog', 'eat', 'eat']
>>> L
['AB', 'BC', 'cat', 'dog', 'eat', 'eat']
>>>
>>>
>>> del L[-1] #删除最后一项
>>> L
['AB', 'BC', 'cat', 'dog', 'eat']
>>> del L[1:] #删除1之后的项
>>> L
['AB']
字典
>>> d2={'spam':2,'ham':1,'eggs':3} #建立字典
>>> d2['spam'] #用键取值
2
>>> len(d2) #字典中的项数
3
>>> d2.has_key('ham') #键成员关系测试
True
>>> d2.keys() #键的列表
['eggs', 'ham', 'spam']
----比较、相等性和真值----
>>> L1=[1,('a',3)] # 同样的值,唯一的对象
>>> L2=[1,('a',3)]
>>> L1==L2 # ==操作符测试值是否相等,python运行了一个相等测试,递归比较所有的内嵌对象
True
>>> L1 is L2 # is操作符测试它们是否真的是同一个对象
False
----同层深度的重复加----
>>> L=[4,5,6]
>>> X=L*4
>>> Y=[L]*4
>>> Z=L[:]*4
>>> Z=[L[:]]*4 # 一份嵌入的L的拷贝
>>> X
[4, 5, 6, 4, 5, 6, 4, 5, 6, 4, 5, 6]
>>> Y
[[4, 5, 6], [4, 5, 6], [4, 5, 6], [4, 5, 6]]
>>> Z
[[4, 5, 6], [4, 5, 6], [4, 5, 6], [4, 5, 6]]
>>> L[1]=0
>>> X
[4, 5, 6, 4, 5, 6, 4, 5, 6, 4, 5, 6]
>>> Y
[[4, 0, 6], [4, 0, 6], [4, 0, 6], [4, 0, 6]]
>>> Z
[[4, 5, 6], [4, 5, 6], [4, 5, 6], [4, 5, 6]]
类型类别复习
对象类型 种类 可变
数字 数字的 不
列表 序列的 可
字符串 序列的 不
字典 映射的 可
元祖 序列的 不
文件 扩展程序 -
>>> T=(1,2,3)
>>> T[2]=4 #error 元组不可原位改变
>>> T=T[:2]+(4,)
>>> T
(1, 2, 4)