-
Notifications
You must be signed in to change notification settings - Fork 2
/
setup.html
executable file
·300 lines (162 loc) · 18.3 KB
/
setup.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
<!DOCTYPE HTML>
<html>
<head>
<title>Setup</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="/static/favicon.ico?v=1">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Raleway:400,500">
<link rel="stylesheet" href="/static/css/main.css">
<link rel="stylesheet" href="/static/css/button.css">
<link rel="stylesheet" href="/static/css/collapsible.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Work+Sans:400,700">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=PT+Mono">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.1.0/css/all.css">
<link rel="stylesheet" href="/static/css/textbook.css">
<link rel="stylesheet" href="/static/css/navbar.css">
<link rel="stylesheet" href="/static/css/dropdown.css">
<link rel="stylesheet" href="/static/css/slider.css">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="/static/js/textbook.js"></script>
<script src="/static/js/display.js"></script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
processEscapes: true
}
});
MathJax.Hub.Register.StartupHook("MathMenu Ready",function () {
MathJax.Menu.BGSTYLE["z-index"] = 1;
});
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
</head>
<div id="navbar" class="navbar">
<div class="page">
<a href="/">
<i class="fas fa-tree"></i>
</a>
<div class="dropdown">
<button class="dropbtn" onclick="toggleDisplay('chapter-dropdown', 'none', 'block')">
More Chapters
</button>
<div id="chapter-dropdown" class="dropdown-content" style="display: none;">
<a href="/techniques-in-computer-science/welcome-to-computer-science.html">Welcome to Computer Science</a>
<a href="/techniques-in-computer-science/setup.html">Setup</a>
<a href="/techniques-in-computer-science/variables.html">Variables</a>
<a href="/techniques-in-computer-science/functions.html">Functions</a>
<a href="/techniques-in-computer-science/booleans.html">Booleans</a>
<a href="/techniques-in-computer-science/iteration.html">Iteration</a>
<a href="/techniques-in-computer-science/higher-order-functions.html">Higher-Order Functions</a>
<a href="/techniques-in-computer-science/lambda-functions.html">Lambda Functions</a>
</div>
</div>
<a href="javascript:void(0);" class="icon"
onclick="toggleClass('navbar', 'navbar', 'responsive');">
☰
</a>
</div>
</div>
<body>
<div class="page content">
<h1 id="the-terminal">The terminal</h1>
<p>The terminal is an app on your computer. It's maybe the most useful one, because you can use it to tell your computer to do pretty much whatever you want. We'll just go over the basics.</p>
<h2 id="opening-the-terminal">Opening the terminal</h2>
<p>First, we need to find the terminal app.</p>
<ul>
<li><strong>On a Mac:</strong> Press <span class="keys"><kbd class="key-command">Cmd</kbd><span>+</span><kbd class="key-space">Space</kbd></span>. This will open up the Spotlight Search. Type "terminal", and you should see the app icon pop up. Double click to open it.</li>
<li><strong>On Windows:</strong> This is a bit tricker, since Microsoft is a rebel and doesn't support UNIX. You'll need to install <a href="https://git-scm.com/downloads">Git Bash</a>. During installation, select the option "Use Windows' default console window".</li>
</ul>
<p>Now that you're all set up with the terminal, make sure you can readily access it. For Mac users, I recommend you right click the app icon and select "Options: Keep in Dock". You will be using this app a <em>lot</em> in this class, and even more if you are pursuing computer science as a major.</p>
<h2 id="your-home-directory">Your home directory</h2>
<p>A directory is a folder on your computer, like "Downloads" or "Documents". Your home directory is the main folder, the one that contains everything in your computer profile. Let's go find it.</p>
<ul>
<li><strong>On a Mac:</strong> You've probably never encountered your home directory in Finder. Since most people never need to access it, Apple sort of hides it from you. The good news is, it's easy to find! Open Finder. On the left, you should see some of the folders you own, like "Downloads". Click on one of those. Then right click the icon that appears at the top of the Finder window. You'll see a little menu appear, as in this screenshot:
<img src="/static/img/the-terminal.png">
Click the directory with the picture of the house. This takes you to your home directory. After entering your home directory, drag the house from the top of your Finder window to the menu on the left. That way you can always access your home directory in a snap. In the screenshot above, I put my home directory right above the folder called "sequoia-tree.github.io".</li>
<li><strong>On Windows:</strong> From the Windows start menu, you should see a folder called "My Computer", or in newer versions of Windows, "This PC". That's actually your home directory.</li>
</ul>
<h2 id="navigating-with-the-terminal">Navigating with the terminal</h2>
<p>When you open the terminal, you probably see something like this:</p>
<pre class="highlight"><code>MacBook-Pro:~ sequoia$</code></pre>
<p>This is called your bash prompt. It's where you type text into the terminal. The part after the colon tells us what directory we're in now, and the squiggly denotes your home directory. (So in the example above, I'm in my home directory.) This is the same folder you opened in Finder (or the Windows Explorer) just a minute ago. Whenever you open the terminal, you will be in your home directory.</p>
<p>In order to see the contents of your current directory, use the <code>ls</code> command, short for "list". Here's what I see when I type it into my bash prompt:</p>
<pre class="highlight"><code>MacBook-Pro:~ sequoia$ ls
Desktop/ Movies/ Todo/
Documents/ Music/ env/
Downloads/ Pictures/ sequoia-tree.github.io/
Library/ Public/</code></pre>
<p>These are all the contents of my home directory. You can see folders for my downloads, documents, todo list, and some other miscellaneous items. <code>ls</code> just shows you the contents of whatever directory you're in.</p>
<p>We're almost ready to navigate through your computer using only the terminal. The last step is learning how to move between directories. How do we go from my home directory to my "Todo" folder, for instance? This is where the <code>cd</code> command comes in. It's short for "change directory". Here's an example:</p>
<pre class="highlight"><code>MacBook-Pro:~ sequoia$ cd Todo
MacBook-Pro:~/Todo sequoia$ ls
CS-186-Textbook/ Life.md
CS-370/ Pyagram/
CS-61A-Fall-2018/ Website/
CS-81A-Curriculum/ untitled.md
CS-81A-Textbook/</code></pre>
<p>Notice that after the colon, I now see <code>~/Todo</code>. That's the computer's way of saying I'm in my <code>Todo</code> directory, which is in my home directory. Whatever folder you end up in, your computer will always display the path from home. You can always get back home by running <code>cd ~</code> or <code>cd ~/</code>. (The slash is used to separate the names of different folders in your path, as you can see in the next example.) Let's try out our cool new skills:</p>
<pre class="highlight"><code>MacBook-Pro:~ sequoia$ cd Documents/UC-Berkeley
MacBook-Pro:~/Documents/UC-Berkeley sequoia$ ls
Year-1/ Year-2/ Year-3/
MacBook-Pro:~/Documents/UC-Berkeley sequoia$ cd Year-2
MacBook-Pro:~/Documents/UC-Berkeley/Year-2 sequoia$</code></pre>
<p>We say the working directory is whatever folder you are currently in. In this example, my working directory was initially my home directory. Next it was my "UC-Berkeley" folder which I keep inside "Documents", and after that it was the folder called "Year-2".</p>
<p>Sometimes you may also want to go "back", for example from your documents folder to your home directory. You can do that like so:</p>
<pre class="highlight"><code>MacBook-Pro:~/Documents sequoia$ cd ..
MacBook-Pro:~ sequoia$</code></pre>
<h2 id="staying-organized">Staying organized</h2>
<p>In this class there will be lots of files to keep track of, so it will be wise to make a folder for all your assignments. Since I obsessively nest folders inside other folders, I keep a separate folder for each year, semester, and class. For instance, my CS 61A work is all under <code>~/Documents/UC-Berkeley/Year-1/Fall-2016/CS-61A</code>. I also have subfolders for homework, lab, and projects, but that's just my preference. Take a moment now to make your own folder for this class, wherever you think it's convenient. (I notice most folks like to keep it on their desktop, so they don't even have to open Finder to access it.)</p>
<p>Over the course of this class, you will download your assignments from the class website. Make sure to keep them all in the folder you just set up, so that you can find all your work in one organized location on your computer. I have worked with a lot of students who did not heed this advice, and they usually end up with multiple copies of a single homework assignment floating around, not knowing wich copy is the one they're editing or the one they submitted. Please keep organized, so you don't make the same mistake.</p>
<h2 id="shortcuts">Shortcuts</h2>
<p>After you set up your directory for this class, try navigating to it using the <code>cd</code> command in the terminal. You might realize it's a bit of a hassle, and certainly not something you want to be doing several times a day, let alone several times a week. There are two good solutions.</p>
<ul>
<li><strong>Drag and drop:</strong> Try opening up a new terminal window. Without pressing <span class="keys"><kbd class="key-enter">Enter</kbd></span>, type <code>cd</code> into the bash prompt, and then type a single space. Also navigate to your new class folder using Finder. Drag and drop the icon from the top of the Finder window, into your terminal window. You'll notice the file path automatically shows up. Press <span class="keys"><kbd class="key-enter">Enter</kbd></span> to run the command.</li>
<li><strong>Aliasing:</strong> This method is a bit more technical, but also more convenient. It's totally up to you whether you want to do it or not. Also note I have only tried this on a Mac and I'm sure the process is a bit different for Windows computers. First go to your home directory in the terminal. We're going to access an "invisible" file called <code>.bash_profile</code>, which gets run every time you open up the terminal. (Invisible files are just files that Apple hid from you because most people don't need to access them.)
<pre class="highlight"><code>MacBook-Pro:~ sequoia$ open .bash_profile</code></pre>
This should open <code>.bash_profile</code> in a text editor like TextEdit. Now, on any blank line, go ahead and set up what's called an alias. Here's an example from my <code>.bash_profile</code>:
<pre class="highlight"><code>alias ucb='cd ~/Documents/UC-Berkeley/Year-2/Spring-2018'</code></pre>
Notice there are no spaces around the equals sign. Also, the quotes are <em>unformatted single quotes</em>. You can copy and paste them from the snippet of code above. Just replace <code>ucb</code> with whatever you want your custom command to be, as long as it is only one word, and replace the path with wherever you put your folder for this class. Then save your changes to <code>.bash_profile</code> and restart the terminal app. Just like that, you made your own custom terminal command! Here's an example using the one I defined above:
<pre class="highlight"><code>MacBook-Pro:~ sequoia$ ucb
MacBook-Pro:~/Documents/UC-Berkeley/Year-2/Spring-2018 sequoia$</code></pre></li>
</ul>
<p>Here's another nice shortcut. Often, you'll want to run a command in the terminal that you recently typed before. You can save time by cycling through your previous commands using the up-arrow key.</p>
<h1 id="python">Python</h1>
<p>Okay, so learning about the terminal is pretty dry stuff. Now let's get set up with the much cooler aspect of this class: Python. This is a computer programming language named after the species of tropical snake that invented it.</p>
<h2 id="installing-python">Installing Python</h2>
<p>Python is available for free download online. Here's how to install it:</p>
<ul>
<li><strong>On a Mac:</strong> Download and run <a href="https://www.python.org/downloads/mac-osx">the Python installer for Mac OS</a>.</li>
<li><strong>On Windows:</strong> Download and run <a href="https://www.python.org/downloads/windows">the Python installer for Windows</a>. Make sure to check the box labeled "Add Python 3.6 to PATH".</li>
<li><strong>On Linux:</strong> From the terminal, run this command: <code>sudo apt-get install python3</code>.</li>
</ul>
<p>After you run the installer, you should be all set up! Let's test it out. In the terminal, run the command <code>python3</code>. (If you're using Windows, the command might simply be <code>python</code>.) You should see a Python interpreter appear, which looks something like this:</p>
<pre class="highlight"><code>Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>></code></pre>
<p>Verify that at the beginning of the first line, you see version 3.5.1 or later. If you're on a Mac, running <code>python</code> may open an interpreter for version 2.7.10 so make sure you always use the command <code>python3</code>.</p>
<h2 id="the-python-interpreter">The Python interpreter</h2>
<p>When you open the Python interpreter from the terminal, you should see 3 greater-than symbols at the far left. You can type Python code here, and run it. In this chapter we won't talk about all the awesome stuff you can do with Python, so for now just try this to make sure everything is working:</p>
<pre class="highlight"><code>>>> import this</code></pre>
<p>You should see a little easter-egg put in by the creators of Python. It likely won't make much sense right now, but maybe by the end of the class you'll understand what it's all about.</p>
<p>Whenever you want to test out some code, you should try it in the Python interpreter. It's a very useful tool for experimenting and answering your own questions about how Python works. Don't forget about it. Students often contact me with questions they could've easily answered on their own, just by trying things out in the interpreter. And while I'm perfectly happy to be of service, it's a lot more convenient for you (and helps you develop your debugging skills) if you try to figure things out alone first.</p>
<p>Also, keep in mind that the Python interpreter is not the same as the terminal. When you see your bash prompt with the dollar sign, you're in the terminal. When you see the 3 greater-than symbols, you're in the Python interpreter. To open the Python interpreter from the terminal, run <code>python3</code> or <code>python</code> as we saw earlier. To exit the Python interpreter and get back to the terminal, run <code>exit()</code> or press <span class="keys"><kbd class="key-control">Ctrl</kbd><span>+</span><kbd class="key-d">D</kbd></span>.</p>
<h1 id="writing-code">Writing code</h1>
<p>Code is sort of like an essay, but the kind of essay your computer can understand. Technically you could write all your code in the terminal, but that would be like writing an essay in pen on your first draft: hard to edit, and pretty frustrating. Most of the time you write code in a text document, like you would with an essay. You just can't use the editors you're familiar with (Pages, Microsoft Word, etc.) because they store formatted text instead of unformatted text.</p>
<h2 id="picking-a-text-editor">Picking a text editor</h2>
<p>It's time to get you set up with a text editor suitable for writing code. There are a few to choose from, and we'll talk more about this later, but for now I recommend <a href="https://atom.io">Atom</a> or <a href="https://www.sublimetext.com">Sublime</a>. They're both great minimal code editors appropriate for getting started. As with the terminal, you'll be using your text editor frequently so make sure you can readily access it.</p>
<p>Conveniently, you can open a folder or text file in either of these editors, simply by dragging and dropping it onto the app icon.</p>
<h2 id="running-code-from-a-file">Running code from a file</h2>
<p>Using either of these code editors, make a Python file. This will be a document ending with the extension ".py". Save it to a directory that's easy to access, like your documents folder, or else it will be hard to find. You can write Python code in this file, just like you would write an essay in a text file. If you want to try it out now, write <code>print('Yowzah!')</code>. Don't worry about what this does yet, since we still haven't started learning how to write code. When you're done, be sure to save your work. Then you can run your code from the terminal using <code>python3</code> (or <code>python</code> if you're on Windows), like so:</p>
<pre class="highlight"><code>MacBook-Pro:~/Documents sequoia$ python3 my_file.py</code></pre>
<p>In order for this to work, you have to be in the same directory as the file you're trying to run. In the example above, <code>my_file.py</code> would have to be in my documents folder.</p>
<p>Please make sure to always save the file you're writing, before you try running it. This is just like how you want to save an essay you're writing, before you try printing it. Far too often, students get really confused because they're running a file but they haven't saved their changes to it yet. Don't make that mistake.</p>
<p>Now you're all set up and ready to get cracking! The awesome computer science starts in the next chapter.</p>
<br><br>
</div>
<script src="/static/js/slider.js"></script>
<script src="/static/js/collapsible.js"></script>
</body>
</html>