Module 1 · Beginner · Getting started

How to run Python: REPL, scripts and notebooks

Three ways to run Python code: the interactive shell (REPL), .py scripts in a terminal or VS Code, and Jupyter notebooks. When to use each, with examples.

By theprogrammers team · · 5 min read

There are three main ways to run Python code, and each one suits a different job:

WayBest forExample
The REPL (interactive shell)Trying out one line, quick maths, checking how something works>>> 2 + 3
Scripts (.py files)Real programs you save, rerun and sharepython hello.py
Notebooks (.ipynb files)Data analysis, charts, step-by-step experimentsJupyter, Google Colab

This guide shows how to use all three, so you can pick the right one each time. If Python isn't installed yet, start with how to install Python and set up VS Code.

1. The REPL: Python's interactive shell

REPL stands for Read, Evaluate, Print, Loop: you type a line, Python runs it, shows the result and waits for the next one. Open a terminal and type python (Windows) or python3 (macOS and Linux):

Python 3.13.5 ...
Type "help", "copyright", "credits" or "license" for more information.
>>> 

The >>> is the prompt: Python is waiting for you. Try a few lines:

>>> 2 + 3
5
>>> 10 / 4
2.5
>>> name = "Ahana"
>>> name.upper()
'AHANA'
>>> len(name)
5

You don't need print() here: the REPL shows the value of each expression automatically. For code that spans several lines, the prompt changes to ..., and you press Enter on an empty line to finish:

>>> for i in range(3):
...     print(i)
...
0
1
2

Useful REPL tricks:

  • Press the up arrow to bring back earlier lines.
  • help(len) explains what a function does, and dir("hi") lists everything a string can do.
  • _ holds the last result: after 2 + 3, typing _ * 10 gives 50.
  • Type exit() or press Ctrl + D (macOS/Linux) or Ctrl + Z then Enter (Windows) to leave.

The REPL is perfect for experiments, but nothing you type is saved. For anything longer than a few lines, use a script.

2. Scripts: saving code in .py files

A script is a text file ending in .py. Create greet.py:

name = input("What's your name? ")
print(f"Nice to meet you, {name}!")

Running a script from the terminal

In the terminal, move to the folder that holds the file with cd, then run it:

cd Documents/python-course
python greet.py

On macOS and Linux, use python3 greet.py. On Windows, py greet.py also works.

What's your name? Rohan
Nice to meet you, Rohan!

Unlike in the REPL, a script shows only what you print. A line like 2 + 3 on its own calculates 5 and throws it away silently.

Running a script in VS Code

Open the file and click the ▶ Run button at the top right, or right-click in the editor and choose Run Python File in Terminal. VS Code runs the same python greet.py command for you in its built-in terminal.

Getting "No such file or directory"? The terminal is in a different folder than your file. Run cd to the right folder first, or open the folder in VS Code with File › Open Folder.

3. Notebooks: Jupyter and Google Colab

A notebook mixes code, results, charts and notes in one document, split into cells. You run one cell at a time and see its output right below it, which makes notebooks the favourite tool for data analysis and machine learning.

Ways to use notebooks:

  • Google Colab (colab.research.google.com): free, runs in the browser, nothing to install. You need a Google account.
  • VS Code: install the Jupyter extension, then create a file ending in .ipynb.
  • JupyterLab: install it with python -m pip install jupyterlab, then start it with jupyter lab.

In a notebook, the last line of a cell is displayed automatically, just like in the REPL:

marks = [72, 88, 95]
sum(marks) / len(marks)    # the cell shows 85.0

One trap: cells share variables and can run in any order. If results look strange, use Restart and Run All to run everything from top to bottom.

Notebooks are used throughout our pandas DataFrame tutorial. For apps, tools and anything you'll reuse, scripts are better.

Which should you use?

  • Checking one thing quickly? The REPL.
  • Writing a program, even a small one? A script in VS Code. This is what you'll use most in this course.
  • Exploring data or drawing charts? A notebook.

Key takeaways

  • The REPL (>>>) runs one line at a time and shows results automatically, but doesn't save your work.
  • Scripts are .py files run with python file.py or the ▶ button in VS Code, and only show what you print.
  • Notebooks combine code, output and notes in cells, and are the standard tool for data work.

Practice

  1. Open the REPL and use it as a calculator: how many seconds are there in a day?
Show the answer
>>> 24 * 60 * 60
86400
  1. Save this as test.py and run it. Why does it print only one number?
5 + 5
print(10 + 10)
Show the answer

It prints only 20. In a script, a value that isn't printed is calculated and discarded; only the REPL and notebooks display values automatically.

  1. In the REPL, run help(round). What does the second argument of round do?
Show the answer

It sets the number of decimal places: round(3.14159, 2) gives 3.14. Press q to leave the help screen if it opens in a pager.

Interview questions

What is the REPL?

The Read-Evaluate-Print Loop is Python's interactive shell, started by running python with no file. It reads a line, runs it, prints the result and waits for more input. It's used for quick experiments and exploring objects with help() and dir().

What does if __name__ == "__main__" do?

It runs the code below it only when the file is run directly as a script, not when it's imported as a module by another file. When Python runs a file directly, it sets that file's __name__ to "__main__"; when importing, __name__ is the module's name.

What is a .pyc file?

A compiled bytecode version of a Python module, stored in the __pycache__ folder. Python creates it when a module is imported so that later imports can skip the compile step. It's safe to delete; Python recreates it.

What to learn next

  1. Write and understand your first Python program, line by line.
  2. Learn to read Python error messages and tracebacks before they scare you.