Python Get Started

Before you can write a single line of Python, you need a working Python installation and a clear idea of how to actually run code. “Getting started” is not just typing print("Hello, World!") — it is understanding where Python lives on your machine, the difference between typing code interactively versus saving it in a file, and how the interpreter turns your text into running instructions. This lesson walks through installing Python, verifying the install, and running your first real programs, so every later lesson can assume you know how to actually execute what you write.

Overview: What Python Is and How It Runs Your Code

Python is an interpreted, high-level programming language. “Interpreted” means there is a program called the Python interpreter (the executable usually named python or python3) that reads your source code and executes it directly, line by line, rather than compiling the whole program into a standalone machine-code executable ahead of time (as C or Rust do). This is why you can type a single line into an interactive prompt and see an immediate result — there is no separate “compile” step you have to run first.

Under the hood, the reference implementation of Python (called CPython, the one you get from python.org) actually does compile your source code — but to an intermediate form called bytecode, not machine code. When you run a .py file, CPython parses your text into bytecode instructions (you may have noticed .pyc files appear in a __pycache__ folder — that is cached bytecode), and then a virtual machine inside the interpreter executes that bytecode one instruction at a time. This two-step process (source → bytecode → execution) is why Python is flexible and portable, but generally slower than fully compiled languages.

There are two fundamentally different ways to feed code to the interpreter, and you will use both constantly:

  • Interactive mode (the REPL) — you type python3 (or python on Windows) at a terminal with no filename, and you get a prompt (>>>) where you can type one expression or statement at a time and see the result immediately. REPL stands for Read-Eval-Print Loop: the interpreter reads what you typed, evaluates it, prints the result, and loops back for more input. This is great for quick experiments, but nothing you type is saved anywhere once you close it.
  • Script mode — you write your code into a text file with a .py extension (for example hello.py) and run the whole file at once with python3 hello.py. This is how every real program, tool, and application is written and shared. Everything you build beyond a quick one-off calculation should live in a file.

Getting Python onto your machine is a one-time setup step. On Windows and macOS, download the installer from the official python.org downloads page and run it (on Windows, be sure to check “Add python.exe to PATH” during install, or the python command will not work from a terminal). Most Linux distributions and macOS versions already ship with a python3 command; you can install or upgrade it via your system package manager (for example sudo apt install python3 on Debian/Ubuntu). Once installed, everything else in this lesson — and this entire course — assumes you can open a terminal and successfully run a .py file.

Syntax: Checking Your Install and Running Code

These are the core commands you will type in a terminal (not inside Python itself) to work with your installation:

Command What it does
python3 --version Prints the installed Python version, e.g. Python 3.12.3
python3 Opens the interactive REPL prompt (>>>)
python3 hello.py Runs the script named hello.py from start to finish
exit() or Ctrl+D Leaves the interactive REPL

On Windows, the command is often just python instead of python3 (Windows installers typically don’t ship a separate “python” for Python 2). If you are unsure which is available, try both — the one that prints a Python 3.x.x version number is the one to use for this course.

A minimal script file follows this general shape: statements are written one per line, top to bottom, and are executed in that exact order the moment the file runs:

statement_1
statement_2
statement_3

There is no main() function requirement and no semicolons at the end of lines (though a semicolon can separate two statements on the same line — it’s just not idiomatic). Python instead relies on indentation (typically 4 spaces) to mark which lines belong inside a block, such as the body of a function or an if statement — you’ll see this in the examples below.

Examples

Example 1: Your first script

Save the following in a file named hello.py, then run it with python3 hello.py:

print("Hello, World!")
print("Welcome to Python programming.")

Output:

Hello, World!
Welcome to Python programming.

Each call to the built-in print() function writes its argument to the terminal followed by a newline character. The interpreter executes the file top to bottom: it runs the first print() completely (including printing the newline), then moves to the second line.

Example 2: Indentation defines blocks

This example defines a function and calls it three times, showing how indentation groups the lines that belong to the if/elif/else block inside the function:

def greet(hour: int) -> str:
    if hour < 12:
        return "Good morning"
    elif hour < 18:
        return "Good afternoon"
    else:
        return "Good evening"

print(greet(9))
print(greet(15))
print(greet(20))

Output:

Good morning
Good afternoon
Good evening

The colon : after def greet(hour: int) -> str: and after each if/elif/else tells the interpreter "an indented block follows." Every line indented at the same level underneath a colon belongs to that block. As soon as the interpreter sees a line indented back out to the left, it knows the block has ended — that's why print(greet(9)) at the far left is understood as being outside the function definition, at the top level of the file.

Example 3: A small, realistic script

Real scripts combine variables, built-in functions, and formatted output. This one summarizes a week of recorded temperatures:

# A short program that summarizes a week of temperatures (in Celsius)
temperatures = [21.5, 23.0, 19.8, 24.2, 22.1, 20.6, 25.3]

total = sum(temperatures)
average = total / len(temperatures)
warmest = max(temperatures)
coolest = min(temperatures)

print(f"Average temperature: {average:.2f}°C")
print(f"Warmest day: {warmest}°C")
print(f"Coolest day: {coolest}°C")

Output:

Average temperature: 22.36°C
Warmest day: 25.3°C
Coolest day: 19.8°C

Here sum(), len(), max(), and min() are built-in functions that work directly on the temperatures list. The f"..." strings are f-strings: any expression inside {} is evaluated and inserted into the string, and the :.2f after average formats that number to exactly two decimal places. Notice the # comment on the first line — anything after a # is ignored by the interpreter entirely; it exists purely for human readers.

Under the Hood: What Happens When You Type python3 hello.py

  1. The operating system's shell locates the python3 executable on your PATH and starts it, passing hello.py as an argument.
  2. The CPython interpreter opens hello.py and reads its raw text.
  3. A tokenizer breaks the text into meaningful pieces (keywords, names, numbers, strings, operators).
  4. A parser arranges those tokens into a tree structure representing the program's grammar (this is where a missing colon or bad indentation would be caught as a SyntaxError, before any code runs).
  5. The tree is compiled into bytecode — a compact, lower-level instruction set for Python's internal virtual machine.
  6. The virtual machine executes the bytecode instructions one at a time, top to bottom, maintaining variables and function calls as it goes, sending any print() output to your terminal.
  7. When the last line finishes (or an unhandled exception occurs), the interpreter exits and control returns to your shell.

This is also why syntax errors are caught immediately (during parsing, before step 6 ever runs) while errors like dividing by zero only appear once the interpreter actually reaches that line during execution.

Common Mistakes

Mistake 1: Using the old Python 2 print statement. In Python 2, print "Hello" (no parentheses) was valid. In Python 3, print is a regular function, so writing:

print "Hello, World!"

raises a SyntaxError, because the interpreter sees a function name followed directly by a string with no call parentheses, which is not valid syntax. The fix is to always call it as a function: print("Hello, World!").

Mistake 2: Mixing tabs and spaces for indentation. Because Python uses indentation to define blocks, a file where some lines are indented with tabs and others with spaces can look correctly aligned in your editor but confuse the interpreter, producing a TabError: inconsistent use of tabs and spaces in indentation. For example, saving a file where the if body below is indented with a tab, but a later line in the same block uses four spaces, is invalid — even if it visually lines up. The fix is to configure your editor to insert spaces (4 is the PEP 8 standard) whenever you press Tab, and to never hand-mix the two within one block.

Mistake 3: Running the wrong command. On many systems, plain python either doesn't exist or points to a Python 2 install. If python --version reports Python 2.7.x, use python3 instead for everything in this course — the syntax and behavior described here targets Python 3 only.

Best Practices

  • Always confirm your version with python3 --version right after installing, and again whenever something behaves unexpectedly — many bugs turn out to be an old or unintended interpreter being invoked.
  • Use the REPL for quick one-line experiments (checking how a function behaves, testing a small expression) but write anything worth keeping in a .py file.
  • Name script files with lowercase letters and underscores (my_script.py), matching Python's own snake_case convention.
  • Use a real code editor with Python support (VS Code, PyCharm, or similar) rather than a plain text editor — it will catch indentation and syntax problems as you type, before you even try to run the file.
  • Indent consistently with 4 spaces per level, and configure your editor to convert the Tab key into spaces automatically, per PEP 8, Python's official style guide.
  • Keep a terminal open in the same folder as your script so python3 filename.py just works without typing long paths.

Practice Exercises

  1. Install Python if you haven't already, open a terminal, and run python3 --version (or python --version). Write down the exact version string it prints.
  2. Create a file named about_me.py that defines three variables (your name, your city, and your favorite number) and prints a sentence using each of them with an f-string. Run it with python3 about_me.py.
  3. Open the interactive REPL by typing python3 with no filename. Type 2 + 2 and press Enter, then type print("testing") and press Enter. Notice that the REPL shows the result of 2 + 2 automatically, without needing print() — why do you think that is, but the second line still needed print() to show anything inside a saved script?

Summary

  • Python is interpreted: CPython compiles your source to bytecode internally and a virtual machine executes it line by line — there is no separate machine-code build step for you to run.
  • You install Python once (via python.org or your OS package manager) and verify it with python3 --version.
  • The interactive REPL (python3 with no arguments) is for quick, temporary experiments; real programs belong in .py script files run with python3 filename.py.
  • Indentation (4 spaces, consistently) is not just style in Python — it is how the language defines code blocks, so mixing tabs and spaces causes real errors.
  • Python 3 requires print() to be called as a function; the old parentheses-free print "..." syntax from Python 2 is a SyntaxError in Python 3.