Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 149 ยท Getting Started

Running Python Code

Learn the ways to run Python - the interactive REPL, script files, editors and IDEs - how to install it, and what the in-browser runner on this site can do.

Python code is just text. Something has to read that text and carry out the instructions, and that something is the Python interpreter: a program, usually called python or python3, that runs your code line by line. Every way of "running Python" is really a way of handing code to that interpreter.

In this lesson you'll meet the main ones: the interactive prompt (the REPL) for quick experiments, script files for real programs, and code editors that tie it all together. You'll also learn how the editors on this site run genuine Python inside your browser, and where their limits are.

The interpreter

When people say "Python", they usually mean CPython, the reference interpreter written in C and published at python.org. New versions come out every October; this course uses Python 3.14, and everything you learn works on any recent 3.x release unless a lesson says otherwise.

Python 2 is gone. Python 2 reached its end of life in 2020. If a tutorial uses print "hello" without parentheses, it is teaching Python 2. Skip it.

You can ask the interpreter which version it is. The sys module (part of the standard library that ships with every Python) knows:

Python Which Python is this?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

On your own computer, sys.platform would say win32, darwin (macOS) or linux. Here it says emscripten, a hint that this Python was compiled to run inside a web browser. More on that below.

Installing Python on your computer

You don't need to install anything to follow this course, but you'll want Python locally once you start writing your own programs. The broad steps are:

  • Windows: download the official installer from python.org, or get Python from the Microsoft Store. Recent releases also offer a Python install manager that lets you keep several versions side by side. Windows uses the py command as a launcher, so py --version is a good first test.
  • macOS: download the installer from python.org or use a package manager such as Homebrew. The command is usually python3.
  • Linux: most distributions already include Python 3, and the command is usually python3. Check what you have first with python3 --version; if you need a newer one, use your distribution's package manager (see below).

Installing Python on Linux

Before you install, run a short checklist: check the current version (python3 --version), make sure you have administrator rights (sudo), and refresh your package lists. Many tools on Linux depend on the system Python, so install a newer version alongside it and never remove or replace the system one.

Distribution family Install command
Debian, Ubuntu, Mint (APT) sudo apt update && sudo apt install python3 python3-pip python3-venv
Fedora (DNF) sudo dnf install python3 python3-pip
CentOS, RHEL, Rocky, Alma (YUM or DNF) sudo yum install python3 python3-pip
Arch, Manjaro (Pacman) sudo pacman -S python python-pip
openSUSE (Zypper) sudo zypper install python3 python3-pip

Package names vary a little between releases (some offer versioned packages such as python3.13), and Debian-based systems split venv and pip into separate packages. If your distribution's repository only has an older Python, use a tool that builds or downloads a specific version into your home folder, such as uv or pyenv, or build from source on your own. Afterwards, check that python3 --version and python3 -m pip --version both work, then create a virtual environment for each project.

Installing Python on macOS

Older macOS versions shipped a system Python 2, and current ones only provide a small stub that points you at Apple's command line developer tools, so install your own copy. There are two good routes, and both work on Apple silicon (M-series) and Intel Macs.

  1. The official installer. Download the macOS installer package from python.org, open it and click through the prompts. It installs to /Library/Frameworks/Python.framework and puts python3 and pip3 on your PATH. The python.org build is a universal binary, so one download serves both chip families.
  2. Homebrew. If you already use Homebrew, run brew install python. Homebrew updates Python with brew upgrade python.

Then open Terminal and check with python3 --version and python3 -m pip --version. Type python3, not python: a plain python command usually doesn't exist on a Mac until you create an alias. If the installer from python.org seems not to take effect, the usual causes are a shell that cached an old path (open a new Terminal window) or a PATH line in ~/.zshrc that lists another Python first. which python3 shows which one wins. Never delete or replace the Apple-provided tools, and use a virtual environment for each project.

After installing, open a terminal (Command Prompt, PowerShell or Terminal) and check the version:

TEXT
$ python3 --version
Python 3.14.0

If the command isn't found, the installer probably didn't add Python to your PATH, the list of folders your terminal searches for programs. Re-run the installer and look for an "Add to PATH" option.

The REPL: Python's interactive prompt

Type python3 (or py on Windows) with no file name and you land in the REPL, short for Read, Evaluate, Print, Loop. It reads one line, runs it, prints the result, and waits for the next line:

TEXT
>>> 6 * 7
42
>>> name = "Ada"
>>> name.upper()
'ADA'
>>> exit

The >>> is the prompt. Notice two things:

  • You never called print(), yet the REPL showed 42 and 'ADA'. In the REPL, the value of a bare expression is echoed automatically.
  • Strings are echoed with their quotes ('ADA'). That's the REPL showing you the value's representation, which is meant for programmers.

When a statement needs more than one line, such as a loop, the prompt changes to ... until you finish the block with an empty line. Since Python 3.13 the REPL has colors, multi-line editing and history, and typing exit or quit (without parentheses) leaves it.

The REPL is perfect for checking how something behaves: "what does round(2.5) give?" takes five seconds to answer. It is not where you write programs, because everything disappears when you close it.

Scripts: saving code in files

A script is a plain text file with the .py extension. You run it by passing the file name to the interpreter:

TEXT
$ python3 greet.py

Scripts behave differently from the REPL in one important way: nothing is echoed automatically. A bare expression is computed and then thrown away. You only see what you explicitly print(). The editors on this site work like scripts, so try it:

Python Scripts only show what you print

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

repr() gives the same view the REPL echoes, which is useful when you need to see whether a value is the number 42 or the text "42".

The interpreter has a few other handy modes you'll meet later:

  • python3 -m module_name runs a module from the standard library or an installed package, for example python3 -m venv .venv to create a virtual environment.
  • python3 -c "print(2 ** 100)" runs a short snippet given on the command line.
  • python3 -i script.py runs a script and then drops you into the REPL with its variables still available, which is great for poking around after a bug.

Editors and IDEs

You can write .py files in any text editor, but a good code editor makes you much faster. It highlights syntax, flags errors as you type, completes names, and runs your script with one key press.

  • Visual Studio Code with the Python extension is a free, popular choice for all levels.
  • PyCharm is a full IDE (Integrated Development Environment) built specifically for Python, with a free edition.
  • IDLE comes bundled with Python itself. It's basic, but it's already on your machine.
  • Jupyter notebooks mix code, output and notes in "cells". They are popular in data science.

Tip: Whatever editor you pick, set it to insert 4 spaces when you press Tab. Indentation is part of Python's syntax, as you'll see in Syntax and Indentation, and mixing tabs and spaces causes confusing errors.

How the editors on this site work

The code editors in these lessons run real CPython, not an imitation. It is built with Pyodide, a project that compiles CPython to WebAssembly so it runs inside a browser. When you press Run the first time, your browser downloads the interpreter (a few seconds), and every later run on the page is fast.

Your code runs in a separate, sandboxed background worker. That keeps the page responsive even if your code loops forever, and it means your code can't see or change the page, your cookies or anything else of ours. Each run starts with a completely fresh interpreter, so variables from one editor (or from a previous run) don't carry over.

Most of the standard library works, including a temporary in-memory file system. You can create and read files, as long as you do it within one run:

Python An in-memory file system

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The editors also have an Input box. Because the code runs without pausing to wait for you, input() reads its answers from that box, one line per call. Some lessons fill it in for you:

Python Answers from the Input box

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Change the lines in the Input box and run it again. If your code calls input() more times than there are lines, you'll get an EOFError (End Of File) telling you to add another line.

What doesn't work in the browser

A browser sandbox is not a full computer, so a few things behave differently from Python on your machine:

  • Files are temporary. The file system lives in memory and is wiped after every run.
  • No internet access from your code. Downloading web pages or calling APIs won't work here.
  • No threads or subprocesses. Programs can't start other programs or run code in parallel threads.
  • No desktop windows. GUI toolkits such as tkinter aren't available.
  • Time limit. A run stops after 30 seconds, and the Stop button ends it sooner. Use it if you accidentally write an infinite loop.
  • Large packages download on demand. Importing a big package such as numpy fetches it first, which takes a while. This course only does that where it's worth it.

Everything in this course has been written to work within those limits. When a topic needs something the browser can't do, the lesson shows the code in a read-only block and explains what it would do on your computer.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. You type 10 / 4 on its own line in a .py script and run it. What appears?

    Scripts don't echo values. The expression is computed and discarded. Only the REPL shows the value of a bare expression; in a script you need print(10 / 4).
  2. 2. What does REPL stand for?

    The REPL reads a line, evaluates it, prints the result, and loops back for more.
  3. 3. Which command runs a module from the standard library as a program?

    -m runs a module by name. -c runs a code string, and -i keeps an interactive prompt open after a script finishes.
  4. 4. In this site's editors, a program calls input() three times but the Input box has two lines. What happens?

    Each input() reads the next line from the Input box. When none are left, Python raises EOFError, just like a script whose input stream has ended.
  5. 5. Why should your editor insert spaces instead of tab characters?

    Python uses indentation to group code into blocks. The convention is 4 spaces per level, and consistent spaces avoid hard-to-see mistakes.

Summary

  • The interpreter (CPython from python.org) runs your code. Check it with python3 --version or py --version.
  • The REPL (>>>) runs one line at a time and echoes values. Great for experiments, but nothing is saved.
  • Scripts are .py files run with python3 file.py. They only show what you print().
  • -m, -c and -i run a module, a code string, or a script followed by the REPL.
  • Editors such as VS Code, PyCharm and IDLE make writing and running scripts easier. Indent with 4 spaces.
  • This site's editors run real Python 3.14 via Pyodide in a sandbox: fresh state each run, input from the Input box, temporary files, no network, threads or GUI, and a 30-second limit.

Next: Syntax and Indentation, where you'll learn the rules that decide how Python reads your code.