-
Notifications
You must be signed in to change notification settings - Fork 0
/
14842902733244.html
654 lines (425 loc) · 17.9 KB
/
14842902733244.html
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
<!doctype html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>
python语法总结 - 星晨入梦
</title>
<link href="atom.xml" rel="alternate" title="星晨入梦" type="application/atom+xml">
<link rel="stylesheet" href="asset/css/foundation.min.css" />
<link rel="stylesheet" href="asset/css/docs.css" />
<script src="asset/js/vendor/modernizr.js"></script>
<script src="asset/js/vendor/jquery.js"></script>
<script src="asset/highlightjs/highlight.pack.js"></script>
<link href="asset/highlightjs/styles/github.css" media="screen, projection" rel="stylesheet" type="text/css">
<script>hljs.initHighlightingOnLoad();</script>
<script type="text/javascript">
function before_search(){
var searchVal = 'site:blog.cxstars.com ' + document.getElementById('search_input').value;
document.getElementById('search_q').value = searchVal;
return true;
}
</script>
</head>
<body class="antialiased hide-extras">
<div class="marketing off-canvas-wrap" data-offcanvas>
<div class="inner-wrap">
<nav class="top-bar docs-bar hide-for-small" data-topbar>
<section class="top-bar-section">
<div class="row">
<div style="position: relative;width:100%;"><div style="position: absolute; width:100%;">
<ul id="main-menu" class="left">
<li id=""><a target="_self" href="index.html">Home</a></li>
<li id=""><a target="_self" href="archives.html">Archives</a></li>
<li id=""><a target="_self" href="about.html">About</a></li>
</ul>
<ul class="right" id="search-wrap">
<li>
<form target="_blank" onsubmit="return before_search();" action="http://google.com/search" method="get">
<input type="hidden" id="search_q" name="q" value="" />
<input tabindex="1" type="search" id="search_input" placeholder="Search"/>
</form>
</li>
</ul>
</div></div>
</div>
</section>
</nav>
<nav class="tab-bar show-for-small">
<a href="javascript:void(0)" class="left-off-canvas-toggle menu-icon">
<span> 星晨入梦</span>
</a>
</nav>
<aside class="left-off-canvas-menu">
<ul class="off-canvas-list">
<li><a target="_self" href="index.html">Home</a></li>
<li><a target="_self" href="archives.html">Archives</a></li>
<li><a target="_self" href="about.html">About</a></li>
<li><label>Categories</label></li>
<li><a href="ml.html">ml</a></li>
<li><a href="MySQL.html">MySQL</a></li>
<li><a href="python.html">python</a></li>
<li><a href="nginx.html">nginx</a></li>
<li><a href="golang.html">golang</a></li>
<li><a href="book.html">book</a></li>
<li><a href="arch.html">arch</a></li>
<li><a href="redis.html">redis</a></li>
<li><a href="life.html">life</a></li>
<li><a href="php.html">php</a></li>
</ul>
</aside>
<a class="exit-off-canvas" href="#"></a>
<section id="main-content" role="main" class="scroll-container">
<script type="text/javascript">
$(function(){
$('#menu_item_index').addClass('is_active');
});
</script>
<div class="row">
<div class="large-8 medium-8 columns">
<div class="markdown-body article-wrap">
<div class="article">
<h1>python语法总结</h1>
<div class="read-more clearfix">
<span class="date">2017/1/13 14:51 下午</span>
<span>posted in </span>
<span class="posted-in"><a href='python.html'>python</a></span>
<span class="comments">
<a class="ds-thread-count" data-thread-key="14842902733244.html" data-count-type="comments" href="14842902733244.html#ds-thread"></a>
</span>
</div>
</div><!-- article -->
<div class="article-content">
<h2 id="toc_0">变量</h2>
<h3 id="toc_1">命令规则</h3>
<ul>
<li>必须是大小写英文,数字和_的组合,且不能用数字开头</li>
<li>不用声明,直接使用(同PHP)</li>
</ul>
<h3 id="toc_2">类型</h3>
<ul>
<li>整数</li>
<li>浮点</li>
<li>字符串</li>
<li>布尔</li>
<li>空: None (与0不同)</li>
<li>Tuple : 元组,值不可变更</li>
<li>List : 表,值可以变更[下限:上限:步长]</li>
<li>Dictionary : 字典</li>
<li>自定义数据类型</li>
</ul>
<h2 id="toc_3">语法</h2>
<h3 id="toc_4">缩进</h3>
<p>python强制使用缩进来解析块代码.</p>
<h3 id="toc_5">运算符</h3>
<ul>
<li>数学 : <code>+,-,*,/,**,%</code></li>
<li>判断 : <code>==,!=,>,>=,<,<=,in</code></li>
<li>逻辑 : <code>and,or,not</code></li>
</ul>
<h3 id="toc_6">条件语句</h3>
<pre><code class="language-python">if condition1:
do1
elif condition2:
do2
elif condition3:
do3
else:
do4
</code></pre>
<h3 id="toc_7">循环</h3>
<p><strong>for</strong></p>
<pre><code class="language-python">for elemnt in list:
statment
</code></pre>
<p><strong>while</strong></p>
<pre><code class="language-python">while condition:
statement
</code></pre>
<p><strong>continue</strong><br/>
<strong>break</strong></p>
<span id="more"></span><!-- more -->
<h3 id="toc_8">错误处理</h3>
<ul>
<li>函数返回值</li>
<li>异常处理</li>
</ul>
<pre><code class="language-python">try:
do1
except exception1:
do e1==只有exception1才会进入这里==
except exception2:
do e2
except:
do default e==默认的exception处理==
else:
do elsedo==如果没有exception则进入这里==
finally:
do finallydo==不论有没有发生exception都要执行==
</code></pre>
<h2 id="toc_9">函数</h2>
<h3 id="toc_10">定义函数</h3>
<p><strong>实现功能,重复利用</strong></p>
<pre><code class="language-python">def func_name(a,b,...):
statement
return v1,v2,...
</code></pre>
<p><strong>若没有return或者return后无返回值时,函数将自动返回None</strong></p>
<h3 id="toc_11">参数传递</h3>
<ul>
<li>按顺序传递</li>
<li>关键字传递</li>
<li>参数默认值</li>
<li>包裹传递(packing) & 解包裹(unpacking)</li>
</ul>
<p><strong>可混用,混用原则:</strong><em>先位置,后关键字,包裹位置,包裹关键字</em></p>
<h4 id="toc_12">packing</h4>
<pre><code class="language-python">>>> def pack(*param):
... print param
>>> pack(1,2,3,4,"abc")
(1, 2, 3, 4, 'abc')
</code></pre>
<p>平常应用,可以强制将tuple转换成list:</p>
<pre><code class="language-python">>>> def pack2(*param):
... param = list(param)
... param[0] = "Hello"
... param[1] = "Python"
... print param
>>> pack2(1,2,"end")
['Hello', 'Python', 'end']
</code></pre>
<h4 id="toc_13">unpacking</h4>
<p><code>**param</code></p>
<h3 id="toc_14">函数对象(lambda)</h3>
<blockquote>
<p>这种能够接受一个函数作为参数的函数叫做「高阶函数」(higher-order function),是来自函数式编程(functional programming)的思想。</p>
</blockquote>
<pre><code class="language-python">>>> seq=[1,2,3,4,5]
>>> map(lambda x:x**3, [ x for x in seq ])
[1, 8, 27, 64, 125]
</code></pre>
<p><strong>高阶函数</strong>:<br/>
* filter(function, sequence):对sequence中的item依次执行function(item),将执行结果为True的item组成一个List/String/Tuple(取决于sequence的类型)返回<br/>
* map(function, sequence) :对sequence中的item依次执行function(item),见执行结果组成一个List返回<br/>
* reduce(function, sequence, starting_value):对sequence中的item顺序迭代调用function,如果有starting_value,还可以作为初始值调用,例如可以用来对List求和<br/>
* lambda:这是Python支持一种有趣的语法,它允许你快速定义单行的最小函数,类似与C语言中的宏,这些叫做lambda的函数,是从LISP借用来的,可以用在任何需要函数的地方</p>
<p><strong>lambda与def的差别</strong></p>
<pre><code class="language-python">map( lambda x: x*x, [y for y in range(10)] )
</code></pre>
<pre><code class="language-python">def sq(x):
return x * x
map(sq, [y for y in range(10)])
</code></pre>
<h2 id="toc_15">面向对象的类</h2>
<h3 id="toc_16">定义</h3>
<p><strong>使用class关键字定义一个类,并且类名首字母大写.</strong></p>
<h3 id="toc_17">语法</h3>
<pre><code class="language-python">class PyClass:
variables
functions
</code></pre>
<h3 id="toc_18">创建对象</h3>
<p><code>myClass = PyClass()</code></p>
<h3 id="toc_19">引用对象属性</h3>
<p><code>myClass.attribute</code></p>
<h3 id="toc_20">作用域</h3>
<ul>
<li>通过self调用类属性</li>
<li>对象生成时自动执行的特殊方法: <code>__init__()</code></li>
<li>类的属性是对数据的封装,方法则是对类的行为封装</li>
<li>类属性:
<ol>
<li>公有属性.</li>
<li>内置属性:由系统在定义类的时候默认添加,由前后两个下划线构成<code>__dict__</code>,<code>__module__</code></li>
<li>私有属性:不能被类以外的函数调用.定义时在属性名前加<code>__</code> </li>
</ol></li>
<li>方法也分为<strong>公有方法</strong>和<strong>私有方法</strong>
<ol>
<li>私有方法:以<code>__</code>定义</li>
<li>类方法:被<code>classmethod()</code>函数处理过的函数,添加类装饰器<code>@classmethod</code></li>
<li>静态方法:等于"全局函数"</li>
</ol></li>
</ul>
<pre><code class="language-python">from functools import wraps
class A:
# Decorator as an instance method
def decorator1(self, func):
@wraps(func)
def wrapper(*args, **kwargs):
print('Decorator 1')
return func(*args, **kwargs)
return wrapper
# Decorator as a class method
@classmethod
def decorator2(cls, func):
@wraps(func)
def wrapper(*args, **kwargs):
print('Decorator 2')
return func(*args, **kwargs)
return wrapper
</code></pre>
<h2 id="toc_21">模块</h2>
<h3 id="toc_22">模块导入</h3>
<pre><code class="language-python">import mod_name #引入模块
import m as k #引入后重命名
from m import func1 #引入m模块的func1方法
from m import * #从m中引入所有对象
</code></pre>
<h3 id="toc_23">模块调用</h3>
<p>mod.obj</p>
<h3 id="toc_24">模块搜索路径</h3>
<ol>
<li>程序所在目录</li>
<li>标准库的安装路径</li>
<li>操作系统环境变量PYTHONPATH的路径</li>
</ol>
<h3 id="toc_25">构建模块</h3>
<h3 id="toc_26">常用标准库</h3>
<ul>
<li>正则<code>re</code></li>
<li>时间与日期<code>time</code> <code>datetime</code></li>
<li>路径与文件<code>os.path</code> <code>glob</code></li>
<li>文件管理 <code>os</code> <code>shutil</code></li>
<li>存储对象 <code>pickle</code> <code>cPickle</code></li>
<li>子进程 <code>subprocess</code></li>
<li>信号 <code>signal</code></li>
<li>线程同步 <code>threading</code></li>
<li>进程信息 <code>os</code></li>
<li>多进程 <code>multiprocessing</code></li>
<li>数学 <code>math</code></li>
<li>随机数 <code>random</code></li>
<li>循环器 <code>itertools</code></li>
<li>数据库 <code>sqlite3</code></li>
<li><a href="https://docs.python.org/2/library">python2 more library</a></li>
</ul>
</div>
<div class="row">
<div class="large-6 columns">
<p class="text-left" style="padding:15px 0px;">
<a href="14848776957351.html"
title="Previous Post: 金山翻译&时间的workflow">« 金山翻译&时间的workflow</a>
</p>
</div>
<div class="large-6 columns">
<p class="text-right" style="padding:15px 0px;">
<a href="14842192746625.html"
title="Next Post: 在mac mini上安装CentOS7">在mac mini上安装CentOS7 »</a>
</p>
</div>
</div>
<div class="comments-wrap">
<div class="share-comments">
<div class="ds-thread" data-thread-key="14842902733244.html" data-url="http://blog.cxstars.com/14842902733244.html" data-title="python语法总结"></div>
</div>
</div>
</div><!-- article-wrap -->
</div><!-- large 8 -->
<div class="large-4 medium-4 columns">
<div class="hide-for-small">
<div id="sidebar" class="sidebar">
<div id="site-info" class="site-info">
<div class="site-a-logo"><img src="asset/img/cx_logo.png" /></div>
<h1>星晨入梦</h1>
<div class="site-des">life&code</div>
<div class="social">
<a target="_blank" class="github" target="_blank" href="https://github.com/star1989" title="GitHub">GitHub</a>
<a target="_blank" class="rss" href="atom.xml" title="RSS">RSS</a>
</div>
</div>
<div id="site-categories" class="side-item ">
<div class="side-header">
<h2>Categories</h2>
</div>
<div class="side-content">
<p class="cat-list">
<a href="ml.html"><strong>ml</strong></a>
<a href="MySQL.html"><strong>MySQL</strong></a>
<a href="python.html"><strong>python</strong></a>
<a href="nginx.html"><strong>nginx</strong></a>
<a href="golang.html"><strong>golang</strong></a>
<a href="book.html"><strong>book</strong></a>
<a href="arch.html"><strong>arch</strong></a>
<a href="redis.html"><strong>redis</strong></a>
<a href="life.html"><strong>life</strong></a>
<a href="php.html"><strong>php</strong></a>
</p>
</div>
</div>
<div id="site-categories" class="side-item">
<div class="side-header">
<h2>Recent Posts</h2>
</div>
<div class="side-content">
<ul class="posts-list">
<li class="post">
<a href="14900171104128.html">TensorFlow安装</a>
</li>
<li class="post">
<a href="14899316488389.html">一篇上了头条的文章感想</a>
</li>
<li class="post">
<a href="14897253436771.html">How to Chose A Restful Api Http Status Code</a>
</li>
<li class="post">
<a href="14894790545658.html">设计模式原则</a>
</li>
<li class="post">
<a href="14893113007289.html">PHP PDO深入探索</a>
</li>
</ul>
</div>
</div>
</div><!-- sidebar -->
</div><!-- hide for small -->
</div><!-- large 4 -->
</div><!-- row -->
<div class="page-bottom clearfix">
<div class="row">
<p class="copyright">Copyright © 2015
Powered by <a target="_blank" href="http://www.mweb.im">MWeb</a>,
Theme used <a target="_blank" href="http://github.com">GitHub CSS</a>.</p>
</div>
</div>
</section>
</div>
</div>
<script type="text/javascript">
var duoshuoQuery = {short_name:'chenxin1989'};
(function() {
var ds = document.createElement('script');
ds.type = 'text/javascript';ds.async = true;
ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
ds.charset = 'UTF-8';
(document.getElementsByTagName('head')[0]
|| document.getElementsByTagName('body')[0]).appendChild(ds);
})();
</script>
<script src="asset/js/foundation.min.js"></script>
<script>
$(document).foundation();
function fixSidebarHeight(){
var w1 = $('.markdown-body').height();
var w2 = $('#sidebar').height();
if (w1 > w2) { $('#sidebar').height(w1); };
}
$(function(){
fixSidebarHeight();
})
$(window).load(function(){
fixSidebarHeight();
});
</script>
<script src="asset/chart/all-min.js"></script><script type="text/javascript">$(function(){ var mwebii=0; var mwebChartEleId = 'mweb-chart-ele-'; $('pre>code').each(function(){ mwebii++; var eleiid = mwebChartEleId+mwebii; if($(this).hasClass('language-sequence')){ var ele = $(this).addClass('nohighlight').parent(); $('<div id="'+eleiid+'"></div>').insertAfter(ele); ele.hide(); var diagram = Diagram.parse($(this).text()); diagram.drawSVG(eleiid,{theme: 'simple'}); }else if($(this).hasClass('language-flow')){ var ele = $(this).addClass('nohighlight').parent(); $('<div id="'+eleiid+'"></div>').insertAfter(ele); ele.hide(); var diagram = flowchart.parse($(this).text()); diagram.drawSVG(eleiid); } });});</script>
<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><script type="text/x-mathjax-config">MathJax.Hub.Config({TeX: { equationNumbers: { autoNumber: "AMS" } }});</script>
<script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?b65a52970ddc989d3f85c97f61de0c39";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
</body>
</html>