-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython.html
More file actions
382 lines (316 loc) · 24.6 KB
/
python.html
File metadata and controls
382 lines (316 loc) · 24.6 KB
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
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>
Hillary Sanders
</title>
<meta name="author" content="Hillary Sanders">
<meta name="keywords" content="Hillary Sanders Statistics Berkeley Premise Data Scientist Art Bayesian Painting">
<meta name="description" content="Hillary Sanders: Data Scientist?">
<meta name="robots" content="index, follow, noarchive">
<link type="text/css" rel="stylesheet" href="css/reset.css"/>
<link type="text/css" rel="stylesheet" href="css/mainstylesheet.css"/>
<style>
.midwrap {
min-height: 1900px;
}
.light {
min-height: 1525px;
}
</style>
</head>
<body>
<div class="midwrap">
<div class="header codex basic"><h1>hillary sanders</h1></div>
<div class="navbox">
<ul class="navlist">
<a href="index.html"><li class="navitem basic">about hillz</li></a>
<a href="drawings.html"><li class="navitem basic">drawings</li></a>
<a href="paintings.html"><li class="navitem basic">paintings</li></a>
<a href="mixed-media.html"><li class="navitem basic">mixed media</li></a>
<a href="cheatsheets.html"><li class="navitem basic breadcrumb">cheatsheets</li></a>
<a href="code.html"><li class="navitem basic">code</li></a>
<a href="graphs.html"><li class="navitem basic">graphs</li></a>
<a href="resume.html"><li class="navitem basic">resume</li></a>
</ul>
</div>
<div class="navbox navbox">
<ul class="navlist-mini">
<a href="emacs.html"><li class="navitem-mini basic">Emacs</li></a>
<a href="python.html"><li class="navitem-mini basic breadcrumb">Python</li></a>
<a href="scala.html"><li class="navitem-mini basic">Scala</li></a>
<a href="latex.html"><li class="navitem-mini basic">LaTeX</li></a>
<a href="UNIX.html"><li class="navitem-mini basic">UNIX</li></a>
<a href="ruby-on-rails.html"><li class="navitem-mini basic">Ruby on Rails</li></a>
<a href="probability_theory.html"><li class="navitem-mini basic">Probability</li></a>
<a href="bayesian_analysis.html"><li class="navitem-mini basic">Bayesian Stats</li></a>
</ul>
</div>
<!-- Main content -->
<div class="onecol">
<h2><b>Python</b></h2>
</div>
<div class="midwrap light">
<div class="threecol thick">
<h2> The Basics </h2>
<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 World" </li>
<h3> Arithmatic </h3>
+, -, *, / <br>
% (remainder), ** (exponent), // (floor division)
</br>
<h3> Truth Value Testing </h3>
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>
<h3> Logical </h3>
==, !=, <, >, <=, >- </br>
and, or, not </br>
in, not in <br>
is, is not </br>
<i> 0 is FALSE, and all other numbers are TRUE </i>
</br>
- x or y </br>
- x and y </br>
- not x </br>
<h3> Functions </h3>
def your_function(parameters): </br>
; """ comment """ </br>
... </br>
return[ ... ]
<h3> If (else) statements </h3>
if a>b: </br>
print b</br>
else:</br>
print a</br>
<h3> Exception else clause </h3>
try:</br>
put_4000_volts_through(parrot)</br>
except Voom:</br>
print "'E's pining!"</br>
else:</br>
print "This parrot is no more!"</br>
finally:</br>
end_sketch()</br>
</div>
<div class="threecol thick">
<h2> Data Types </h2>
<h3> Numbers: </h3>
int(), long(), float(), and complex() </br>
Numbers allow the use of math.floor(), math.ceil(), math.round(), etc.
<h3> Strings: </h3> If lala="omg cool", then <br>
lala[0:4] is "omg". </br>
lala[0:4]*2 is "omgomg". </br>
lala + " wee!" is "omg cool wee!" </br>
<h3> Lists[ ]:</h3> 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>
<h3> Tuples( ): </h3> In short, frozen lists. No .append, .extend, .insert. </br>
<h3> Dictionaries{ }: </h3> 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>
<h2> Loops </h2>
for, while </br>
<h3> Control Statements: </h3>
<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.)<br>
<h3> Nested List comprehensions </h3>
[(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>
</div>
<div class="threecol thick">
<h2> Assignment </h2>
<h3> Assignment Operators </h3>
= 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>
</br>
<h3> Conditional Assignment </h3>
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>
<h3> Chaining Comparison Operators </h3>
>>> 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>
<h2> Methods </h2>
<h3> Argument Unpacking</h3>
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>
<h3> Slice Operators </h3>
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="onecol">
</br>
<table border="1" class="docutils">
<colgroup>
<col width="21%" />
<col width="19%" />
<col width="20%" />
<col width="19%" />
<col width="22%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head"></th>
<th class="head"></th>
<th class="head">Built-in Functions</th>
<th class="head"></th>
<th class="head"></th>
</tr>
</thead>
<tbody valign="top">
<tr><td><a href="http://docs.python.org/2/library/functions.html#abs" title="abs"><tt class="xref py py-func docutils literal"><span class="pre">abs()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#divmod" title="divmod"><tt class="xref py py-func docutils literal"><span class="pre">divmod()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#input" title="input"><tt class="xref py py-func docutils literal"><span class="pre">input()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#open" title="open"><tt class="xref py py-func docutils literal"><span class="pre">open()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#staticmethod" title="staticmethod"><tt class="xref py py-func docutils literal"><span class="pre">staticmethod()</span></tt></a></td>
</tr>
<tr><td><a href="http://docs.python.org/2/library/functions.html#all" title="all"><tt class="xref py py-func docutils literal"><span class="pre">all()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#enumerate" title="enumerate"><tt class="xref py py-func docutils literal"><span class="pre">enumerate()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#int" title="int"><tt class="xref py py-func docutils literal"><span class="pre">int()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#ord" title="ord"><tt class="xref py py-func docutils literal"><span class="pre">ord()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#str" title="str"><tt class="xref py py-func docutils literal"><span class="pre">str()</span></tt></a></td>
</tr>
<tr><td><a href="http://docs.python.org/2/library/functions.html#any" title="any"><tt class="xref py py-func docutils literal"><span class="pre">any()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#eval" title="eval"><tt class="xref py py-func docutils literal"><span class="pre">eval()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#isinstance" title="isinstance"><tt class="xref py py-func docutils literal"><span class="pre">isinstance()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#pow" title="pow"><tt class="xref py py-func docutils literal"><span class="pre">pow()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#sum" title="sum"><tt class="xref py py-func docutils literal"><span class="pre">sum()</span></tt></a></td>
</tr>
<tr><td><a href="http://docs.python.org/2/library/functions.html#basestring" title="basestring"><tt class="xref py py-func docutils literal"><span class="pre">basestring()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#execfile" title="execfile"><tt class="xref py py-func docutils literal"><span class="pre">execfile()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#issubclass" title="issubclass"><tt class="xref py py-func docutils literal"><span class="pre">issubclass()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#print" title="print"><tt class="xref py py-func docutils literal"><span class="pre">print()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#super" title="super"><tt class="xref py py-func docutils literal"><span class="pre">super()</span></tt></a></td>
</tr>
<tr><td><a href="http://docs.python.org/2/library/functions.html#bin" title="bin"><tt class="xref py py-func docutils literal"><span class="pre">bin()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#file" title="file"><tt class="xref py py-func docutils literal"><span class="pre">file()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#iter" title="iter"><tt class="xref py py-func docutils literal"><span class="pre">iter()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#property" title="property"><tt class="xref py py-func docutils literal"><span class="pre">property()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#tuple" title="tuple"><tt class="xref py py-func docutils literal"><span class="pre">tuple()</span></tt></a></td>
</tr>
<tr><td><a href="http://docs.python.org/2/library/functions.html#bool" title="bool"><tt class="xref py py-func docutils literal"><span class="pre">bool()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#filter" title="filter"><tt class="xref py py-func docutils literal"><span class="pre">filter()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#len" title="len"><tt class="xref py py-func docutils literal"><span class="pre">len()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#range" title="range"><tt class="xref py py-func docutils literal"><span class="pre">range()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#type" title="type"><tt class="xref py py-func docutils literal"><span class="pre">type()</span></tt></a></td>
</tr>
<tr><td><a href="http://docs.python.org/2/library/functions.html#bytearray" title="bytearray"><tt class="xref py py-func docutils literal"><span class="pre">bytearray()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#float" title="float"><tt class="xref py py-func docutils literal"><span class="pre">float()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#list" title="list"><tt class="xref py py-func docutils literal"><span class="pre">list()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#raw_input" title="raw_input"><tt class="xref py py-func docutils literal"><span class="pre">raw_input()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#unichr" title="unichr"><tt class="xref py py-func docutils literal"><span class="pre">unichr()</span></tt></a></td>
</tr>
<tr><td><a href="http://docs.python.org/2/library/functions.html#callable" title="callable"><tt class="xref py py-func docutils literal"><span class="pre">callable()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#format" title="format"><tt class="xref py py-func docutils literal"><span class="pre">format()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#locals" title="locals"><tt class="xref py py-func docutils literal"><span class="pre">locals()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#reduce" title="reduce"><tt class="xref py py-func docutils literal"><span class="pre">reduce()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#unicode" title="unicode"><tt class="xref py py-func docutils literal"><span class="pre">unicode()</span></tt></a></td>
</tr>
<tr><td><a href="http://docs.python.org/2/library/functions.html#chr" title="chr"><tt class="xref py py-func docutils literal"><span class="pre">chr()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#func-frozenset"><tt class="docutils literal"><span class="pre">frozenset()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#long" title="long"><tt class="xref py py-func docutils literal"><span class="pre">long()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#reload" title="reload"><tt class="xref py py-func docutils literal"><span class="pre">reload()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#vars" title="vars"><tt class="xref py py-func docutils literal"><span class="pre">vars()</span></tt></a></td>
</tr>
<tr><td><a href="http://docs.python.org/2/library/functions.html#classmethod" title="classmethod"><tt class="xref py py-func docutils literal"><span class="pre">classmethod()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#getattr" title="getattr"><tt class="xref py py-func docutils literal"><span class="pre">getattr()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#map" title="map"><tt class="xref py py-func docutils literal"><span class="pre">map()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#func-repr"><tt class="docutils literal"><span class="pre">repr()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#xrange" title="xrange"><tt class="xref py py-func docutils literal"><span class="pre">xrange()</span></tt></a></td>
</tr>
<tr><td><a href="http://docs.python.org/2/library/functions.html#cmp" title="cmp"><tt class="xref py py-func docutils literal"><span class="pre">cmp()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#globals" title="globals"><tt class="xref py py-func docutils literal"><span class="pre">globals()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#max" title="max"><tt class="xref py py-func docutils literal"><span class="pre">max()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#reversed" title="reversed"><tt class="xref py py-func docutils literal"><span class="pre">reversed()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#zip" title="zip"><tt class="xref py py-func docutils literal"><span class="pre">zip()</span></tt></a></td>
</tr>
<tr><td><a href="http://docs.python.org/2/library/functions.html#compile" title="compile"><tt class="xref py py-func docutils literal"><span class="pre">compile()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#hasattr" title="hasattr"><tt class="xref py py-func docutils literal"><span class="pre">hasattr()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#func-memoryview"><tt class="docutils literal"><span class="pre">memoryview()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#round" title="round"><tt class="xref py py-func docutils literal"><span class="pre">round()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#__import__" title="__import__"><tt class="xref py py-func docutils literal"><span class="pre">__import__()</span></tt></a></td>
</tr>
<tr><td><a href="http://docs.python.org/2/library/functions.html#complex" title="complex"><tt class="xref py py-func docutils literal"><span class="pre">complex()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#hash" title="hash"><tt class="xref py py-func docutils literal"><span class="pre">hash()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#min" title="min"><tt class="xref py py-func docutils literal"><span class="pre">min()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#func-set"><tt class="docutils literal"><span class="pre">set()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#apply" title="apply"><tt class="xref py py-func docutils literal"><span class="pre">apply()</span></tt></a></td>
</tr>
<tr><td><a href="http://docs.python.org/2/library/functions.html#delattr" title="delattr"><tt class="xref py py-func docutils literal"><span class="pre">delattr()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#help" title="help"><tt class="xref py py-func docutils literal"><span class="pre">help()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#next" title="next"><tt class="xref py py-func docutils literal"><span class="pre">next()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#setattr" title="setattr"><tt class="xref py py-func docutils literal"><span class="pre">setattr()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#buffer" title="buffer"><tt class="xref py py-func docutils literal"><span class="pre">buffer()</span></tt></a></td>
</tr>
<tr><td><a href="http://docs.python.org/2/library/functions.html#func-dict"><tt class="docutils literal"><span class="pre">dict()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#hex" title="hex"><tt class="xref py py-func docutils literal"><span class="pre">hex()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#object" title="object"><tt class="xref py py-func docutils literal"><span class="pre">object()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#slice" title="slice"><tt class="xref py py-func docutils literal"><span class="pre">slice()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#coerce" title="coerce"><tt class="xref py py-func docutils literal"><span class="pre">coerce()</span></tt></a></td>
</tr>
<tr><td><a href="http://docs.python.org/2/library/functions.html#dir" title="dir"><tt class="xref py py-func docutils literal"><span class="pre">dir()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#id" title="id"><tt class="xref py py-func docutils literal"><span class="pre">id()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#oct" title="oct"><tt class="xref py py-func docutils literal"><span class="pre">oct()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#sorted" title="sorted"><tt class="xref py py-func docutils literal"><span class="pre">sorted()</span></tt></a></td>
<td><a href="http://docs.python.org/2/library/functions.html#intern" title="intern"><tt class="xref py py-func docutils literal"><span class="pre">intern()</span></tt></a></td>
</tr>
</tbody>
</table>
</br>
</div>
<!-- ######################################################################## -->
</div>
</div>
</body> </html>