Skip to content

Getting Python

Before you can start writing programs, you need to install the Python interpreter on you machine.

Install Python

You want Python 3.10 or newer (newer versions have friendlier error messages).

Get the Installer from the official Python website.

Add it to the system PATH

On the first installer screen, check "Add python.exe to PATH..

If you forget to do this, you will get an error when you try to run Python for the first time, and you will have to add it to the PATH manually.

brew install python, or use the installer from python.org

Python is almost certainly already there. Run python --version to check.

If not already installed, use your distro's package manager to install it.

Info

On some Linux distros, Python is invoked with python3 instead of python. Try which python and which python3 and if neither of those yields an answer, Python is probably not installed.

Install a code editor

While you can write Python programs using any text editor, code editors provide a much better user experience. Some of the features you may want in a coded editor include:

  • Syntax highlighting: reveal the structure of your code using different colors
  • Automatic indentation: Python uses indentation as syntax and it is easy to forget or be inconsistent in how you indent your code. A good code editor takes care of that for you
  • Catch errors: you will make many mistakes while programming Python. A good code editor lets you know when that happens.

There are many code editors to choose from, but this course recommends Visual Studio Code from Microsoft. Get it from here: VS Code Download

Running Python

Python programs are instructions that are executed by an interpreter. The interpreter can be used interactively to execute instructions as you type them, or it can be used to execute instructions contained in a text file, a.k.a, a script.

The REPL — an interactive prompt for quick checks

REPL stands for Read-Eval-Print Loop. It read Python expressions you type into it, evaluates them, prints the results to the screen and then waits for the next instruction.

Type python with no arguments into a terminal window. You will see these characters >>> printed on the screen, followed by a space. We call this a prompt and it is waiting for you to type something.

Try this:

>>> 2 + 3
5
>>> "hi" * 3
'hihihi'
>>> name = "Ada"
>>> name
'Ada'

Press Ctrl-D (macOS/Linux) or Ctrl-Z then Enter (Windows), or type exit(), to leave. Throughout the course, lines beginning with >>> mean "type this into the REPL" and the line under it is what the REPL echoes back.

Script files — for anything more than a line or two

Real programs live in .py files. Create one and run it:

python3 hello.py

Where hello.py contains:

print("hello from a script")
hello from a script

Use print() if you want to see output from a script

The REPL evaluates what you type and prints the results immediately. A script does not - there you must call print(...) if you want to see output on the screen.

Create a home for your programs

Make a folder for this course so nothing you try affects your existing files:

mkdir acit1515 && cd acit1515

Learn to use the terminal

mkdir and cd in the previous command create a folder and change into it respectively.

It is important to familiarize yourself with the terminal enough to be able to navigate the filesystem, create, open and delete files and folders and run commands. This is one of the first skills a programmer is expected to learn.

Also, learn where things are on your system. Make sure you know how to find where programs are installed, how to find your files, how to tell the type of a file - for example, Python scripts use the .py suffix. When you see such a file, you can immediately tell that it is a program intended to be processed by the Python interpreter. Similarly, when you see a .txt this tells you the file contains text, but it is not executable.

Getting help

Python literally has a built-in help() function that exists to explain things. It doesn't explain everything, only things the interpreter knows. But it can be a quick way to look up information about Python language constructs and other Python objects.

Try this:

help(print)
help('while')       # for python keywords, surround them with quotes

For more detailed explanations, The Official Python Tutorial is a good place to start.