Running Your First Code

Beginner · 16 min read · ▶ live playground · ✦ checkpoint

You have Python installed. Time to actually talk to it. There are two ways to run Python code — a conversation and a script — and by the end of this lesson you'll have done both, understood what really happens when you press Enter, and read your first error message without flinching.

The interactive shell (REPL)

Type python in your terminal with no filename and something different happens — instead of running and exiting, Python starts listening:

$ python
Python 3.14.2 (main) [...] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

That >>> is the REPLRead, Evaluate, Print, Loop. It reads what you type, evaluates it, prints the result, and waits for more. Type 2 + 2, get 4, instantly. Notice you didn't even need print() — the REPL automatically shows the value of any expression you enter. That's REPL behavior, not Python-file behavior, and mixing the two up causes real confusion later.

The REPL is where Python programmers think out loud: test an idea, check what a function returns, poke at data. You'll leave it with exit() — but you'll come back thousands of times over your career.

Scripts: writing and running a .py file

Conversations vanish when you close the terminal. For anything you want to keep, you write a script — a plain text file of Python, run top to bottom:

print("Hello, World!")

Save that as hello.py, then in the terminal (in the same folder — lesson 1.3's trap!):

$ python hello.py
Hello, World!

One line of the ritual greeting every programmer has typed since 1978. Let's take it apart properly.

"Hello, World!" explained line by line

There's more happening in that one line than it seems:

  • print — a name. It refers to a built-in function, a reusable chunk of behavior that Python ships with.
  • () — the parentheses call the function: "don't just mention print, run it." The things inside are its arguments.
  • "Hello, World!" — a string: text data, marked off with quotes so Python knows it's data, not instructions.
  • The function does its one job: write the text to the screen, then move to the next line.

Every program you ever write will be built from exactly these ingredients — names, calls, and data — just more of them.

Try it — REPL and script in one place

This playground works like both modes: the editor is your script, and the >>> prompt underneath is a live REPL sharing the same session. Run the script, then try typing answer or answer * 2 at the prompt.

python — playgroundlive
⌘/Ctrl + Enter to run

Reading your first error messages without panic

Break something on purpose. Run this:

print("Hello, World!"

SyntaxError: '(' was never closed — pointing right at line 1.

An error message isn't the computer scolding you — it's the computer filing a precise bug report. Python tells you three things, always: which file and line (File "broken.py", line 1), what kind of problem (SyntaxError — the code doesn't parse as valid Python), and its best guess at the cause (an unclosed parenthesis).

The two kinds you'll meet constantly this early:

  • SyntaxError — Python can't even understand the code. Nothing ran. Usually a missing quote, bracket, or colon, often on the line above where Python points.
  • NameError — the code is valid, but you used a name that doesn't exist yet. print(greting)NameError: name 'greting' is not defined. Did you mean: 'greeting'? — modern Python literally suggests the fix.

The habit that separates programmers from people who give up: read the last line of the error first, then the line number. Never just re-run it hoping. Try it now — go back to the playground above, delete a quote or misspell print, and run it. Getting comfortable with errors is the skill.

Checkpoint

Answer all three to mark this lesson complete

+50 XP on completion