SkarpSkarp

Chapter 2 of 11

Setting Up Python and Your First Lines of Code

Watch your computer turn into a coding playground as you install Python and run your very first commands. In just a few minutes, you’ll go from no setup at all to printing your first message and doing quick calculations in Python.

15 min readen

Welcome: Turn Your Computer Into a Python Playground

Your Python Playground

In this module, you will turn your computer into a Python playground and run your very first code. No experience needed, just your computer and about 15 minutes.

What You Will Be Able To Do

By the end, you will install a current Python 3 version, open the Python REPL, run simple commands, and create and run a basic `.py` script file.

Python Version (2026 Context)

As of early 2026, the latest stable Python 3 versions are in the 3.12 family. Any version starting with `3.` is fine for this course.

Our Step-by-Step Plan

We will: 1) check if Python is already installed, 2) download Python, 3) install it, 4) open the REPL, and 5) create and run your first script.

Step 1: Check If Python Is Already Installed

Why Check First?

Before installing, check if Python 3 is already on your machine. Many macOS and Linux systems include it, and some Windows setups do too.

Check on Windows

Open Command Prompt, then run `python --version`. If that fails, try `py --version`. If you see `Python 3.x.x`, you already have Python 3.

Check on macOS

Open Terminal and run `python3 --version`. If you see `Python 3.x.x`, great. If you get `command not found`, you will need to install Python.

Check on Linux

Open your Terminal and run `python3 --version`. A result starting with `3.` means Python 3 is installed. Otherwise, you will install it next.

Step 2: Download the Latest Python 3 Safely

Use the Official Site

Always download Python from the official site: `https://www.python.org`. This avoids unsafe or very old versions from random websites.

The Big Yellow Button

On the Python.org home page, look for the big yellow button: Download Python 3.12.x (numbers may vary). Click it to get the right installer.

Choosing Your OS

If needed, click Downloads in the top menu and pick Windows or macOS. Linux users usually install via their distribution’s package manager.

Stable vs Preview

In 2026, Python 3.12 is a stable choice. Ignore preview or beta versions like 3.13 alpha. Always pick the latest stable release for this course.

Step 3: Install Python on Your System (Visual Walkthrough)

Windows Install Key Step

On Windows, run the installer and make sure you check Add python.exe to PATH before clicking Install Now. This makes `python` work in Command Prompt.

Verify on Windows

After it finishes, open Command Prompt and run `python --version` or `py --version` to confirm Python 3 is installed.

macOS Install and Check

On macOS, open the `.pkg` or `.dmg`, follow the installer, then open Terminal and run `python3 --version` to confirm the installation.

Linux Install and Check

On Linux, if needed, use your package manager (for example `sudo apt install python3` on Ubuntu) and then run `python3 --version` in Terminal.

Quick Check: Installation Basics

Test your understanding of the installation step before moving on.

On Windows, which action is especially important during Python installation for this course?

  1. Choosing the custom installation location
  2. Checking the 'Add python.exe to PATH' box
  3. Installing every optional feature
  4. Disabling all internet access during install
Show Answer

Answer: B) Checking the 'Add python.exe to PATH' box

Checking 'Add python.exe to PATH' lets you run `python` from any Command Prompt window, which makes the rest of the course much easier.

Step 4: Meet the Python REPL (Interactive Shell)

What Is the REPL?

The Python REPL is an interactive shell where you type commands and see results immediately. REPL stands for Read–Evaluate–Print Loop.

Open REPL on Windows

On Windows, open Command Prompt and type `python` (or `py`) and press Enter. If you see `>>>`, the REPL is ready.

Open REPL on macOS/Linux

On macOS or Linux, open Terminal and type `python3` and press Enter. When you see `>>>`, Python is ready for your commands.

First Commands to Try

At `>>>`, try: `2 + 3`, `10 * 5`, and `print("Hello, Python!")`. You should see the results appear right away.

Interactive Practice: Your First Python Expressions

Type these lines one by one into the Python REPL (where you see `>>>`). Watch what happens after each line.

```python

Try some math

2 + 3

7 * 8

10 / 4

10 // 4 # integer division (no decimals)

10 % 4 # remainder

Try text (strings)

"Hello" # just a value

print("Hello, Python!") # prints to the screen

Combine text and math

print("2 + 3 is", 2 + 3)

```

If something goes wrong (for example, you see an error message):

  • Read the error text slowly
  • Check for missing quotes `"` or missing parentheses `)`
  • Try the line again carefully

To exit the REPL later:

  • On Windows: press `Ctrl + Z`, then Enter
  • On macOS/Linux: press `Ctrl + D`

Step 5: Interactive Mode vs Script Files

What You Just Used

You have been using interactive mode (the REPL): type a line at `>>>`, see the result immediately, then move on. It is great for quick experiments.

What Is a Script File?

A script file is a text file ending in `.py` (for example `hello.py`) that contains several lines of Python code saved on your computer.

Key Differences

In the REPL, code runs line by line and is not saved. In a script, you save all your code and run the whole file at once.

Scratchpad vs Document

Interactive mode is like a scratchpad. A script file is like a saved document you can reopen, share, and run again later.

Step 6: Create and Run Your First Python Script

Pick a Text Editor

Use a plain text editor like Notepad, TextEdit (plain text mode), VS Code, or gedit. Do not use Word or Google Docs for code.

Write Your Script

Type this into your editor:

```python

print("Hello from a Python script!")

print("2 + 3 is", 2 + 3)

```

Save as hello.py

Save the file as `hello.py` in an easy-to-find folder like Desktop or Documents. Make sure it is really `hello.py`, not `hello.py.txt`.

Run the Script

In Command Prompt or Terminal, `cd` to the folder and run `python hello.py` (Windows) or `python3 hello.py` (macOS/Linux) to see the output.

Step 7: Tweak the Script – Make It Yours

Now that you have a working script, customize it so it feels like yours.

Your task

  1. Open your `hello.py` file again in your editor.
  2. Change it so it:
  • Prints a greeting with your name.
  • Prints one fun fact about yourself.
  • Uses Python to calculate something simple.

Example idea (do not just copy, make your own):

```python

print("Hi, I am Alex!")

print("Fun fact: I love basketball.")

print("In 5 years, I will be", 15 + 5, "years old.")

```

  1. Save the file.
  2. Run it again from the command line.

Reflection questions

  • What changed between the first and second time you ran `hello.py`?
  • How would you explain the difference between running code in the REPL and running `hello.py` to a friend?

Think through your answers before moving on.

Key Terms Review

Flip through these cards to review the main ideas from this module.

Python REPL
The interactive Python shell where you type commands at `>>>` and see results immediately. REPL stands for Read–Evaluate–Print Loop.
Script file
A saved text file ending in `.py` that contains Python code. You run the whole file at once using Python.
Official Python download site
The safe, up-to-date place to get Python: `https://www.python.org`. Avoid random download sites.
PATH (on Windows)
A system setting that lets you run `python` from any Command Prompt. Checking 'Add python.exe to PATH' during install sets this up.
Interactive mode vs script
Interactive mode is like a scratchpad for quick tests. A script is a saved program you can run again and share.

Final Check: Are You Ready to Move On?

Answer this question to confirm you understand the core idea of this module.

Which of these is the BEST description of the difference between using the Python REPL and running a `.py` script file?

  1. The REPL is only for Windows, and scripts are only for macOS and Linux.
  2. The REPL runs code one line at a time interactively, while a script runs saved code from a file all at once.
  3. Scripts can do math, but the REPL cannot.
  4. The REPL requires internet, but scripts do not.
Show Answer

Answer: B) The REPL runs code one line at a time interactively, while a script runs saved code from a file all at once.

The REPL is interactive: you type a line, it runs immediately. A script is code saved in a `.py` file that you run as a whole program.

Key Terms

PATH
An operating system setting that tells your computer where to look for programs like `python` when you type their names in the command line.
REPL
Short for Read–Evaluate–Print Loop. An interactive way to use Python where you type commands and see results immediately.
Script
A text file containing Python code, usually with a `.py` extension, that can be run as a program.
Python 3
The current major version of the Python programming language. In 2026, versions in the 3.12 family are common and recommended.
Terminal
A command-line application on macOS and Linux used to type commands such as `python3` or `cd`.
Command Prompt
The built-in command-line tool on Windows where you can type commands like `python`.

Finished reading?

Test your understanding with a custom practice exam on this chapter.

Test yourself