Python and the Command Line

How can we write and run Python code?

KEY TERMS

  • Python interpreter: a program that turns Python commands into instructions a computer can understand and execute

Note: This guide on how to use the Python interpreter assumes that Python is already on your machine (which is included in the Anaconda installation process). If this is not the case, refer to the instructions provided in the previous section.

We can use the Command Prompt to accomplish a variety of tasks, including navigating directories, editing files, and running programs. We can also use the Command Prompt to run the Python interpreter in interactive mode, which will allow us to type Python commands into the Command Prompt.

In the first section, we saw that the Command Prompt is a command line interpreter; it is capable of taking command line keywords and turning them into something the computer can understand and execute. However, the Command Prompt cannot interpret Python commands directly because those commands are in a different programming language. This is why we need to run the Python interpreter first before we can type Python commands into the Command Prompt. The Python interpreter is a program specifically designed to turn commands in the Python programming language into instructions that the computer can understand and execute.

To start the Python interpreter, we can type python into the Command Prompt. Once we have started running the Python interpreter in interactive mode, it will display a prompt >>> to indicate that it is ready for you to start typing Python commands. To run a line of Python code, you can type your code after the prompt and press enter or return to see the result. To close the Python interpreter and return to the normal Command Prompt, you can press the control and D keys or type quit() after the next prompt.

In the next few chapters of this textbook, we will learn more about specific Python commands and what we can do with them. For now, we can try out the Python interpreter by using Python to do some simple math! See Figure 2 below for some examples.

The best way to become familiar with programming and data science is to try it yourself! You can test out the examples below in Command Prompt or you can create some of your own.

Last updated