Stop 01 · Python FoundationsTopics · 3/8

Running Scripts & the Python REPL

Part of the Python Roadmap

In short

There are two ways to talk to Python: a quick back-and-forth chat for trying things, and a saved file for real programs. Knowing when to use which saves you a lot of retyping.

The REPL (the >>> prompt) runs one line at a time and is great for quick experiments, while a .py script runs a whole file from top to bottom with python file.py.

The jaadu chats on an old rotary phone with one hand while sliding a sealed envelope into a red letterbox with the other.

Think of it like this

A phone call vs a letter

The REPL is a phone call: you say one line, Python answers immediately, and when you hang up the conversation is gone. A script is a letter: you write the whole thing, post it, and Python reads it top to bottom. You can send the same letter again tomorrow without rewriting it.

Watch it happen

REPL or script?

REPL (>>>)

  • Start with python3 (or python) and no file name
  • Each line runs the moment you press Enter
  • Shows the value of an expression automatically
  • Everything is lost when you exit()

Script (.py file)

  • Run with python3 file.py
  • Runs the whole file from top to bottom
  • Only shows what you print()
  • Saved, shareable and runnable again any time

Test a small idea in the REPL, then put anything you want to keep into a script.

The big ideas

What you need to know

The >>> prompt means Python is listening

Type an expression like 7 * 24 and the answer appears below. A ... prompt means Python is waiting for you to finish a multi-line block.

The REPL echoes values, scripts don't

Typing price * 2 in the REPL shows the result. The same line in a script calculates it silently. Use print() in scripts to see anything.

Run scripts from the right folder

python3 hello.py looks for hello.py in the terminal's current folder. Use cd to move there first, or give the full path.

Leaving the REPL

Type exit() or press Ctrl+D (macOS and Linux) or Ctrl+Z then Enter (Windows). In Python 3.13 and newer, plain exit also works.

See it in code

Walk through the code

text
1$ python32>>> 7 * 2431684>>> city = "Jaipur"5>>> city.upper()6'JAIPUR'7>>> exit()8 9$ cat hello.py10print("Hello from a script!")11$ python3 hello.py12Hello from a script!

1/5Running python3 with no file name opens the interactive REPL.

A short REPL session, then the same kind of work saved in a file and run as a script.

Save yourself the pain

Common mistakes

Oops

Typing python3 hello.py at the >>> prompt and getting a SyntaxError.

Do this instead

That is a terminal command. Exit the REPL first, then run it from the normal shell prompt.

Oops

Writing total * 2 on its own line in a script and wondering why nothing shows.

Do this instead

Scripts don't echo values. Write print(total * 2) to see the result.

Oops

Getting "No such file or directory" because the terminal is in a different folder.

Do this instead

Run pwd (or cd on Windows) to see where you are, then cd into the folder that contains the script.

Your mission

Chat first, then write it down

~15 min

Quick check

A script contains only the line 5 + 5. What does running python3 script.py show?

Go deeper

  1. 1Open the REPL and try a few calculations and print() calls, then exit with exit().
  2. 2Save the same code in a .py file and run it from the terminal.
  3. 3Use the REPL to test small ideas before adding them to your scripts.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge