Installing and running Python¶
~15 min ยท prerequisite: What is a program
What you will be able to do¶
- Check whether Python is already on your computer and which version it is.
- Run a Python file from the terminal and see its output.
- Start the interactive REPL, type a line of Python, and leave it.
The idea¶
To run Python code, you need two things: the Python interpreter installed on your computer, and a way to give it instructions. The second part is usually a terminal, a text window where you type commands. A terminal is also called a command line or shell. You type a command, press Enter, and the computer replies with text.
This course uses Python 3.12 or a newer 3.x release. The official downloads page is python.org/downloads. Download the installer for your system and follow its steps. Some computers already have Python, so check first.
To check, open a terminal and type the following command, then press Enter.
On macOS and Linux, use python3. On Windows, the launcher is usually called py, so try py --version. If the output starts with Python 3.12 or a higher version, you are ready. If the command is not found, install Python from the downloads page and try again.
Python can run in two ways. The first is the REPL, short for read, evaluate, print, loop. It is an interactive prompt that reads one line at a time and shows the result. It starts when you type python3 alone. The prompt looks like >>>. You type a line, press Enter, and Python answers. To leave, type exit() and press Enter.
The second way is a script, which is a file of Python code that you run all at once. Scripts end in .py. You create the file in any text editor, save it, and run it from the terminal with python3 filename.py. Scripts are what you will write for most of this course, because you can save them and run them again.
When you run a script, Python reads the file from the first line to the last. It does not print the result of every line the way the REPL does. Only the lines that ask for output, such as print, show something. You will learn print in lesson 3.
Keep the terminal open while you work. If you change the file, save it and run the command again. Each run starts fresh.
Worked example¶
Create a file named hello.py in an empty folder. Put one line in it.
Line by line, this is what happens when you run it:
- The file contains one statement,
print("Hello, world"). A statement is an instruction that Python carries out. printis a built-in function. A function is a named action you call with parentheses.- The text inside the quotes is a string, a piece of text. Python sends it to the screen.
In the terminal, run this command from the same folder as the file.
The output is:
The REPL shows the same result if you type the line at the >>> prompt. The difference is that a script runs every line in the file, while the REPL waits for your next line.
Try it¶
The blocks below are checks that run in the browser. They do not need your computer's Python. Each assert must pass.
Next, a check of the basic idea that the interpreter evaluates an expression.
Common mistakes¶
-
Running the command from the wrong folder. If your terminal is in a different folder from the file, Python cannot find it:
Use
ls(macOS and Linux) ordir(Windows) to list the files in the current folder. -
Typing a command into the REPL. The REPL expects Python code. Typing the shell command
python3 hello.pyat the>>>prompt gives:Exit the REPL with
exit()and then run the command in the terminal. -
Using an old Python 2 style of print. Writing
print "Hello"without parentheses gives:In Python 3,
printis a function, so it needs parentheses. -
Typing a command that is not installed. On Windows, if Python is not on your path, you may see:
Try
pyinstead, or reinstall Python and tick the option that adds it to your path.
Practice¶
- Find out which Python version your computer runs. Write the command you typed and the first line of the output in a comment in a file named
version.py. - Create a file named
first.pythat prints your name. Run it withpython3 first.py. - Open the REPL, type
2 * 21, and read the answer. Then typeexit()to leave. - Predict what this script prints, then run it to check:
print("one")followed byprint("two")on the next line. - Stretch: create a script with two
printlines, each showing a sum. Use oneprintfor7 + 8and one for10 * 3. Run it and check that the output is15and30.
Solution
-
Type
python3 --version(orpy --versionon Windows). A typical output isPython 3.12.4. Your version number may differ. -
python print("Your Name")
Replace Your Name with your name. The terminal shows Your Name.
-
The REPL shows
42. Typeexit()to return to the terminal. -
The output is
oneon one line andtwoon the next line. -
python print(7 + 8) print(10 * 3)
The output is 15 on the first line and 30 on the second.
Recap¶
- Check your Python version with
python3 --version, orpy --versionon Windows. - A script is a
.pyfile that you run from the terminal withpython3 filename.py. - The REPL (
>>>) runs one line at a time, andexit()leaves it.