1  Getting Started

In This Chapter

Before we can start issuing commands to the computer, we need to install the software that actually understands those commands. In this chapter, we will prepare your computer for the rest of the course and learn the absolute basics of coding.

We will cover:

  • The Python Interpreter: Understanding how the computer turns your text into actions.
  • Installing Python: Getting Python set up on Windows, macOS, or Linux.
  • IDLE and the REPL: Launching your environment and opening a direct line to the computer.
  • The Programmer’s Keyboard: Spotting the critical keys you will use every day.
  • Expressions and Operators: Using Python as a super-powered calculator.
  • Syntax Errors: Learning how to respond when the computer gets confused.

1.1 The Python Interpreter

Your computer’s brain (the CPU) only understands raw machine code. It doesn’t natively understand English, and it doesn’t even understand the Python language which we will be using in this course.

To bridge this gap, we use a translation program called the Python Interpreter.

You write instructions in plain-text files using the Python language. Then the interpreter reads those instructions and carries them out right away. Without this interpreter, your code is just a useless text document that your computer cannot run.

When you run Python code the interpreter performs a short sequence of steps behind the scenes:

  1. Read: It reads the characters you typed and breaks them into meaningful pieces like words, numbers, and symbols.
  2. Parse: It checks that those pieces follow Python’s rules and builds an internal representation of the instructions.
  3. Compile: It converts that internal representation into a compact, low-level form called bytecode. These bytecode files are optimized so that future runs happen even faster.
  4. Execute: A tiny virtual engine inside Python steps through the bytecode and commands your CPU to perform the actual actions, such as calculating math, storing files, or drawing graphics on your screen.

Because Python is an interpreted language, you get immediate feedback. You type a command, and the interpreter responds instantly. This makes it an ideal language for beginners. Along with the interpreter, we will be installing IDLE, a built-in text editor designed specifically for writing and testing our code

1.2 Installing Python

Below are instructions for installing the Python tools necessary for this course on Windows and Mac OS. If you are having difficulties you can always reference the official Python installation documentation at link-here or visit the Mainloop Press website for more support.

A Note on Python Versions

Python is constantly being updated. For this book we need to install Python 3. Any modern version (like Python 3.12 or Python 3.14) will work fine.

Warning: Do not use Python 2! It is obsolete, and none of the code in this course will work on it.

Installing on Windows

The official way to install Python on Windows has recently changed. Instead of a traditional Windows installer for Python, we use a tool called the Python Install Manager. We will use this manager to install and manange the actual Python coding environment.

Follow these steps carefully:

  1. Open your web browser and go to the official Python website: https://www.python.org/downloads/

  2. Click the big yellow button that says Download Python install manager. This will download a file that ends in .msix.

  3. Locate the .msix file in your Downloads folder and double-click it.

  4. A Windows installation window will pop up. Click Install.

Figure 1.1: It should look like this. Make sure you check ‘Launch when ready’.
  1. Once the manager finishes installing, it should open a command terminal and ask if you want to install the default Python runtime. The prompt will say something like ‘Install CPython now? [Y/n]’. Enter y and hit Enter.
Figure 1.2: Press ‘y’ and hit the Enter key.
  1. If it does not open a command terminal you will need to do it manually. Click your Windows Start Menu, type cmd, and press ENTER to open the Command Prompt (a black window with white text).

  2. Type the following command exactly as written and press ENTER:

    py install default
Figure 1.3: Enter the command and press ENTER.
  1. It may ask several other questions. You can just hit ENTER to accept the default values except for “Add commands directory to your PATH now? [y/n]”. Enter y for this. You need Python added to your global system path.
Figure 1.4: Make sure you say yes (‘y’) to add commands to PATH.

The manager will now download and install the newest version of the Python interpreter. You will see a progress bar in the black window.

Once it says the installation is successful, you can close the Command Prompt!

Old Windows Installer

If you have already followed the steps above to install Python you can skip this section. If for some reason you have downloaded the older Windows Python installer here are the steps for installation:

  1. Open your web browser and go to the official Python website: https://www.python.org/downloads/
  2. Click the big yellow button that says Download Python 3.x.x (the numbers will match the newest version).
Figure 1.5: Download Python from python.org
  1. Open the .exe installer file you just downloaded.
Figure 1.6: Make sure you check ‘Add python.exe to PATH’.
  1. STOP! Look at the bottom of the installer window. You must check the box that says Add python.exe to PATH (or Add Python 3.x to PATH). If you do not check this box, your computer will not know where Python is, and you will have major headaches later when we install Pygame.
  2. Once that box is checked, click Install Now.
  3. The installer will handle the rest. You may need to click “Yes” to allow the app to make changes to your device.
  4. When it finishes, click Close.

Installing on macOS

Apple computers used to come with an ancient version of Python installed by default. We want to ignore that and install the newest version.

  1. Go to https://www.python.org/downloads/
  2. The website will automatically detect you are on a Mac. Click the yellow Download Python 3.x.x button.
  3. Open the .pkg installer file you just downloaded.
Figure 1.7: The ‘Welcome to the Python Installer’ screen.
  1. Click Continue through the Introduction, Read Me, and License screens. Click Agree to accept the software license.
  2. Click Install. You will likely be asked to enter your Mac administrator password (or use TouchID) to approve the installation.
  3. Once the installation is successful, a Finder window will pop up showing the new Python 3 folder in your Applications directory. You can close the installer.

Installing on Ubuntu or other Linux OS

Python 3 is actually already installed on most modern Linux distributions! Often, however, IDLE and other graphical tools we need are stripped out to save space. We need to tell Linux to download those specific tools.

If you are using Ubuntu, you can install the complete package using the Terminal.

  1. Open your Terminal (you can press Ctrl + Alt + T).

  2. First, update your package list by typing this command and hitting ENTER (you will need to type your computer’s password):

    sudo apt update
  3. Next, install IDLE and Tkinter by typing this command and hitting ENTER:

    sudo apt install python3 python3-idle python3-tk
  4. Press Y and ENTER when it asks if you want to continue. It will download and install everything you need.

If you are using a different Linux distribution, check your package manager for the python3, python3-idle, and python3-tk packages.


1.3 IDLE

IDLE (Interactive DeveLopment Environment) is the program where we will write, test, and run our Python code. Later on we will introduce some more advanced tools for writing and maintaining your code, but for now we will use IDLE as it was convienently installed along with Python.

Starting IDLE is slightly different depending on your operating system:

  • On Windows: Click the Start Menu icon at the bottom of your screen, type IDLE, and click on the app named IDLE (Python 3.x 64-bit).
  • On macOS: Open your Applications folder, find the folder named Python 3.x, and double-click the IDLE icon. (You can also press Cmd + Space to open Spotlight Search, type IDLE, and hit ENTER).
  • On Linux (Ubuntu): Open your application menu and search for IDLE. Alternatively, open a Terminal and simply type idle3 and press ENTER.

When you open IDLE, a window with a white background and text at the top will appear. This is the Interactive Shell.

Figure 1.8: A screenshot of the IDLE Interactive Shell. The text at the top shows the Python version, and at the bottom left are the three arrows >>> waiting for a command.

If you see the three arrows (>>>) at the bottom, congratulations! You have successfully installed Python, and your computer is waiting for your first command.

1.4 A Conversation with the Computer

Computers are incredibly fast, capable of calculating millions of math problems in a single second. But computers have absolutely zero common sense. They take everything literally.

A computer cannot guess what you meant to say; it only understands exactly what you did say. Programming is simply learning the strict language required to give these literal machines instructions.

The REPL

The three arrows >>> you see in IDLE are called the prompt. This is where we talk directly to the computer’s internal interpreter.

When you type an instruction here and press ENTER, you are participating in a REPL, which stands for Read-Evaluate-Print Loop.

  • Read: The computer reads the text command you input.
  • Evaluate: The interpreter processes the instruction and calculates the answer.
  • Print: The result is immediately displayed on the screen.
  • Loop: The prompt resets, printing >>> to signal it is ready for another command.

The interactive shell is a completely safe sandbox. If you make a mistake, it will not damage your computer; it will simply display a red error message. Reading these notifications carefully will tell you exactly where your instructions broke down. If you ever feel lost or want to start fresh, simply close the IDLE window and reopen it to reset the system.

1.5 The Programmer’s Keyboard

Because a computer is so literal, it relies heavily on specific symbols to understand what you want it to do. Before we type our first instructions, you need to know the difference between three very important pairs of symbols on your keyboard. Beginners often mix these up!

  • Parentheses ( ): Used for doing math, and for sending data into functions (like when we ask the computer to print something). You can usually find these by holding Shift and pressing 9 or 0.
  • Square Brackets [ ]: Used for organizing lists of data. You can usually find these near the Enter or Return key.
  • Curly Braces { }: Used for formatting text and building advanced maps of data. You can usually find these by holding Shift and pressing the Square Bracket keys.

If a book or tutorial asks you to type a parenthesis (, do not type a square bracket [. The computer will immediately crash because it thinks you are trying to give it a completely different kind of instruction!

1.6 Expressions: Making the Computer Do Math

First, we will use Python to solve some simple math problems. The interactive shell can work just like a calculator. Type 2 + 2 into the interactive shell at the >>> prompt and press ENTER.

Figure 1.9 shows how this math problem looks in the interactive shell. Notice that it responds with the number 4.

Figure 1.9: Simple math in the interactive shell

This math problem is a simple programming instruction. The plus sign (+) tells the computer to add the numbers 2 and 2. The computer does this and responds with the number 4 on the next line. Table 1.1 lists the basic math symbols available in Python.

Table 1.1: Math Operators
Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division

When used in this way, +, -, *, and / are called operators. Operators tell Python what to do with the numbers surrounding them.

The math problem 2 + 2 is an example of an expression. As Figure 1.10 shows, expressions are made up of values (the numbers) connected by operators (the math signs) that produce a new value the code can use.

Figure 1.10: An expression is made up of values and operators that produce a single value.

Try entering some of these expressions into the interactive shell, pressing ENTER after each one:

>>> 2+2+2+2+2
10
>>> 8*6
48
>>> 10-5+6
11
>>> 2  +        2
4

Notice all the spaces in the 2 + 2 example. In Python, you can add any number of spaces between values and operators. However, you must always start instructions at the very beginning of the line (with no spaces before the first number).

Evaluating Expressions

When a computer solves the expression 10 + 5 and returns the value 15, it has evaluated the expression. Evaluating an expression reduces it down to a single value, just like solving a math problem reduces the problem to a single number: the answer.

Expressions can be of any size, but they will always evaluate down to a single value. Even single values are expressions. For example, the expression 15 simply evaluates to the value 15.

1.7 Syntax Errors: When the Computer Gets Confused

Because computers take everything literally, they get easily confused if you break the rules of their language. If you enter 5 + into the interactive shell, you’ll get an error message in red text:

>>> 5 +
SyntaxError: invalid syntax

This error happened because 5 + isn’t a complete expression. The + operator expects a value before and after it. A SyntaxError means Python doesn’t understand the instruction because you typed it incorrectly.

Don’t worry about making mistakes! Errors won’t damage your computer. They are just the computer’s way of saying, “I don’t understand.” Just retype the instruction correctly at the next >>> prompt.

Summary

In this chapter, you established your local programming environment and learned how to communicate directly with your computer’s interpreter.

  • The Python Interpreter is a translation program that reads, parses, compiles, and executes text code into machine language instructions.
  • The REPL (Shell) reads your input command, evaluates it, prints the final result, and loops back to await your next choice.
  • Expressions combine values and operators.
  • Syntax Errors happen when typing errors or rule violations cause your instructions to become unreadable to the literal interpreter.