-
Notifications
You must be signed in to change notification settings - Fork 4
/
python.html
415 lines (338 loc) · 17.3 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>CS 4440</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/jpswalsh/academicons/css/academicons.min.css">
<link rel="stylesheet" href="css/main.css"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.1/styles/default.min.css">
<script src="https://kit.fontawesome.com/982c2a20d7.js" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.1/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
</head>
<body>
<nav class="navbar navbar-inverse">
<a class="navbar-brand" href="http://cs.utah.edu">
<img class="button" width="185px" src="../../../img/ksoclogo_white.png" style="margin-top:1.3%">
</a>
<div class="ml-auto">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav" style="margin-top: 1px;">
<li><a href="../">Syllabus</a></li>
<li><a href="../schedule">Schedule</a></li>
<li><a href="../assignments">Assignments</a></li>
<li><a href="../wiki">Wiki</a></li>
<li><a href="../redirect_piazza">Piazza</a></li>
<li><a href="../redirect_canvas">Canvas</a></li>
<li><a href="../redirect_pollev">PollEv</a></li>
<li><a href="../redirect_taqueue">TA Queue</a></li>
</ul>
</div>
</div>
</nav>
<!-- ################################################################################### -->
<div class="container main-container">
<div class="col-md-10">
<h1>CS 4440 Wiki: <br class="on-narrow"><b style="color:dodgerblue">Python 3 Cheat Sheet</b></h1><br>
<!-- ################################################################################### -->
<p>Below is an abridged cheat sheet of Python 3 fundamentals that you'll use in this course.</p>
<p><b>This page is by no means comprehensive—we encourage you to bookmark and familiarize yourself with one of the many in-depth Python tutorials on the web.</b> Some great examples are:</p>
<ul>
<li><a href="https://docs.python.org/3/contents.html">The Official Python Docs</li>
<li><a href="https://www.learnpython.org/">LearnPython.org</a></li>
<li><a href="https://developers.google.com/edu/python">Google's Python Class</a></li>
</ul>
<!-- ################################################################################### -->
<hr><h2 id=run>Running Python Code</h2>
<h3 id=run#interactive><b>Interactive Mode:</b></h3>
<p>In some course exercises, we'll walk you through examples demonstrated in Python's <b>interactive mode</b>. Think of interactive mode as a Python "session", where you write programs line-by-line (rather than all at once) and get feedback as each line is processed and executed.</p>
<p>To launch interactive mode, run <code>python3</code> in your terminal. An example session is below:</p>
<pre><code class="language-python"
>$ python3
>>> print("Hello from the interpreter!")
Hello f​rom the interpreter!
>>> exit()
</code></pre>
<h3 id=run#scripts><b>Writing and Running Scripts:</b></h3>
<p>The most common way that you'll be using Python is writing and running your own programs (or more technically, "scripts"). After editing your Python program (any IDE or text editor will work), save it as a <code>.py</code> file. Then, run it by calling <code>$ python3 /path/to/your/script.py</code>. An example is below:</p>
<pre><code class="language-python"
>$ echo 'print("Hello from a script!")' > example.py
$ python3 example.py
Hello f​rom a script!
</code></pre>
<h3 id="run#debug"><b>Debugging Code:</b></h3>
<p>Occasionally your code might not function as expected. Thankfully, Python provides descriptive <b>error messages</b> to help you narrow down where your code is incorrect. For example:</p>
<pre><code class="language-python"
>>>> print(os.path)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
</code></pre>
<p>Here, we see error <code>NameError: name 'os' is not defined</code>. We can pinpoint that our mistake is a failure to define module <code>os</code>. A quick Google search turns up that this is indeed a core Python library, and that the fix is probably to just prepend the line <code>import os</code> at the beginning of our code.</p>
<p>To be successful in this course, <b>you'll need to get comfortable with debugging</b>. Often, the right fix will take you just a few minutes to find via research on Google, some trial-and-error fixing, adding a few <code>print()</code> statements, etc. Before you reach out to the course staff or ask on Piazza, we expect that <b>you've spent some time trying to debug</b> your own code <i>first</i>. To help others best help you, be sure to describe in detail <b>what you've tried</b>, what you think might be wrong, what solutions didn't work, etc.</p>
<p>Debugging is a skill you'll use all throughout your software development career—get comfortable with it!</p>
<!-- ################################################################################### -->
<hr><h2 id=mods>Modules and Libraries</h2>
<p>In many course exercises, you'll borrow functionality from third-party modules (files ending in <code>.py</code>) or libraries (collections of modules). These modules may contain useful variables (e.g., <code>os.<b><code>name</code></b></code>) or functions (e.g., <code>os.<b><code>uname()</code></b></code>). We'll cover the three main ways to import and use such objects below.</p><br>
<p>Note that in all of CS 4440, you may only use <b>default Python libraries</b> (the ones that come pre-installed in your Linux VM) and/or <b>modules that we provide you</b>. We won't install any others when grading your work, which means your code will fail—and you'll lose points—if you rely on any external dependencies.</p>
<h3 id="mods#import"><b>Importing Modules:</b></h3>
<pre><code class="language-python"
>>>> import os # Import the `os` module.
>>> print(os.name) # To access objects, we use prefix "os.OBJNAME".
posix
>>> print(os.uname()) # This applies to all objects. E.g., `uname()`.
posix.uname_result(sysname='Linux', ...)
</code></pre>
<h3 id="mods#import#one"><b>Importing Specific Objects:</b></h3>
<pre><code class="language-python"
>>>> from os import name # Import variable `name` from the `os` module.
>>> print(name) # We no longer need a prefix to access `name`!
posix
>>> print(uname()) # This fails. Why? We've only imported `name`!
NameError: name 'uname' is not defined. Did you mean: 'name'?
>>> print(os.uname()) # Also fails. Why? We didn't import the module!
NameError: name 'os' is not defined
</code></pre>
<h3 id="mods#import#all"><b>Importing All Objects:</b></h3>
<pre><code class="language-python"
>>>> from os import * # The '*' imports every object from the module.
>>> print(name) # As before, we no longer need to prefix objects!
posix
>>> print(uname()) # We can now access all objects from the module!
posix.uname_result(sysname='Linux', ...)
</code></pre>
<!-- ################################################################################### -->
<hr><h2 id=vars>Variables</h2>
<h3 id="vars#init"><b>Initializing:</b></h3>
<pre><code class="language-python"
>>>> x = 5 # We've declared variable `x` with value 5.
>>> print(type(x)) # Python correctly infers it is an integer!
<​c​lass 'int'>
>>> x = "cs4440" # Let's now overwrite `x` with a string.
>>> print(type(x)) # Notice how its type changes accordingly!
<​c​lass 'str'>
</code></pre>
<h3 id="vars#cast"><b>Casting:</b></h3>
<pre><code class="language-python"
>>>> x = int(5) # Let's cast `x` as an `int` (integer).
>>> print(x) # Now print it — it's indeed an integer!
5
>>> x = float(5) # Now cast it as a `float` type!
>>> print(x) # Here we go — it is indeed a float!
5.0
</code></pre>
<!-- ################################################################################### -->
<hr><h2 id=strs>String Manipulation</h2>
<h3 id="strs#length"><b>Length of a String:</b></h3>
<pre><code class="language-python"
>>>> x = "cs4440" # Use `len()` to get a string's length.
>>> print(len(x))
6
</code></pre>
<h3 id="strs#append"><b>Appending to a String:</b></h3>
<pre><code class="language-python"
>>>> x = "cs4440" # Append ' is cool' to string 'cs4440'.
>>> x += " is cool"
>>> print(x)
cs4440 i​s cool
</code></pre>
<h3 id="strs#substrs"><b>Substrings:</b></h3>
<pre><code class="language-python"
>>>> x = "cs4440 is cool" # Check if substring 'cool' is present.
>>> print("cool" in x)
T​rue
>>> print("4440" not in x) # Check if substring '4440' not present.
F​alse
</code></pre>
<h3 id="strs#split"><b>Splitting by a Delimiter:</b></h3>
<pre><code class="language-python"
>>>> x = "cs4440:cool" # Use delimiter ':' to split into a list.
>>> print(x.split(':'))
['cs4440', 'cool']
</code></pre>
<h3 id="strs#strip"><b>Stripping Characters:</b></h3>
<pre><code class="language-python"
>>>> x = " cs4440 " # Strip any leading / trailing whitespace.
>>> print(x.strip())
cs4440
>>> x = "cs4440" # Strip the substring '4440' from our string.
>>> print(x.strip('4440'))
cs
</code></pre>
<h3 id="strs#repeat"><b>Repeating Characters:</b></h3>
<pre><code class="language-python"
>>>> x = "A"*10 # You can use multiplication to quickly
>>> print(x) # generate strings of repeated characters.
AAAAAAAAAA
</code></pre>
<h3 id="strs#bytes"><b>Byte Strings:</b></h3>
<pre><code class="language-python"
>>>> x = b'cs4440' # Python byte strings are wrapped by `b''`.
>>> print(x.decode('utf-8')) # Use `decode()` to convert them to strings.
cs4440
>>> x = "cs4440" # To convert a string to a byte string, you
>>> print(x.encode('utf-8')) # can similarly use the `encode()` function.
b'cs4440'
</code></pre>
<!-- ################################################################################### -->
<hr><h2 id=lists>List Manipulation</h2>
<h3 id="lists#index"><b>Indexing:</b></h3>
<pre><code class="language-python"
>>>> x = ['cs4440', 'is', 'cool'] # Print the 0th item of our list.
>>> print(x[0])
cs4440
>>> x = ['cs4440', 'is', 'cool'] # Print the last item of our list.
>>> print(x[-1])
cool
</code></pre>
<h3 id="lists#insert"><b>Inserting:</b></h3>
<pre><code class="language-python"
>>>> x = ['cs4440', 'is', 'cool'] # Overwrite the last item with 'fun'.
>>> x[-1] = 'fun'
>>> print(x)
['cs4440', 'is', 'fun']
>>> x.insert(2, 'super') # Insert string 'super' in index two.
>>> print(x)
['cs4440', 'is', 'super', 'fun']
</code></pre>
<h3 id="lists#join"><b>Joining:</b></h3>
<pre><code class="language-python"
>>>> x = ['cs4440', 'is', 'cool'] # Join items into a space-delimited string.
>>> print(' '.join(x))
cs4440 i​s cool
>>> y = ['all', 'day'] # Joins list y to our previous list x.
>>> print(x + y)
['cs4440', 'is', 'super', 'cool', 'all', 'day']
</code></pre>
<!-- ################################################################################### -->
<hr><h2 id=conds>Conditional Statements</h2>
<h3 id="conds#if"><b>If Statements:</b></h3>
<pre><code class="language-python"
>>>> x = 5
>>> if (5 % 2 == 1): # Evaluates to True if x modulo 2 equals 1.
... print("Yes!") # Prints string "Yes!" if condition is True.
Yes!
</code></pre>
<h3 id="conds#else"><b>Else Statements:</b></h3>
<pre><code class="language-python"
>>>> x = 5
>>> if (x % 3 == 1): # Evaluates to True if x modulo 3 equals 1.
... print("Yes!")
... else: # Prints "Nope!" if the condition is False.
... print("Nope!")
Nope!
</code></pre>
<!-- ################################################################################### -->
<hr><h2 id=loops>Loops</h2>
<h3 id="loops#for"><b>For Loops:</b></h3>
<pre><code class="language-python"
>>>> x = ['a', 'b', 'c'] # For every item `y` in list `x`...
>>> for y in x:
... print(y)
a
b
c
</code></pre>
<h3 id="loops#while"><b>While Loops:</b></h3>
<pre><code class="language-python"
>>>> x = 3
>>> while x != 0: # While x is not equal to 0...
... print(x) # Print x and then decrement it.
... x -= 1
3
2
1
</code></pre>
<!-- ################################################################################### -->
<hr><h2 id=funcs>Functions</h2>
<h3 id="funcs#defs"><b>Defining Functions:</b></h3>
<pre><code class="language-python"
>>>> def foo(): # Definition of function `foo()`.
... print("Hello!")
... return
>>> def bar(x, y): # Definition of function `bar()`,
... print(x+y) # which expects two arguments.
... return
</code></pre>
<h3 id="funcs#call"><b>Calling Functions:</b></h3>
<pre><code class="language-python"
>>>> foo() # Call foo(), which has no arguments.
Hello!
>>> bar(4000,440) # Call bar(), which has two arguments.
4440
</code></pre>
<!-- ################################################################################### -->
</div>
<div class="col-md-2 text-left scrollable" style="position:static;">
<div class="col-md-12 text-left scrollable" style="position:fixed;">
<h4><b>Table of Contents:</b></h4>
<ul>
<li><a href="#run">Running Code</a></li>
<ul>
<li><a href="#run#interactive">Interactive Mode</a></li>
<li><a href="#run#scripts">Running Scripts</a></li>
<li><a href="#run#debug">Debugging</a></li>
</ul>
<li style="margin-top:1ex"><a href="#mods">Modules</a></li>
<ul>
<li><a href="#mods#import">Importing</a></li>
</ul>
<li style="margin-top:1ex"><a href="#vars">Variables</a></li>
<ul>
<li><a href="#vars#init">Initializing</a></li>
<li><a href="#vars#cast">Casting</a></li>
</ul>
<li style="margin-top:1ex"><a href="#strs">Strings</a></li>
<ul>
<li><a href="#strs#length">Length</a></li>
<li><a href="#strs#append">Appending</a></li>
<li><a href="#strs#substrs">Substrings</a></li>
<li><a href="#strs#split">Splitting</a></li>
<li><a href="#strs#strip">Stripping</a></li>
<li><a href="#strs#repeat">Repeating</a></li>
<li><a href="#strs#bytes">Byte Strings</a></li>
</ul>
<li style="margin-top:1ex"><a href="#lists">Lists</a></li>
<ul>
<li><a href="#lists#index">Indexing</a></li>
<li><a href="#lists#insert">Inserting</a></li>
<li><a href="#lists#join">Joining</a></li>
</ul>
<li style="margin-top:1ex"><a href="#conds">Conditionals</a></li>
<ul>
<li><a href="#conds#if">If and Else</a></li>
</ul>
<li style="margin-top:1ex"><a href="#loops">Loops</a></li>
<ul>
<li><a href="#loops#for">For and While</a></li>
</ul>
<li style="margin-top:1ex"><a href="#funcs">Functions</a></li>
<ul>
<li><a href="#funcs#defs">Definitions</a></li>
<li><a href="#funcs#call">Calling</a></li>
</ul>
</ul>
</h4>
</div>
</div>
</div>
<!-- ################################################################################### -->
<footer class="bg-light text-center text-lg-start">
<div class="text-center p-3" style="background-color: rgba(0, 0, 0, 0.2); font-size: 0.75em;">
Copyright © Stefan Nagy. All rights reserved.
</div>
</footer>
<!-- ################################################################################### -->
</body>
</html>