Launch `Terminal.app` (Mac) or whatever the `bash`*terminal*, *shell*, or *commandline prompt* program is on your flavor of UNIX. You should see a blinking cursor and a `$` prompt:
The `$` sign above is just the prompt that indicates the terminal is waiting for you to type something. After command executes, you will see the `$` prompt again.
上面的`$`符号只是提示,终端正在等待您输入内容。执行命令后,您将再次看到`$`提示符。
The commandline is a very low-level interface for communicating with the operating system of your computer. You can think of the terminal as the diagnostics computer that mechanics plug into your car to really take control. The dashboard is analogous to the windowing graphical interface we use most of the time. Becoming a programmer is like becoming a mechanic; sometimes you need more powerful but more complicated tools to operate machinery.
You should more or less always have a command shell running in case you need to do something low-level. The first thing a mechanic does when he or she starts to work is to connect the diagnostic computer. So should you.
The command line is actually a full programming language with loops and everything but most of the time we simply execute commands. Commands have arguments just like function calls in a programming language have arguments. Here's how to say hello from the command line:
Commands are terminated by hitting the return key.
通过按返回键终止命令。
The `echo` command is analogous to the `print` command in Python code.
`echo`命令类似于 Python 代码中的`print`命令。
After executing the command, the prompt returns indicating you can type another command.
执行该命令后,提示符将返回,指示您可以键入另一个命令。
There are lots of things we can do; here's another one:
我们可以做很多事情; 这是另一个:
```bash
$ date
Wed Jul 12 14:18:51 PDT 2017
```
We can pass an argument (like a function call in Python) to commands:
我们可以将一个参数传递给命令(类似 Python 中的函数调用):
```bash
$ date"+%Y-%m-%d"
2018-07-08
```
Here's a cool example of a loop from the command line to examine all `.md` files (you don't need to run this one as part of the lab; I'm just showing off here):
From the command line, type in the following command to verify that you have access to `python3`:
```bash
$ which python3
/Users/parrt/anaconda3/bin/python3
```
Or, better yet, make sure the default Python is version 3:
或者,更好的是,确保默认的 Python 是版本 3:
```bash
$ which python
/Users/parrt/anaconda3/bin/python
```
The Python program should be in the Anaconda `bin` (binary) directory, that holds all of the binary executables. If you don't see anaconda somewhere in the output from the `which` command, you need to look at the documentation for Anaconda and figure out why you are not running the appropriate Python. (Most likely something is wrong with your `PATH` environment variable.) That's okay for now as we can use any 3.X version of Python running.
Next, run the program and make sure you have a recent version like 3.6.5:
接下来,运行该程序并确保您有最新版本如 3.6.5:
```bash
$ python
...
...
@@ -91,7 +91,7 @@ Type "help", "copyright", "credits" or "license" for more information.
>>>
```
Next try importing a library:
接下来尝试导入一个库:
```bash
$ python
...
...
@@ -102,17 +102,17 @@ Type "help", "copyright", "credits" or "license" for more information.
>>>
```
You should not get an error.
你不应该得到错误。
See also [Computer environment sanity check](https://mlbook.explained.ai/intro.html#sec:2.2.1) from my book [The mechanics of machine learning](https://mlbook.explained.ai).
Now let's jump into an interactive Python shell. In the abstract, it's the same as the bash command shell that controls your computer. Both are programming languages; they just have different specialties. When we jump from the bash shell into Python's shell, it's like jumping across the border from France into Germany. We have to stop speaking French and start speaking German. (Or, keep speaking English if you're American because we're lazy and don't know any foreign languages. haha) When you exit the Python interpreter, you will be back in the bash world, just as if you had returned from Germany to France.
To enter the Python world, from the bash `$` prompt type `python` as we did in the last section. You should see that it is using the **Anaconda** version. If not, that means you are using the default Python on your system.
@@ -124,50 +124,50 @@ Type "help", "copyright", "credits" or "license" for more information.
>>>
```
Python has evaluated the expression and printed the result back to the screen. It is as if we had used a `print` function call, which gives us the same result:
The Python interactive shell prints expression values immediately, but that is not the case when running a program as a script (i.e., not interactively).
The `$` prompt indicates that you are back at the bash command line. You can no longer type python code.
`$`提示符表示您回到了 bash 命令行。 您不能再键入 python 代码。
## Scripting Python
## 脚本化 Python
Go to a suitable directory (folder) on your disk, or create one, such as `/Users/YOURID/msan501/inclass`. (**Do not use spaces in any of your directory or file names...ever!**) Now create a **text file** called `hello.py` in that directory that contains exactly one line:
Use the editor of your choice, though `nano` is a good one because you will be able to use it on remote servers when we do cloud computing. Sublime and TextEdit.app also work.
Once you get the Python file written to the disk using your editor, you should be able to jump to that directory using `cd` (change directory) from the commandline:
You verify that you have created the Python script properly by typing the following at the command line:
通过在命令行键入以下内容,验证是否已正确创建 Python 脚本:
```bash
$ cat hello.py
...
...
@@ -175,27 +175,27 @@ $ cat hello.py
$
```
Here are solutions to the most common errors:
以下是最常见错误的解决方案:
1.Do not put `.txt` at the end of the file name; it must be `.py`
2.Do NOT use M$ Word or any other word processor; You think it's text but it's not. There are lots of text editors out there including Mac's `TextEdit.app`. Just make sure save as plain text not "rich text". There are also plenty of text editor such as [Sublime](https://www.sublimetext.com/) and [TextMate](https://macromates.com/). (If you are really hard-core, you will learn `vi` or `emacs`, which you will see me use in class.) You can also use `nano` from the command line for editing directly in the command line window.
1. 不要将`.txt`放在文件名的末尾;它必须是`.py`
2. 不要使用 MS Word 或任何其他文字处理器;你认为它是文本,但事实并非如此。有很多文本编辑器,包括 Mac 的`TextEdit.app`。只需确保保存为纯文本而不是“富文本”。还有很多文本编辑器,如 [Sublime](https://www.sublimetext.com/) 和 [TextMate](https://macromates.com/)。(如果你真的很硬核,你将学习`vi`或`emacs`,你会看到我在课堂上使用它。)你也可以在命令行中使用`nano`,直接在命令行窗口中进行编辑。
Now, we're going to run that program/script:
现在,我们将运行该程序/脚本:
```bash
$ python hello.py
$
```
We do not get any output. This is a critical difference. The interactive Python shell immediately prints expression values because it is interactive. When you run a file from the commandline, it assumes you wanted to execute the code like a script in batch mode. That is why we do not get any output without a print statement.
Now, we're going to do the exact same thing except using the Jupyter Lab browser-based environment. (See [Your machine learning development environment](https://mlbook.explained.ai/prep.html#sec:3.1) from the [in-progress ML book](https://mlbook.explained.ai) for more.)
现在,除了使用基于 Jupyter Lab 浏览器的环境之外,我们将做同样的事情。 (请参阅[正在编写的 ML 书](https://mlbook.explained.ai)中的[您的机器学习开发环境](https://mlbook.explained.ai/prep.html#sec:3.1)来了解更多信息。)
Launch Jupyter from the command line::
从命令行启动 Jupyter:
```bash
$ jupyter lab
...
...
@@ -218,16 +218,16 @@ I 11:27:00.606 LabApp] [jupyter_nbextensions_configurator] enabled 0.2.8
...
```
That will start up a program that launches a tab in your browser as well:
这将启动一个程序,在您的浏览器中启动一个选项卡:
<imgsrc="../notes/img/lab1.png"width="60%">
Clicking on the "Python 3" icon under the "Notebook" category creates a new notebook window for you:
单击`Notebook`类别下的`Python 3`图标,可为您创建一个新的笔记本窗口:
<imgsrc="../notes/img/lab2.png"width="80%">
Type `500+1` into the first cell next to the `In [ ]`. You should see the output 501 generated in the `Out` section below it. Hit control-enter to execute that cell or the right-facing triangle in the toolbar to execute the cell.
This is an interactive environment and so you can go back and edit the `500+1` to be, say, `print(500+1)`. Do that and then hit control-enter again to run. You should get the same output.