-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython.html
291 lines (263 loc) · 11.7 KB
/
python.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no"/>
<title>Hillary Sanders</title>
<!-- CSS -->
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link href="css/materialize.css" type="text/css" rel="stylesheet" media="screen,projection"/>
<link href="css/style.css" type="text/css" rel="stylesheet" media="screen,projection"/>
</head>
<body>
<nav class="blue-grey" role="navigation">
<div class="nav-wrapper container">
<!-- Dropdown Structure -->
<ul id="dd_art" class="dropdown-content blue-grey">
<li><a href="graphite.html" class="white-text">graphite</a></li>
<li><a href="ink.html" class="white-text">ink</a></li>
<li><a href="paint.html" class="white-text">paint</a></li>
<li><a href="mixed.html" class="white-text">mixed</a></li>
</ul>
<ul id="dd_cheatsheets" class="dropdown-content blue-grey">
<li><a href="python.html" class="white-text">Python</a></li>
<!-- <li><a href="r.html" class="white-text">R</a></li> -->
<li><a href="unix.html" class="white-text">UNIX</a></li>
<li><a href="latex.html" class="white-text">LaTeX</a></li>
<!-- <li><a href="probability.html" class="white-text">Probability</a></li> -->
</ul>
<nav>
<div class="nav-wrapper">
<a href="index.html" class="brand-logo hillz"> hillz</a>
<ul class="right hide-on-med-and-down">
<li><a href="resume.html">Resume</a></li>
<li><a href="recent_projects.html">Recent Projects</a></li>
<!-- <li><a href="bayesian_statistics.html">Bayesian Statistics</a></li> -->
<li><a href="about_me.html">About Me</a></li>
<!-- Dropdown Trigger -->
<!-- <li><a class="dropdown-button" href="#" data-hover="true" data-beloworigin="true" data-activates="dd_cheatsheets">
Cheatsheets
<i class="material-icons right">arrow_drop_down</i></a></li> -->
<li><a class="dropdown-button" href="#" data-hover="true" data-beloworigin="true" data-activates="dd_art">
Art
<i class="material-icons right">arrow_drop_down</i></a></li>
</ul>
</div>
</nav>
<ul id="nav-mobile" class="side-nav blue-grey white-text">
<li><a href="index.html" class="white-text">HOME</a></li>
<li><a href="resume.html" class="white-text">Resume</a></li>
<li><a href="recent_projects.html" class="white-text">Recent Projects</a></li>
<!-- <li><a href="bayesian_statistics.html" class="white-text">Bayesian Statistics</a></li> -->
<li><a href="about_me.html" class="white-text">About Me</a></li>
<li><a class="white-text">Art</a></li>
<li><a href="graphite.html" class="white-text"> graphite</a></li>
<li><a href="ink.html" class="white-text"> pen</a></li>
<li><a href="paint.html" class="white-text"> paint</a></li>
<li><a href="mixed.html" class="white-text"> mixed</a></li>
<li><a class="white-text">Cheatsheets</a></li>
<li><a href="python.html" class="white-text"> Python</a></li>
<li><a href="r.html" class="white-text"> R</a></li>
<li><a href="unix.html" class="white-text"> UNIX</a></li>
<li><a href="latex.html" class="white-text"> LaTeX</a></li>
<li><a href="probability.html" class="white-text"> Probability</a></li>
</ul>
<a href="#" data-activates="nav-mobile" class="button-collapse"><i class="material-icons">menu</i></a>
</div>
</nav>
<div class="row center">
<h2 class="header col s12 white-text">Python</h2>
</div>
<div class="row">
<div class="col s12 m4 l3">
<div class="cheatbox">
<h5> Overall </h5>
<li> Indendation matters! </li>
<li> Python is case sensitive </li>
<li> Comments: # , or """<i>multiline comment</i> """ </li>
<br>
<li> a=b=c=1 defines a, b, and c to be 1</li>
<li> a,b,c = 1, 1.0, "Hello Glorious World" </li>
</br>
</div>
<div class="cheatbox">
<h5> Logical </h5>
==, !=, <, >, <=, >- </br>
and, or, not </br>
in, not in <br>
is, is not (x is y, here is results in 1 if id(x) equals id(y).) </br>
<i> In general, 0 is FALSE, and all other numbers are TRUE </i>
</br>
- x or y </br>
- x and y </br>
- not x </br>
</div>
<div class="cheatbox">
<h5> Arithmatic </h5>
+, -, *, / <br>
% (remainder), ** (exponent), // (floor division)
</div>
<div class="cheatbox">
<h5> Assignment Operators </h5>
= Simple assignment </br>
c += a is equivalent to c = c + a</br>
c -= a is equivalent to c = c - a</br>
c *= a is equivalent to c = c * a</br>
c /= a is equivalent to c = c / a</br>
c %= a is equivalent to c = c % a</br>
c **= a is equivalent to c = c ** a</br>
c //= a is equivalent to c = c // a</br>
<h5> Conditional Assignment </h5>
x = 3 if (y == 1) else 2 </br>
x = 3 if (y == 1) else 2 if (y == -1) else 1 </br>
# also valid:
x = (class1 if y == 1 else class2)(arg1, arg2) </br>
</br>
</div>
</div>
<div class="col s12 m4 l3">
<div class="cheatbox">
<h5> Functions </h5>
def FUN(parameters): </br>
""" docstring """ </br>
# do stuff </br>
return ...
</div>
<div class="cheatbox">
<h5> Data Types </h5>
<li><b> Numbers: </b> </li>
int(), long(), float(), and complex() </br>
Numbers allow the use of math.floor(), math.ceil(), math.round(x[, n])
<li><b> Strings: </b> If lala="omg so cool", then <br>
lala[0:4] is "omg". </br>
lala[0:4]*2 is "omgomg". </br>
lala + " PUMPKIN!" is "omg so cool PUMPKIN!" </br>
<b> % </b> </br>
>>> print '%(lang)s has %(n)03d quote types.' % \ </br>
... {"lang": "Python", "n": 2} </br>
Python has 002 quote types. </br>
</li>
<li><b> Lists[ ]:</b> Very versatile! Unfrozen Tuples. </br>
IF li = [1, 12.02, "Cheese"]</br>
li[0:] is [1, 12.02, "Cheese"]</br>
li[0:2] is [1, 12.02]</br>
li[1] is 12.02</br>
li[-1] is "Cheese"</br>
li.append("b") is [1, 12.03, "Cheese", "b"]</br>
li.insert(2, "1") is [1, 12.013, "1", "Cheese"]</br>
li.extend(["1","2"]) is [1, 12.03, "Cheese", "1", "2"]</br>
[0,9]*2 returns [0,9,0,9] </br>
</li>
<li><b> Tuples( ): </b> In short, frozen lists. No .append, .extend, .insert. </br> </li>
<li><b> Dictionaries{ }: </b> UNORDERED </br>
d={'one':1, 'two':2} <b>#or</b> </br>
d={} </br>
d['one'] = 1 ; d['two'] = 2 </br>
THEN </br>
d['one'] is 1 </br>
d.keys() is ['one', 'two'] </br>
d.values() is [1, 2] </br>
</li>
</br>
</div>
</div>
<div class="col s12 m4 l3">
<div class="cheatbox">
<h5> Truth Value Testing </h5>
Any object can be tested for truth value. Basically any empty object, None, or False is considered False:</br>
- None </br>
- False </br>
- 0, 0.00, etc. </br>
- "", [], or () </br>
- {} </br>
- an object with length 0 </br>
</div>
<div class="cheatbox">
<h5>Flow Control</h5>
<li> <b> If (else) statements </b> </li>
if a>b: </br>
print b</br>
else:</br>
print a</br>
</br>
<li> <b> Loops </b> </br>
for, while </br>
Control Statements: </br>
<b>break</b> (terminates the loop) </br>
<b>continue</b> (skip the remainder and continue to the next iteration) </br>
<b>pass</b> (used when a statement is required syntactically but you do not want any command or code to execute.)
</li>
</br>
<li> <b> Nested List comprehensions </b> </li>
[(i,j) for i in range(3) for j in range(i) ] </br>
((i,j) for i in range(4) for j in range(i) ) </br>
</br>
<li> <b> Exception else clause </b> </li>
try:</br>
put_4000000000_volts_through_it(parrot)</br>
except Voom:</br>
print "'E's pining!"</br>
else:</br>
print "This parrot is no more!"</br>
finally:</br>
end_sketch()</br>
</br>
</div>
</div>
<div class="col s12 m4 l3">
<div class="cheatbox">
<h5> Argument Unpacking</h5>
def some_function(x, y): </br>
# do some magic </br>
</br>
tuple_foo = (3, 4) </br>
dict_bar = {'y': 3, 'x': 2} </br>
</br>
some_function(*tuple_foo) </br>
some_function(**dict_bar) </br>
</br>
</div>
<div class="cheatbox">
<h5> Slice Operators </h5>
a = [1,2,3,4,5]</br>
# iterate over the whole list in 2-increments </br>
>>> a[::2] </br>
[1,3,5]</br>
</br>
# reverse a list: </br>
>>> a[::-1] </br>
[5,4,3,2,1] </br>
</br>
</div>
<div class="cheatbox">
<h5> Chaining Comparison Operators </h5>
>>> x = 5 </br>
>>> 1 < x < 10 </br>
True </br>
>>> 10 < x < 20 </br>
False </br>
>>> x < 10 < x*10 < 100 </br>
True </br>
>>> 5 == x > 4 </br>
True </br>
</br>
</div>
<div class="cheatbox">
<h5> Readable, multi-line regular expressions </h5>
>>> pattern = """ </br>
... ^ # beginning of string </br>
... M{0,4} # thousands - 0 to 4 M's </br>
... (CM|CD|D?C{0,3}) # hundreds - 900 (CM), 400 (CD), 0-300 (0 to 3 C's), </br>
... # or 500-800 (D, followed by 0 to 3 C's) </br>
... $ # end of string </br>
... """ </br>
>>> re.search(pattern, 'M', re.VERBOSE) </br>
</div>
</div>
</div>
<!-- Scripts-->
<script src="js/jquery-2.2.3.min.js"></script>
<script src="js/materialize.js"></script>
<script src="js/init.js"></script>
</body>
</html>