Skip to content
absolute beginner

How to Run Python Code: Scripts, Interactive Mode, and More

Writing Python and running Python are two different jobs, and most beginners trip on the gap between them. An editor is where you type code. The Python…

Published 2026-05-17Updated 2026-09-159 min read
Aerial view of intricate sand patterns on a beach at sunset, capturing nature's artwork.
Aerial view of intricate sand patterns on a beach at sunset, capturing nature's artwork. Photo by Atahan Demir on Pexels.

Writing Python and running Python are two different jobs, and most beginners trip on the gap between them. An editor is where you type code. The Python interpreter is the program that executes it. Every terminal command, IDE button, and notebook cell is just a different way to hand your code to that one interpreter. Learn that handoff once, and every environment stops being a confusing menu.

The One Idea Behind Every Way to Run Python

A flowchart shows interactive commands, .py scripts, editor run buttons, and notebook cells converging on the Python interpreter, which produces program output.
Whether code starts in a terminal, file, editor, or notebook cell, the Python interpreter is the engine that executes it.

Get this straight before touching a command: nothing runs until your code reaches the interpreter.

A .py file is just text until the interpreter reads it. A notebook cell is just text until you tell it to run. The interpreter is the engine; everything else is the steering wheel. A terminal, a run button, or a notebook cell only decides how the code gets handed over.

That is the whole mental model. Everything in this guide is a variation on that single handoff.

Knowledge check

Check your understanding

Answer this question before you continue.

What is the role of the Python interpreter in the article's execution model?
Single Choice

Focus: Explain the shared execution model behind terminals, editors, and notebooks.

What You Need Before You Start

The interpreter must be installed before anything can run. Check for it first.

  1. Open a terminal or command prompt:

    • Windows: Press Win + R, type cmd, and press Enter.
    • macOS: Open Spotlight (Cmd + Space), type Terminal, and press Enter.
    • Linux: Open your terminal application.
  2. Type one of these and press Enter:

    python --version
    

    or

    python3 --version
    

If you see a version number like Python 3.11.0, you are ready. If not, install Python for your operating system before continuing. Python 3 is the right choice for all new work.

Note: On many systems, especially macOS and Linux, the command is python3 instead of python. If one does not work, try the other.

Your First Run: Interactive Mode

Start in interactive mode. It is the fastest way to run Python code because it gives you instant feedback with zero setup.

Interactive mode is also called the REPL, short for Read-Eval-Print Loop. That name is the mechanism: the interpreter reads your line, evaluates it, prints the result, and waits for the next one.

How to start it:

  1. Open your terminal or command prompt.
  2. Type python (or python3) and press Enter.

You will see a prompt that looks like this:

>>>

That >>> is the signal that you are now inside Python. Type a command and press Enter:

>>> print("Hello, Python!")
Hello, Python!

Try some math:

>>> 2 + 3
5

To exit: type exit() and press Enter, or press Ctrl + Z then Enter on Windows, or Ctrl + D on macOS and Linux.

Interactive mode is where you test a small idea, do a quick calculation, or check how a function behaves before committing it to a file. When you are unsure how something works, test it here first instead of guessing inside a bigger program.

The Next Step: Save a Script and Run It

Interactive mode answers a question. A script is what you keep. The moment code is worth running again, save it as a .py file and run it from the terminal. That move from experiment to repeatable file is the real progression.

Here is the handoff in action. The editor creates the file. The terminal starts Python and passes the file to it. The interpreter reads the file and runs it line by line.

Save this as hello.py:

print("Hello from a script!")

Now run it. The terminal only finds the file if it is in the folder you are currently in, so navigate there first. On Windows:

cd Desktop
python hello.py

On macOS or Linux:

cd ~/Desktop
python3 hello.py

If your file is saved somewhere else, point the command at the full path instead:

python /full/path/to/hello.py

You should see:

Hello from a script!

Common mistake: Typing python hello.py while your terminal is in a different folder. The interpreter looks for the file in the folder you are currently in. If you get a "No such file or directory" error, use cd to reach the right folder first, or pass the full file path.

Common mistake: Typing a shell command inside the Python interpreter. When you see the >>> prompt, you are inside Python, and it only understands Python syntax. Commands like python hello.py belong in the terminal itself, not at the >>> prompt. If you find yourself there, exit with exit() first, then run the command.

If you get an error saying 'python' is not recognized, try python3 instead, or confirm Python is installed correctly.

Knowledge check

Check your understanding

Answer this question before you continue.

You have tested a calculation and now want to run it again tomorrow. Which approach best matches the article?
Single Choice

Focus: Choose interactive mode or a saved script based on whether code needs to be repeated.

IDLE: Editor and Interpreter in One Window

IDLE is the editor that ships with Python. Its real value for a beginner is that it combines both modes in one window: an interactive shell with the >>> prompt, plus an editor for saved scripts.

How to open IDLE:

  • Windows: Search for "IDLE" in the Start menu.
  • macOS: Open Launchpad and search for "IDLE."
  • Linux: Type idle or idle3 in the terminal.

What you can do in IDLE:

  • Interactive mode: When IDLE opens, you see the >>> prompt. Type commands and see results instantly.
  • Script mode: Click File > New File, write your code, save it with a .py extension, then press F5 or select Run > Run Module.

The F5 shortcut is the whole point: it hands your saved file to the interpreter for you. You experiment in the shell, then move a working idea into a saved script without switching tools.

Knowledge check

Check your understanding

Answer this question before you continue.

Which statement about IDLE is correct?
Misconception Check

Focus: Identify how IDLE combines interactive execution with saved-script editing.

Visual Studio Code: Automating the Same Handoff

Visual Studio Code (VS Code) is a free code editor many developers use for Python. It is more powerful than IDLE, and it becomes more useful as your projects grow.

The key insight is that VS Code does not change how Python runs. It automates the handoff. When you click the run button, VS Code opens a terminal and runs the same python script.py command you would type yourself. The interpreter model has not changed.

Ways to run Python code in VS Code:

  • Run the whole script: Open your .py file and click the "Run Python File in Terminal" button (a small play icon at the top right).
  • Run selected lines: Highlight code, right-click, and choose "Run Selection/Line in Python Terminal."
  • Use the built-in terminal: Open the terminal (View > Terminal) and type python script.py.

VS Code adds useful features like code completion and debugging, but those are conveniences layered on top of the same execution. If you already understand the terminal command, the button is just a faster way to type it.

Online Notebooks: Cells That Remember

You do not have to install anything to run Python. Online notebooks let you write and run code in your browser, in small editable blocks called cells.

If you want a no-install option, Google Colab is a good single starting point: open the site, start a new notebook, and you get a ready-to-run cell. Type code in a cell and click the run button; the output appears right below.

Here is the part that surprises most beginners: notebook cells do not run like a script. A script always runs from the top, line by line. A notebook runs only the cell you click, and it remembers the results of cells you ran earlier. That persistence is powerful, but it has a cost.

Try this. In one cell, type:

total = 5

Run it. In the next cell, type:

print(total + 3)

Run that cell and you get 8. Now go back and run the second cell again without running the first. You still get 8, because the notebook remembers that total was set earlier. That is handy for exploration, but it also means the order you run cells becomes part of your result. Run cells out of order and you can get output that does not match the code you see on screen.

When an online notebook earns its place:

  • You are on a computer where you cannot install software
  • You want to experiment without setting up a local environment
  • You want to share a runnable example with someone else

For a normal program you will run repeatedly, a saved script is usually the better tool, because a script always runs top to bottom with no hidden state.

Knowledge check

Check your understanding

Answer this question before you continue.

A notebook first runs a cell containing `total = 5`, then runs a second cell containing `print(total + 3)`. What output does the article say the second cell produces?
Output Prediction

Focus: Predict how notebook state persists when cells are run independently.

Which Method Should You Use?

Here is the decision rule I use when teaching beginners. Ask the same three questions for every environment: where do you type the code, what executes it, and can you save or share the result?

The path is not a menu of equally good tools. It is a progression:

  1. Start in interactive mode to get instant feedback while you learn what Python does.
  2. Save a script the moment you write something you want to keep and run again.
  3. Reach for an editor or notebook when you need extra help or a different way to package that same handoff.
MethodBest forAvoid when
Interactive modeTesting one idea, quick math, exploring syntaxYou need to save or rerun the code
Script fileA program you will run againYou only need a one-line check
IDLEBeginner-friendly editor with both modesYou want advanced debugging tools
VS CodeGrowing projects with completion and debuggingYou want zero setup
Online notebookNo-install experimentation and sharingYou need a normal runnable program

Your Next Step

Pick one small program and run it through the whole loop. Start with a calculation in interactive mode, confirm the output, then save the same code as a script and run it from the terminal.

Try this: write a script that prints your name and a calculation, run it, then change one number and run it again. Watch the output change. That single observation — same code, new value, new result — is the execution model made visible.

The rule to carry forward: reach for interactive mode when you want an answer, save a script the moment the code is worth repeating, and switch to a notebook when cells and sharing are the point. Once that loop feels natural, you can stop thinking about how to run Python code and start thinking about what to build with it.

Knowledge check

Final check

Finish the article by checking the ideas you just learned.

A learner sees the `>>>` prompt and types `python hello.py`, expecting the saved script to run. What should the learner do?
Question 1 of 2Debugging

Focus: Distinguish terminal commands from Python code at the interactive prompt.

The terminal command is being entered while the learner is inside interactive Python mode.
Which method best fits a normal program that you will run repeatedly and want to execute from top to bottom without hidden notebook state?
Question 2 of 2Single Choice

Focus: Select a Python execution method based on the need for installation, repetition, and sharing.

References

  1. 1. Command line and environment — Python 3.14.7 documentationdocs.python.org
  2. Running Python code in Visual Studio Codecode.visualstudio.com
  3. How to Run Your Python Scripts and Code – Real Pythonrealpython.com
Practical resource

Want a more structured Python path?

Use the Python Starter Pack to turn scattered tutorials into a focused practice path.

View the bundle
Coming soon

Python Starter Pack

A compact LearnPyFast PDF pack covering what Python is, installation, your first program, running Python code, and Python versions.

$9
PDF BundleTopic PackPythonBeginner
  • 5 curated chapters
  • Enhanced PDF edition with bundle-only learning guidance
  • Offline-friendly format for focused review
  • Source article links for future online updates

Coming soon

Free Python bundle

Get the LearnPyFast Python for Artificial Intelligence Starter Bundle

Build a Python foundation you can actually use. The Python for Artificial Intelligence Starter Pack brings together a guided path through setup, core programming concepts, data structures, files, JSON, APIs, debugging, and practical projects—so you can move quickly from running your first program to understanding and building useful software.

You’ll receive the bundle by email. You can unsubscribe anytime.

No spam. You can unsubscribe anytime. See our Privacy policy.

Related sites

Continue beyond Python

Explore related Worldmonger sites when you want to move from Python basics into JavaScript or LLM application building.

JavaScript tutorialstutorial

LearnJSFast

Beginner-friendly JavaScript tutorials for practical web development and self-taught developers.

JavaScriptFrontendWeb development
Visit LearnJSFast
LLM tutorialstutorial

LearnLLMFast

Practical LLM tutorials for builders who want to understand prompting, workflows, agents, and AI applications.

LLMAIBuilders
Visit LearnLLMFast

Keep learning

Related tutorials

Continue with nearby Python topics and beginner-friendly explanations.

Expansive desert landscape with golden sand dunes illuminated by sunrise, showcasing natural patterns and tranquility.
absolute beginner
6 min read

Your First Python Program

Your first program is not really about the words "Hello, World!" It is about proving the whole loop works: you write code, the computer runs it, and you…

Read tutorial
A woman engineer focuses on software analysis using a laptop indoors.
absolute beginner
8 min read

How to Install Python

Python is installed when your computer can do two things: find the python command, and run a tiny program with it. This guide walks you through that on…

Read tutorial