forked from UWPCE-PythonCert/ProgrammingInPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTutorial.html
More file actions
461 lines (344 loc) · 28.5 KB
/
Tutorial.html
File metadata and controls
461 lines (344 loc) · 28.5 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
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Python Tutorial — Programming in Python 7.0 documentation</title>
<link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<!--[if lt IE 9]>
<script src="../_static/js/html5shiv.min.js"></script>
<![endif]-->
<script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/language_data.js"></script>
<script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script type="text/javascript" src="../_static/js/theme.js"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search" style="background: #4b2e83" >
<a href="../index.html">
<img src="../_static/UWPCE_logo_full.png" class="logo" alt="Logo"/>
</a>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<p class="caption"><span class="caption-text">Topics in the Program</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../topics/01-setting_up/index.html">1. Setting up your Environment</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/02-basic_python/index.html">2. Basic Python</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/03-recursion_booleans/index.html">3. Booleans and Recursion</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/04-sequences_iteration/index.html">4. Sequences and Iteration</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/05-text_handling/index.html">5. Basic Text Handling</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/06-exceptions/index.html">6. Exception Handling</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/07-unit_testing/index.html">7. Unit Testing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/08-dicts_sets/index.html">8. Dictionaries and Sets</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/09-files/index.html">9. File Handling</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/10-modules_packages/index.html">10. Modules and Packages</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/11-argument_passing/index.html">11. Advanced Argument Passing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/12-comprehensions/index.html">12. Comprehensions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/13-intro_oo/index.html">13. Intro to Object Oriented Programing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/14-magic_methods/index.html">14. Properties and Magic Methods</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/15-subclassing/index.html">15. Subclassing and Inheritance</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/16-multiple_inheritance/index.html">16. Multiple Inheritance</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/17-functional_programming/index.html">17. Introduction to Functional Programming</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/18-advanced_testing/index.html">18. Advanced Testing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/99-extras/index.html">19. Extra Topics</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../index.html">Programming in Python</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content style-external-links">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="../index.html" class="icon icon-home"></a> »</li>
<li>Python Tutorial</li>
<li class="wy-breadcrumbs-aside">
<a href="../_sources/modules/Tutorial.rst.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<p>NOTE: this is only the start of a tutorial – it would be nice to complete it some day!</p>
<div class="section" id="python-tutorial">
<h1>Python Tutorial<a class="headerlink" href="#python-tutorial" title="Permalink to this headline">¶</a></h1>
<p>This is a tutorial to get you started. Not a lot of explanation,
but enough to get you going with writing basic python code.</p>
<p>This tutorial uses exclusively Python3.</p>
<p>You may want to also check out some other tutorial options:</p>
<ul class="simple">
<li><p><strong>Jessica McKeller’s beginning tutorial</strong> (Video)
<a class="reference external" href="https://www.youtube.com/watch?v=MirG-vJOg04">https://www.youtube.com/watch?v=MirG-vJOg04</a></p></li>
<li><p><strong>The Python Tutorial</strong>
(<a class="reference external" href="https://docs.python.org/3/tutorial/">https://docs.python.org/3/tutorial/</a>): This is the
official tutorial from the Python website. No more authoritative source is
available.</p></li>
<li><p><strong>Code Academy Python Track</strong>
(<a class="reference external" href="http://www.codecademy.com/tracks/python">http://www.codecademy.com/tracks/python</a>): Often
cited as a great resource, this site offers an entertaining and engaging
approach and in-browser work.</p></li>
<li><p><strong>Learn Python the Hard Way</strong>
(<a class="reference external" href="https://learnpythonthehardway.org/python3/">https://learnpythonthehardway.org/python3/</a>): Solid and gradual.
This course offers a great foundation for folks who have never
programmed in any language before. It used be be fully available
for free online – but now you can only get the first bits. But still
a good way to get started.</p></li>
</ul>
<div class="section" id="running-your-code">
<h2>Running Your Code<a class="headerlink" href="#running-your-code" title="Permalink to this headline">¶</a></h2>
<p>There are a number of ways to run python code:</p>
<ul class="simple">
<li><p>At the interpreter, often referred to as a REPL (Read, Evaluate, Print Loop)</p></li>
<li><p>At an enhanced interpreter such as iPython</p></li>
<li><p>In a browser-based interactive system such as the Jupyter Notebook</p></li>
<li><p>From an IDE, such as IDLE or PyCharm</p></li>
<li><p>Calling python from the command line to run a file.</p></li>
</ul>
<p>While working with an interactive interpreter can be an excellent way to explore Python (and I highly recommend it), For this tutorial, to get you used to “real” production development, you will write, edit, and save your code in a programmer’s text editor, and run it from the command line.</p>
<div class="section" id="a-programmer-s-text-editor">
<h3>A Programmer’s Text Editor<a class="headerlink" href="#a-programmer-s-text-editor" title="Permalink to this headline">¶</a></h3>
<p>See These notes for getting set up with an editor and Python itself: <a class="reference internal" href="../topics/01-setting_up/index.html#setting-up-dev-environment"><span class="std std-ref">Setting up your Environment</span></a></p>
</div>
<div class="section" id="the-python-interpreter">
<h3>The Python Interpreter<a class="headerlink" href="#the-python-interpreter" title="Permalink to this headline">¶</a></h3>
<p>Python is a “byte compiled, interpreted” language. What this means to you is that you need a Python interpreter to run your Python code. It also means that that is all you need – write some code, and run it with Python. That’s it.</p>
<p>There are a few Python interpreters (or run-times) available:</p>
<ul class="simple">
<li><p>cPython</p></li>
<li><p>PyPy</p></li>
<li><p>Jython</p></li>
<li><p>Iron Python</p></li>
<li><p>MicroPython</p></li>
</ul>
<p>These each have their own special uses for interaction the the Java VM or CLR, or running on micro controllers. But most production Python is run with the cPython interpreter, and most people mean cPython when they say “Python”.</p>
<p>For this tutorial, you will need cPython version 3.7 or 3.8, installed and running so that when you type “python” at your command line, it starts up:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>MacBook-Pro:~ Chris$ python
Python <span class="m">3</span>.7.4 <span class="o">(</span>v3.6.2:5fd33b5926, Jul <span class="m">16</span> <span class="m">2017</span>, <span class="m">20</span>:11:06<span class="o">)</span>
<span class="o">[</span>GCC <span class="m">4</span>.2.1 <span class="o">(</span>Apple Inc. build <span class="m">5666</span><span class="o">)</span> <span class="o">(</span>dot <span class="m">3</span><span class="o">)]</span> on darwin
Type <span class="s2">"help"</span>, <span class="s2">"copyright"</span>, <span class="s2">"credits"</span> or <span class="s2">"license"</span> <span class="k">for</span> more information.
>>>
</pre></div>
</div>
<p>Your result may be slightly different, but it should say Python 3. <em>something</em> there at the top, and give you the command prompt (<code class="docutils literal notranslate"><span class="pre">>>></span></code>) at the end.</p>
<p>You can get out of it by typing ctrl+D (on OS-X and Linux) or ctrl+Z (On Windows), or typing <code class="docutils literal notranslate"><span class="pre">exit()</span></code> and then <return>.</p>
</div>
<div class="section" id="your-first-program">
<h3>Your first “program”<a class="headerlink" href="#your-first-program" title="Permalink to this headline">¶</a></h3>
<p>Create your first program by typing this into your text editor:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="s2">"this worked!"</span><span class="p">)</span>
</pre></div>
</div>
<p>Type it exactly as above, with no extra space at the beginning, and no other characters.</p>
<p>Save the file as <code class="docutils literal notranslate"><span class="pre">first.py</span></code>. Make sure to save it somewhere that makes sense, maybe a directory you create for this purpose, called “tutorial”.</p>
<p>Start up the command line (“Terminal” on OS-X, “Command Prompt” on Windows), and “Navigate” to the directory where you just saved that file:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">cd</span> <span class="n">tutorial</span>
</pre></div>
</div>
<p>Now run your little program, by typing <code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">first.py</span></code> and hitting <return>. You should get something like this:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>MacBook-Pro:tutorial Chris$ python first.py
this worked!
MacBook-Pro:tutorial Chris$
</pre></div>
</div>
<p>If this is <em>NOT</em> what you got then something went wrong. Some things to check:</p>
<blockquote>
<div><ul class="simple">
<li><p>Did you save the file?</p></li>
<li><p>Is your command prompt “in” the same directory as the file?
- you can check this by typing <code class="docutils literal notranslate"><span class="pre">ls</span></code> on *nix, and <code class="docutils literal notranslate"><span class="pre">dir</span></code> on Windows, to see what files are in the dir that the command prompt is in.</p></li>
<li><p>Did you type <em>exactly</em> the same line as above?</p></li>
</ul>
</div></blockquote>
<div class="section" id="what-did-you-just-do">
<h4>What did you just do?<a class="headerlink" href="#what-did-you-just-do" title="Permalink to this headline">¶</a></h4>
<p>The “python” command starts up the python interpreter. If you “pass in” a file name, by typing the name of the file after “python”, then the interpreter will read that file and run any code that is in it.</p>
<p>In this case, python ran the one line of code you put in that file, which told it to print the text: “this worked!” – and that is what it did.</p>
</div>
</div>
<div class="section" id="the-print-function">
<h3>The print function<a class="headerlink" href="#the-print-function" title="Permalink to this headline">¶</a></h3>
<p>You can display just about anything in Python with the <code class="docutils literal notranslate"><span class="pre">print()</span></code> function. Simply type:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="n">what</span> <span class="n">you</span> <span class="n">want</span> <span class="n">to</span> <span class="nb">print</span><span class="p">)</span>
</pre></div>
</div>
<p>examples:</p>
<blockquote>
<div><p>print(45)
print(“this is a bit of text”)</p>
</div></blockquote>
<p>you can print more than one thing by separating them with commas, inside the parenthesis:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="s2">"the value of pi is:"</span><span class="p">,</span> <span class="mf">3.1459</span><span class="p">,</span> <span class="s2">"to four decimal places"</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="text-in-python">
<h3>Text in Python<a class="headerlink" href="#text-in-python" title="Permalink to this headline">¶</a></h3>
<p>Text in python is supported by the “str” datatype, which is short for “string”. The text datatype is often referred to as “strings” in computer science because it is a series, or string, of characters.</p>
<p>In Python3, strings can be any length, and contain any character (in virtually any language). This is because they support “Unicode” which is a system for representing all the characters of virtually all the languages used on earth.</p>
<p>There are many complications to full support of Unicode, but for the most part, in Python it “just works”. Any text you can put in your text editor should work fine.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>With Unicode, the actual characters can be stored in multiple ways in the files themselves. If you’ve heard of “plain text”, there really is no such thing anymore with Unicode. The exact way the text is stored is known as the “encoding”. These days, an encoding known and “utf-8” is the mast commonly used. Python assumes that you are using utf-8 encoded files, but if strange things happen, make sure your editor is using utf-8. (or ASCII, which is an older encoding that does not support multiple languages – but ASCII is subset of utf-8, so it still works.)</p>
</div>
<p>To create a str, you simply type what you want surrounded by either double or single quotes (the apostrophe).</p>
<p>Type this in a new file, called <code class="docutils literal notranslate"><span class="pre">strings.py</span></code>:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="s2">"This is a basic string"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">'This is exactly the same string'</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"You want to use double quotes if there's an apostrophe, like this: ' in the string"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">'You can use single quotes if you want to "quote" a word'</span><span class="p">)</span>
</pre></div>
</div>
<p>run the file, and you should get something like this:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>MacBook-Pro:tutorial Chris$ python strings.py
This is a basic string
This is exactly the same string
You want to use double quotes if there's an apostrophe, like this: ' in the string
You can use single quotes if you want to "quote" a word
</pre></div>
</div>
</div>
<div class="section" id="numbers-in-python">
<h3>Numbers in Python<a class="headerlink" href="#numbers-in-python" title="Permalink to this headline">¶</a></h3>
<p>Python supports two types of numbers: integers (int) – or “whole numbers”, with no fractional part:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="mi">3</span><span class="p">,</span> <span class="mi">123</span><span class="p">,</span> <span class="o">-</span><span class="mi">345</span><span class="p">,</span> <span class="mi">23473948</span>
</pre></div>
</div>
<p>integers can be negative or positive and as large as you want:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="mi">12345678987654321234567890987654321234567898765</span><span class="p">)</span>
<span class="go">12345678987654321234567890987654321234567898765</span>
</pre></div>
</div>
<p>“real numbers” are called “floating point” (float) numbers. They are internally stored as binary, but you write them as regular decimal (base 10) numbers:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="mf">2.3</span><span class="p">,</span> <span class="mf">3.0</span><span class="p">,</span> <span class="mf">3.2459</span><span class="p">,</span> <span class="o">-</span><span class="mf">23.21</span>
</pre></div>
</div>
<p>Note that while the integer`3` and the float <cite>3.0</cite> have the same value, they are different types of numbers. But for the most part, Python will convert from integer to floating point numbers for you (and back again), so this distinction is rarely important.</p>
</div>
<div class="section" id="math">
<h3>Math<a class="headerlink" href="#math" title="Permalink to this headline">¶</a></h3>
<p>Being a computer language, Python, of course, supports the regular math functions. Type the following into a file named math.py and run it:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="mi">3</span> <span class="o">*</span> <span class="mi">4</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="mi">3</span> <span class="o">*</span> <span class="mi">4</span> <span class="o">+</span> <span class="mi">10</span> <span class="o">-</span> <span class="mi">2</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"twelve divided by 5 is:"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="mi">12</span> <span class="o">/</span> <span class="mi">5</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"twelve divided by 5 is:"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="mi">12</span> <span class="o">//</span> <span class="mi">5</span><span class="p">)</span>
</pre></div>
</div>
<p>What is the difference between <code class="docutils literal notranslate"><span class="pre">12</span> <span class="pre">/</span> <span class="pre">5</span></code> and <code class="docutils literal notranslate"><span class="pre">12</span> <span class="pre">//</span> <span class="pre">5</span></code> ? Run your this code and find out.</p>
</div>
<div class="section" id="order-of-operations">
<h3>Order of Operations<a class="headerlink" href="#order-of-operations" title="Permalink to this headline">¶</a></h3>
<p>Python follows the standard rules of “operator precedence” from algebra – which operations are performed first when there are a bunch in a row:</p>
<p><a class="reference external" href="https://en.wikipedia.org/wiki/Order_of_operations">https://en.wikipedia.org/wiki/Order_of_operations</a></p>
<p>Add this to the <code class="docutils literal notranslate"><span class="pre">math.py</span></code> file:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="mi">3</span> <span class="o">+</span> <span class="mi">4</span> <span class="o">/</span> <span class="mi">2</span><span class="p">)</span>
</pre></div>
</div>
<p>run the file, and see if you get the answer you expect. The result should be 5.0, not 6.0.</p>
<p>That is because multiplication and division are a higher priority than addition, so Python divided 4 by 2 to get 2.0, and then added 3 + 2.0 to get 5.0.</p>
<p>Always keep that in mind when you do math expressions in Python. If you want to change the order of operations, you can group them with parentheses. Try adding this to the <code class="docutils literal notranslate"><span class="pre">math.py</span></code> file and run it:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="mi">3</span> <span class="o">+</span> <span class="p">(</span><span class="mi">4</span> <span class="o">/</span> <span class="mi">2</span><span class="p">))</span>
<span class="nb">print</span><span class="p">((</span><span class="mi">3</span> <span class="o">+</span> <span class="mi">4</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span><span class="p">)</span>
</pre></div>
</div>
<p>Python will always evaluate what is in parentheses first.</p>
</div>
<div class="section" id="variables">
<h3>Variables<a class="headerlink" href="#variables" title="Permalink to this headline">¶</a></h3>
<p>Directly printing things is not all that useful – though Python does make a good calculator!</p>
<p>To do anything more complicated, you need to store values to be used later. We do this by “assigning” them to a “variable”, essentially giving them a name. Save the following in a <code class="docutils literal notranslate"><span class="pre">variables.py</span></code> file:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">x</span> <span class="o">=</span> <span class="mi">5</span>
<span class="n">y</span> <span class="o">=</span> <span class="mi">20</span>
<span class="n">z</span> <span class="o">=</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"the value of z is: "</span><span class="p">,</span> <span class="n">z</span><span class="p">)</span>
</pre></div>
</div>
<p>The equals sign: <code class="docutils literal notranslate"><span class="pre">=</span></code> is the “assignment operator”. It assigns a value to a name, and then when you use the name in the future, Python will replace it with the value it is assigned to when it is used.</p>
<p>Names can (and generally should) be long and descriptive, and can contain letters, numbers (but not at the beginning) and only a few symbols, like the underscore character:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">rectangle_width</span> <span class="o">=</span> <span class="mi">200</span>
<span class="n">rectangle_height</span> <span class="o">=</span> <span class="mi">23</span>
<span class="n">rectangle_area</span> <span class="o">=</span> <span class="n">rectangle_width</span> <span class="o">*</span> <span class="n">rectangle_height</span>
</pre></div>
</div>
</div>
<div class="section" id="comments">
<h3>Comments<a class="headerlink" href="#comments" title="Permalink to this headline">¶</a></h3>
<p>Try running this code:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="s2">"this"</span><span class="p">)</span>
<span class="c1"># print ("that")</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"the other"</span><span class="p">)</span>
</pre></div>
</div>
<p>What does it print?</p>
<p>“that” didn’t print because the “#” symbol (the hash) tells python not to run any code after it on that line.</p>
<p>How about this?</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="s2">"this"</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">"that"</span><span class="p">)</span> <span class="n">I</span> <span class="n">think</span> <span class="n">we</span> <span class="n">need</span> <span class="n">this</span> <span class="n">line</span> <span class="n">too</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"the other"</span><span class="p">)</span>
</pre></div>
</div>
<p>And this?</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># Here we are printing useless stuff:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"this"</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">"that"</span><span class="p">)</span> <span class="c1"># I think we need this line too</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"the other"</span><span class="p">)</span>
</pre></div>
</div>
<p>comments can come after running code on a line as well. Using the hash to “comment out” parts of code is used in two ways:</p>
<ol class="arabic simple">
<li><p>To add a little extra description to some code, to explain what it doing.</p></li>
<li><p>To temporarily disable some code</p></li>
</ol>
</div>
</div>
</div>
</div>
</div>
<footer>
<hr/>
<div role="contentinfo">
<p>
© Copyright 2020, University of Washington, Natasha Aleksandrova, Christopher Barker, Brian Dorsey, Cris Ewing, Christy Heaton, Jon Jacky, Maria McKinley, Andy Miles, Rick Riehle, Joseph Schilz, Joseph Sheedy, Hosung Song. Creative Commons Attribution-ShareAlike 4.0 license
</p>
</div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a
<a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a>
provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
</body>
</html>