Skip to content
absolute_beginner

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

Getting started with Python is exciting! One of the first things you'll want to learn is how to run Python code. There are several easy ways to do this,…

Published 2026-05-17Updated 2026-05-179 min read

Getting started with Python is exciting! One of the first things you'll want to learn is how to run Python code. There are several easy ways to do this, each with its own strengths. Whether you want to test a quick idea or build a full program, knowing your options will help you feel confident and ready to start real projects.

This guide will walk you through the main ways to run Python code: interactive mode, scripts, IDLE, Visual Studio Code, and online notebooks. You'll learn what each method is, when to use it, and how to get started.


Why Learn Different Ways to Run Python Code?

Python is flexible, and there's no single "right" way to run your code. Here’s why it helps to know multiple methods:

  • Different situations call for different approaches. Sometimes you want to test a single line of code; other times, you’ll want to run a whole program.
  • Some methods are better for quick experiments, others for building bigger projects. The interactive shell is perfect for learning and testing ideas, while scripts are best for saving and running more complex programs.
  • Knowing your options builds confidence. As you learn, you’ll find it easier to pick the right tool for the job and adapt to different environments.

What You Need Before You Start

Before you can run Python code, you need to make sure Python is installed on your computer.

How to check if Python is installed:

  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 commands and press Enter:

    python --version
    

    or

    python3 --version
    

If you see a version number like Python 3.11.0, you’re ready to go! If not, you’ll need to download and install Python. Python 3 is recommended for all new projects.


Running Python in Interactive Mode

One of the easiest ways to run Python code is with the interactive shell, also called the REPL (Read-Eval-Print Loop). This is a command-line environment where you can type Python commands and see the results instantly.

When to use interactive mode:

  • Testing small code snippets
  • Learning Python basics
  • Doing quick calculations

How to start the Python interactive shell:

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

You’ll see a prompt that looks like this:

>>>

Now you can type Python commands. For example:

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

Or try some math:

>>> 2 + 3
5

To exit:
Type exit() and press Enter, or press Ctrl + Z (Windows) or Ctrl + D (macOS/Linux).

Quiz Question 1

Question: Which method is best for quickly testing a single line of Python code?

  • A) Interactive mode (REPL)
  • B) Writing and running a script file
  • C) Using an online notebook

Running Python Scripts from a File

For longer programs or code you want to save, you’ll write your Python code in a file called a script. Python scripts have the .py extension.

What is a Python script?
A script is just a text file containing Python code.

How to create and run a Python script:

  1. Open a text editor (like Notepad, VS Code, or any editor you like).

  2. Type your Python code. For example:

    print("Hello from a script!")
    
  3. Save the file as hello.py (make sure it ends with .py).

  4. In your terminal, navigate to the folder where you saved the file. For example, if you saved it on your desktop:

    • Windows:
      cd Desktop
      
    • macOS/Linux:
      cd ~/Desktop
      
  5. Run the script by typing:

    python hello.py
    

    or

    python3 hello.py
    

You should see:

Hello from a script!

Common issues:

  • Make sure you’re in the correct folder.
  • Double-check the file extension is .py.

Quiz Question 2

Question: What file extension should a Python script have?

  • A) .txt
  • B) .py
  • C) .exe

Using IDLE: Python’s Built-in Editor

Python comes with a simple editor called IDLE (Integrated Development and Learning Environment). It’s designed for beginners and is included with most Python installations.

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:

  • Use the interactive shell (just like in the terminal).
  • Create and run scripts in a built-in editor.

Running code in IDLE:

  1. Interactive mode:
    When you open IDLE, you’ll see the >>> prompt. Type commands and see results instantly.

  2. Script mode:

    • Click File > New File to open the editor.
    • Write your code and save the file with a .py extension.
    • Press F5 or select Run > Run Module to run your script.

Why use IDLE?
It’s easy to use, requires no extra setup, and is perfect for beginners.

Quiz Question 3

Question: What does IDLE provide for beginners?

  • A) A built-in editor and interactive shell
  • B) A way to run code only online
  • C) A tool for compiling Python to C

Running Python Code in Visual Studio Code

Visual Studio Code (VS Code) is a popular, free code editor that works well for Python development.

Getting started with Python in VS Code:

  1. Install VS Code from the official website.
  2. Install the Python extension:
    • Open VS Code.
    • Go to Extensions (the square icon on the sidebar).
    • Search for “Python” and install the one from Microsoft.

Ways to run Python code in VS Code:

  • Run entire 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 in VS Code (View > Terminal) and type python script.py.

Why use VS Code?
It offers helpful features like code completion, debugging, and easy navigation—great as you start building bigger projects.


Trying Python in Online Notebooks

You don’t have to install anything to try Python. Online notebooks let you write and run Python code in your browser.

What are online notebooks?
Tools like Jupyter Notebook and Google Colab let you run Python code in small, editable blocks called “cells.” They’re perfect for learning, experimenting, and sharing code.

How to use an online notebook:

Type your code in a cell and click “Run” (usually a play button). You’ll see the output right below your code.

Benefits of online notebooks:

  • No installation needed
  • Easy to experiment and share code
  • Great for learning and visualizing results

Quiz Question 4

Question: What is one advantage of using online notebooks like Jupyter or Google Colab?

  • A) They require no installation
  • B) They only work on Windows
  • C) They can't display output

Running Python Scripts from the Terminal

Running scripts from the terminal is a common way to execute Python code, especially as your projects grow.

How to run a Python script from the terminal:

  1. Open your terminal or command prompt.

  2. Navigate to the folder where your script is saved.

  3. Type:

    python script.py
    

    or

    python3 script.py
    
  4. Press Enter to run your script.

Tip: If you get an error saying 'python' is not recognized, try using python3 instead, or check your Python installation.

Quiz Question 5

Question: How can you run a Python script from the terminal?

  • A) Type 'python script.py' and press Enter
  • B) Double-click the .py file
  • C) Type 'run script.py' in the terminal

Which Method Should You Use?

With so many ways to run Python code, how do you choose?

Diagram comparing different ways to run Python code: interactive mode, scripts, IDLE, VS Code, and online notebooks, showing their typical use cases and benefits.

Overview of the main ways to run Python code, with typical use cases and benefits for each method. This helps beginners quickly see their options and when to use each.

  • Quick tests or learning:
    Use interactive mode (terminal or IDLE).
  • Saving and running programs:
    Write scripts and run them from the terminal, IDLE, or an editor like VS Code.
  • Experimenting and sharing:
    Try online notebooks like Jupyter or Google Colab.

Don’t be afraid to try each method. As you practice, you’ll find what feels most comfortable and useful for your projects.


Conclusion

Learning how to run Python code is the first step toward building real projects and growing your programming skills. Whether you use the interactive shell, scripts, IDLE, VS Code, or online notebooks, each method has its place. Try them out, experiment, and see which fits your style best. With practice, running Python code will soon feel like second nature.

Ready for the next step? Check out our next beginner-friendly tutorial to keep building your Python skills!


Quiz Answer Key

Question 1

Correct answer: A) Interactive mode (REPL)

Explanation: Interactive mode lets you type and run code one line at a time, making it ideal for quick tests.


Question 2

Correct answer: B) .py

Explanation: Python scripts are saved with the .py extension so Python knows how to run them.


Question 3

Correct answer: A) A built-in editor and interactive shell

Explanation: IDLE includes both an editor for scripts and an interactive shell for quick commands, making it beginner-friendly.


Question 4

Correct answer: A) They require no installation

Explanation: Online notebooks run in your browser, so you don't need to install anything.


Question 5

Correct answer: A) Type 'python script.py' and press Enter

Explanation: Running 'python script.py' in the terminal executes the script using Python.

Keep learning

Related tutorials

Continue with nearby Python topics and beginner-friendly explanations.

absolute_beginner8 min read

Your First Python Program

Welcome to your very first step into the world of programming! If you’re here, you’re ready to learn Python—a language loved by beginners and professionals…

Read tutorial