How to Run Python Code: Scripts vs. Interactive Mode

Python is used in many development fields and data analysis because it is easy to read and versatile. There are two main ways to run Python code: creating a script file (.py) or using “Interactive Mode” (REPL) to run code line by line.

This article explains these two basic methods.


目次

Creating and Running Python Script Files

For programs that involve multiple steps or need to be run repeatedly, you should write the code in a text file. The file extension is usually .py.

1. Create a Sample Script

First, let’s create a script that calculates an area and prints the result. Open a text editor (like VS Code or Notepad) and write the following code.

Save the file as file_executor.py.

def calculate_area(width, height):
    """
    Calculates area from the given width and height.
    """
    if width <= 0 or height <= 0:
        return 0
    return width * height

# Execute calculation
base_width = 15
base_height = 25
area_result = calculate_area(base_width, base_height)

print(f"Width: {base_width}, Height: {base_height}")
print(f"The calculated area is {area_result}.")

This script defines a function called calculate_area, calculates the area for a width of 15 and a height of 25, and outputs the result.

2. Run the Script from the Terminal

To run the .py file, use the terminal (Command Prompt or PowerShell on Windows, Terminal on macOS/Linux).

  1. Open your terminal.
  2. Use the cd command to move to the directory (folder) where you saved file_executor.py.
  3. Enter the following command:
python file_executor.py

Execution Result:

Width: 15, Height: 25
The calculated area is 375.

Note: python vs python3 Depending on your environment (especially on macOS or Linux), you may need to use python3 instead of python. If you see an error like python: command not found, please try: python3 file_executor.py


Running in Python Interactive Mode (REPL)

Interactive Mode (REPL: Read-Eval-Print Loop) allows you to type Python code line by line and see the results immediately without creating a file. This is perfect for checking grammar, simple math, or testing library functions.

1. Start Interactive Mode

Type python (or python3) in your terminal and press Enter.

python

You will see the >>> symbol (prompt). This means Python is waiting for your input.

2. Run Code

Type Python code after >>>. It will be evaluated (Eval) and displayed (Print) immediately.

>>> message = "Hello, Python REPL"
>>> print(message)
Hello, Python REPL
>>>
>>> 5 * 8
40
>>>
>>> 10 / 4
2.5

3. Exit Interactive Mode

To exit the interactive mode, type exit() or use the following keyboard shortcuts:

  • macOS / Linux: Press Ctrl + D
  • Windows: Press Ctrl + Z and then Enter

Summary

It is important to use the right method for your purpose.

  • Script File (.py): Best for complete programs, complex logic, or code you need to run multiple times.
  • Interactive Mode (REPL): Best for checking short code, learning syntax, or testing libraries.

Master these basic methods and choose the one that fits your situation.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次