/
index.html
413 lines (267 loc) · 8.92 KB
/
index.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
<!DOCTYPE html>
<!--[if IEMobile 7 ]><html class="no-js iem7"><![endif]-->
<!--[if lt IE 9]><html class="no-js lte-ie8"><![endif]-->
<!--[if (gt IE 8)|(gt IEMobile 7)|!(IEMobile)|!(IE)]><!--><html class="no-js" lang="en"><!--<![endif]-->
<head>
<meta charset="utf-8">
<title>Beginner_Of_Python - Mike Tech</title>
<meta name="author" content="Mike Gao">
<meta name="description" content="变量交换 x = 6
y = 5 x, y = y, x
print x >>> 5
print y
>>> 6 if 语句再行内 print "Hello" if True else "World"
>>> Hello 连接 …">
<!-- http://t.co/dKP3o1e -->
<meta name="HandheldFriendly" content="True">
<meta name="MobileOptimized" content="320">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="canonical" href="http://mikegao.github.com/blog/beginner-of-python/">
<link href="/favicon.png" rel="icon">
<link href="/stylesheets/screen.css" media="screen, projection" rel="stylesheet" type="text/css">
<script src="/javascripts/modernizr-2.0.js"></script>
<script src="/javascripts/ender.js"></script>
<script src="/javascripts/octopress.js" type="text/javascript"></script>
<link href="/atom.xml" rel="alternate" title="Mike Tech" type="application/atom+xml">
<!--Fonts from Google"s Web font directory at http://google.com/webfonts -->
<link href="http://fonts.googleapis.com/css?family=PT+Serif:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">
<link href="http://fonts.googleapis.com/css?family=PT+Sans:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">
</head>
<body >
<header role="banner"><hgroup>
<h1><a href="/">Mike Tech</a></h1>
</hgroup>
</header>
<nav role="navigation"><ul class="subscription" data-subscription="rss">
<li><a href="/atom.xml" rel="subscribe-rss" title="subscribe via RSS">RSS</a></li>
</ul>
<form action="http://google.com/search" method="get">
<fieldset role="search">
<input type="hidden" name="q" value="site:mikegao.github.com" />
<input class="search" type="text" name="q" results="0" placeholder="Search"/>
</fieldset>
</form>
<ul class="main-navigation">
<li><a href="/">Blog</a></li>
<li><a href="/blog/archives">Archives</a></li>
</ul>
</nav>
<div id="main">
<div id="content">
<div>
<article class="hentry" role="article">
<header>
<h1 class="entry-title">Beginner_Of_Python</h1>
<p class="meta">
<time datetime="2013-01-29T10:42:00+08:00" pubdate data-updated="true">Jan 29<span>th</span>, 2013</time>
</p>
</header>
<div class="entry-content"><h4>变量交换</h4>
<pre><code>x = 6
y = 5
x, y = y, x
print x
>>> 5
print y
>>> 6
</code></pre>
<h4>if 语句再行内</h4>
<pre><code>print "Hello" if True else "World"
>>> Hello
</code></pre>
<h4>连接</h4>
<p> 下面的最后一种方式在绑定两个不同类型的对象时显得很cool</p>
<pre><code>nfc = ["Packers", "49ers"]
afc = ["Ravens", "Patriots"]
print nfc + afc
>>> ['Packers', '49ers', 'Ravens', 'Patriots']
print str(1) + " world"
>>> 1 world
pring 1, "world"
>>> 1 world
print nfc, 1
>>> ['Packers', '49ers'] 1
</code></pre>
<h4>数字技巧</h4>
<pre><code># 除后向下取整
print 5.0//2
>>> 2
# 2的5次方
print 2**5
>>> 32
</code></pre>
<h4>注意浮点数的除法</h4>
<pre><code>print .3/.1
>>> 2.9999999999999996
print .3//.1
>>> 2.0
</code></pre>
<h4>数值比较</h4>
<pre><code>x = 2
if 3 > x > 1:
print x
>>> 2
if 1 < x > 0:
print x
>>> 2
</code></pre>
<h4>同时迭代两个列表</h4>
<pre><code>nfc = ['Packers', '49ers']
afc = ['Ravens', 'Patriots']
for teama, teamb in zip(nfc, afc)
print teama + " vs. " + teamb
>>> Packers vs. Ravens
>>> 49ers vs. Patriots
</code></pre>
<h4>带索引的列表迭代</h4>
<pre><code>teams = ["Packers", "49ers", "Ravens", "Patriots"]
for index, team in enumerate(teams):
print index, team
>>> 0 Packers
>>> 1 49ers
>>> 2 Ravens
>>> 3 Patriots
</code></pre>
<h4>列表推导式</h4>
<p> 已知一个列表,我们可以刷选出偶数列表方法</p>
<pre><code>numbers = [1,2,3,4,5]
even = [number for number in numbers if number%2 == 0]
</code></pre>
<h4>字典推导</h4>
<p> 和列表推导类似</p>
<pre><code>teams = ['Packers', '49ers', 'Ravens', 'Patriots']
print {key: value for value, key in enumerate(teams)}
>>> {'49ers': 1, 'Ravens': 2, 'Patriots': 3, 'Packers': 0}
</code></pre>
<h4>初始化列表的值</h4>
<pre><code>items = [0]*3
print items
>>> [0, 0, 0]
</code></pre>
<h4>列表转换为字符串</h4>
<pre><code>teams = ['Packers', '49ers', 'Ravens', 'Patriots']
print ", ".join(teams)
>>> 'Packers, 49ers, Ravens, Patriots'
</code></pre>
<h4>从字典中获取元素</h4>
<pre><code>data = {'user': 1, 'name': 'Max', 'three': 4}
try:
is_admin = data['admin']
except KeyError:
is_admin = False
</code></pre>
<p> 替换成这样:</p>
<pre><code>data = {'user': 1, 'name': 'Max', 'three': 4}
is_admin = data.get('admin', False)
</code></pre>
<h4>获取列表的子集</h4>
<pre><code>x= [1,2,3,4,5]
# 前3个
print x[:3]
>>> [1, 2, 3]
# 中间4个
print x[1:5]
>>> [2, 3, 4, 5]
# 最后3个
print x[3:]
>>> [4, 5, 6]
# 奇数项
print x[::2]
>> [1, 3, 5]
# 偶数项
print x[1::2]
>> [2, 4, 6]
</code></pre>
<h4>60个字符解决FizzBuzz</h4>
<p> 写一个程序,打印数字1到100,3的倍数打印“Fizz”来替换这个数,5的倍数打印“Buzz”,对于既是3的倍数又是5的倍数的数字打印“FizzBuzz”</p>
<pre><code>for x in range(101):print'fizz'[x%3*4::]+'buzz'[x%5*4::]or x
</code></pre>
<h4>集合</h4>
<pre><code>from collections import Counter
print Counter("hello")
>>> Counter({'1': 2, 'h': 1, 'e': 1, 'o': 1})
</code></pre>
<h4>迭代工具</h4>
<p> 和collections库一样,还有一个库叫itertools,对某些问题真能高效地解决。其中一个用例是查找所有组合,他能告诉你在一个组中元素的所有不能的组合方式</p>
<pre><code>from itertools import combinations
teams = ["Packers", "49ers", "Ravens", "Patriots"]
for game in combinations(teams, 2):
print game
>>> ('Packers', '49ers')
>>> ('Packers', 'Ravens')
>>> ('Packers', 'Patriots')
>>> ('49ers', 'Ravens')
>>> ('49ers', 'Patriots')
>>> ('Ravens', 'Patriots')
</code></pre>
<h4>False == True</h4>
<pre><code>False = True
if False:
print "Hello"
else:
print "World"
>>> Hello
</code></pre>
</div>
<footer>
<p class="meta">
<span class="byline author vcard">Posted by <span class="fn">Mike Gao</span></span>
<time datetime="2013-01-29T10:42:00+08:00" pubdate data-updated="true">Jan 29<span>th</span>, 2013</time>
<span class="categories">
<a class='category' href='/blog/categories/python/'>python</a>
</span>
</p>
<div class="sharing">
<a href="http://twitter.com/share" class="twitter-share-button" data-url="http://mikegao.github.com/blog/beginner-of-python/" data-via="" data-counturl="http://mikegao.github.com/blog/beginner-of-python/" >Tweet</a>
</div>
<p class="meta">
<a class="basic-alignment left" href="/blog/hello-world/" title="Previous Post: Hello World">« Hello World</a>
</p>
</footer>
</article>
</div>
<aside class="sidebar">
<section>
<ul id="recent_posts">
<li class="post">
<a href="http://mikegao.github.com" alt="Home"><img src="/images/Home.png"></a>
<a href="http://mikegao.github.com/archives/" alt="Archives"><img src="/images/Calendar.png"></a>
<a href="mailto:mike.gao0611@gmail.com" alt="E-Mail"><img src="/images/Envelope.png"></a>
<a href="http://mikegao.github.com/atom.xml" alt="subscribe feed"><img src="/images/rss_big.png"></a>
</li>
</ul>
</section>
<section>
<h1>About Me</h1>
<p>A rubyer, Apple User</p>
</section>
<section>
<h1>Recent Posts</h1>
<ul id="recent_posts">
<li class="post">
<a href="/blog/beginner-of-python/">Beginner_Of_Python</a>
</li>
<li class="post">
<a href="/blog/hello-world/">Hello World</a>
</li>
</ul>
</section>
</aside>
</div>
</div>
<footer role="contentinfo"><p>
Copyright © 2013 - Mike Gao -
<span class="credit">Powered by <a href="http://octopress.org">Octopress</a></span>
</p>
</footer>
<script type="text/javascript">
(function(){
var twitterWidgets = document.createElement('script');
twitterWidgets.type = 'text/javascript';
twitterWidgets.async = true;
twitterWidgets.src = 'http://platform.twitter.com/widgets.js';
document.getElementsByTagName('head')[0].appendChild(twitterWidgets);
})();
</script>
</body>
</html>