-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
129 lines (120 loc) · 12.5 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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Python Cool Stuff</title>
<meta name="description" content="A collection of snippets and well-known techniques to make your Python code more concise & elegant.">
<link rel="icon" href="favicon.png">
<link rel="stylesheet" href="css/global.css">
</head>
<body>
<header>
<h1>Python Cool Stuff</h1>
<h2>A collection of snippets and well-known techniques to make your code more concise & elegant.</h2>
</header>
<main>
<article id="ternary-expression">
<h2><a href="#ternary-expression">Ternary Expressions</a></h2>
<p><em>if ... else</em> can be used to write a ternary expression. It does exactly what is sounds like: <q>assign 3 to x if y is 1</q>. The parens are not needed but they are good for readability.</p>
<code class="highlight"><pre><span class="o">>>></span> <span class="n">y</span> <span class="o">=</span> <span class="mi">0</span>
<span class="o">>>></span> <span class="n">x</span> <span class="o">=</span> <span class="mi">3</span> <span class="k">if</span> <span class="p">(</span><span class="n">y</span> <span class="o">==</span> <span class="mi">1</span><span class="p">)</span> <span class="k">else</span> <span class="mi">2</span>
<span class="o">>>></span> <span class="k">print</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="mi">2</span>
</pre></code>
</article>
<article id="the-zen-of-python">
<h2><a href="#the-zen-of-python">The Zen of Python</a></h2>
<p><q>Long time Pythoneer Tim Peters succinctly channels the BDFL's guiding principles for Python's design into 20 aphorisms, only 19 of which have been written down.</q></p>
<code class="highlight"><pre><span class="o">>>></span> <span class="kn">import</span> <span class="nn">this</span>
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
</pre></code>
</article>
<article id="chained-comparisons">
<h2><a href="#chained-comparisons">Chained Comparisons</a></h2>
<p>Chained comparisons are faster and prettier than the <q>normal</q> way:</p>
<code class="highlight"><pre><span class="o">>>></span> <span class="k">if</span> <span class="mi">10</span> <span class="o"><=</span> <span class="n">f</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o"><</span> <span class="mi">150</span><span class="p">:</span>
<span class="o">...</span> <span class="n">g</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
</pre></code>
</article>
<article id="looping-over-two-collections">
<h2><a href="#looping-over-two-collections">Looping over two collections</a></h2>
<p><a href="http://docs.python.org/2/library/functions.html#zip">zip</a> (or <a href="http://docs.python.org/2/library/itertools.html#itertools.izip">izip</a> for better performance) let you iterate over two collections at the same time by returning tuples of their corresponding values:</p>
<code class="highlight"><pre><span class="o">>>></span> <span class="n">names</span> <span class="o">=</span> <span class="p">[</span><span class="s">'john'</span><span class="p">,</span> <span class="s">'adam'</span><span class="p">,</span> <span class="s">'betty'</span><span class="p">]</span>
<span class="o">>>></span> <span class="n">drinks</span> <span class="o">=</span> <span class="p">[</span><span class="s">'coffee'</span><span class="p">,</span> <span class="s">'tea'</span><span class="p">,</span> <span class="s">'milk'</span><span class="p">]</span>
<span class="o">>>></span>
<span class="o">>>></span> <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">drink</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">names</span><span class="p">,</span> <span class="n">drinks</span><span class="p">):</span>
<span class="o">...</span> <span class="k">print</span><span class="p">(</span><span class="n">name</span> <span class="o">+</span> <span class="s">' -> '</span> <span class="o">+</span> <span class="n">drink</span><span class="p">)</span>
<span class="o">...</span>
<span class="n">john</span> <span class="o">-></span> <span class="n">coffee</span>
<span class="n">adam</span> <span class="o">-></span> <span class="n">tea</span>
<span class="n">betty</span> <span class="o">-></span> <span class="n">milk</span>
</pre></code>
<p>You can even construct a dict from these two lists with <a href="http://docs.python.org/2/library/functions.html#zip">zip</a>/<a href="http://docs.python.org/2/library/itertools.html#itertools.izip">izip</a>:</p>
<code class="highlight"><pre><span class="o">>>></span> <span class="n">d</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">izip</span><span class="p">(</span><span class="n">names</span><span class="p">,</span> <span class="n">drinks</span><span class="p">))</span>
<span class="p">{</span><span class="s">'john'</span><span class="p">:</span> <span class="s">'coffee'</span><span class="p">,</span> <span class="s">'adam'</span><span class="p">:</span> <span class="s">'tea'</span><span class="p">,</span> <span class="s">'betty'</span><span class="p">:</span> <span class="s">'milk'</span><span class="p">}</span>
</pre></code>
</article>
<article id="looping-backwards">
<h2><a href="#looping-backwards">Looping backwards</a></h2>
<p>With <a href="http://docs.python.org/2/library/functions.html?highlight=reversed#reversed">reversed</a> you get an iterator over the backwards of the sequence you passed in:</p>
<code class="highlight"><pre><span class="o">>>></span> <span class="n">s</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">]</span>
<span class="o">>>></span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>
<span class="o">...</span> <span class="k">print</span> <span class="n">i</span>
<span class="o">...</span>
<span class="mi">5</span> <span class="mi">4</span> <span class="mi">3</span> <span class="mi">2</span> <span class="mi">1</span>
</pre></code>
</article>
<article id="simple-http-server">
<h2><a href="#simple-http-server">SimpleHTTPServer</a></h2>
<p>Want to know how your site looks on the server? Just launch the python built-in <a href="http://docs.python.org/2/library/simplehttpserver.html#module-SimpleHTTPServer">SimpleHTTPServer</a> module:</p>
<code class="highlight"><pre><span class="nv">$ </span>python -m SimpleHTTPServer
<span class="nv">$ </span>Serving HTTP on 0.0.0.0 port 8000 ...
</pre></code>
</article>
<article id="defaultdict">
<h2><a href="#defaultdict">defaultdict</a></h2>
<p>With <a href="http://docs.python.org/2/library/collections.html?highlight=defaultdict#collections.defaultdict">defaultdict</a> you'll never get a <a href="http://docs.python.org/2/library/exceptions.html?highlight=keyerror#exceptions.KeyError">KeyError</a>. Instead, it will always return the factory function you passed in:</p>
<code class="highlight"><pre><span class="o">>>></span> <span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">defaultdict</span>
<span class="o">>>></span> <span class="n">s</span> <span class="o">=</span> <span class="s">'mississippi'</span>
<span class="o">>>></span> <span class="n">d</span> <span class="o">=</span> <span class="n">defaultdict</span><span class="p">(</span><span class="nb">int</span><span class="p">)</span>
<span class="o">>>></span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">s</span><span class="p">:</span>
<span class="o">...</span> <span class="n">d</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="o">...</span>
<span class="o">>>></span> <span class="n">d</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
<span class="p">[(</span><span class="s">'i'</span><span class="p">,</span> <span class="mi">4</span><span class="p">),</span> <span class="p">(</span><span class="s">'p'</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="p">(</span><span class="s">'s'</span><span class="p">,</span> <span class="mi">4</span><span class="p">),</span> <span class="p">(</span><span class="s">'m'</span><span class="p">,</span> <span class="mi">1</span><span class="p">)]</span>
</pre></code>
</article>
<article id="dictcomprehension">
<h2><a href="#dictcomprehension">dict comprehension</a></h2>
<p>Likewise list, there's a way to create dictionary using the 'comprehension-way':</p>
<code class="highlight"><pre><span class="o">>>></span> <span class="p">{</span> i <span class="p">:</span> chr<span class="p">(</span><span class="mi">65</span>+i<span class="p">)</span> <span class="k">for</span> i <span class="k">in</span> range<span class="p">(</span><span class="mi">26</span><span class="p">) }</span></pre></code>
<p>This will prompt:</p>
<code class="highlight"><pre><span class="o">>>></span> <span class="p">{</span><span class="mi">0</span>: <span class="s">'A'</span>, <span class="mi">1</span>: <span class="s">'B'</span>, <span class="mi">2</span>: <span class="s">'C'</span>, <span class="mi">3</span>: <span class="s">'D'</span>, <span class="mi">4</span>: <span class="s">'E'</span>, <span class="mi">5</span>: <span class="s">'F'</span>, <span class="mi">6</span>: <span class="s">'G'</span>, <span class="mi">7</span>: <span class="s">'H'</span>, <span class="mi">8</span>: <span class="s">'I'</span>, <span class="mi">9</span>: <span class="s">'J'</span>, <span class="mi">10</span>: <span class="s">'K'</span>, <span class="mi">11</span>: <span class="s">'L'</span>, <span class="mi">12</span>: <span class="s">'M'</span>, <span class="mi">13</span>: <span class="s">'N'</span>, <span class="mi">14</span>: <span class="s">'O'</span>, <span class="mi">15</span>: <span class="s">'P'</span>, <span class="mi">16</span>: <span class="s">'Q'</span>, <span class="mi">17</span>: <span class="s">'R'</span>, <span class="mi">18</span>: <span class="s">'S'</span>, <span class="mi">19</span>: <span class="s">'T'</span>, <span class="mi">20</span>: <span class="s">'U'</span>, <span class="mi">21</span>: <span class="s">'V'</span>, <span class="mi">22</span>: <span class="s">'W'</span>, <span class="mi">23</span>: <span class="s">'X'</span>, <span class="mi">24</span>: <span class="s">'Y'</span>, <span class="mi">25</span>: <span class="s">'Z'</span><span class="p">}</span>
</pre></code>
</article>
</main>
<footer>
<div>made by <a href="https://twitter.com/mstrcnvs">@mstrcnvs</a></div>
<div>thanks to <a href="https://twitter.com/raymondh">@raymondh</a></div>
</footer>
<a href="https://github.com/mstrcnvs/python-cool-stuff"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_gray_6d6d6d.png" alt="Fork me on GitHub"></a></body>
</html>