How to Run Python in the Terminal

Step-by-step Guide

To run Python in your terminal, follow these simple steps:

  1. Open your terminal (Command Prompt, PowerShell, or Terminal on macOS/Linux).
  2. Type python or python3 depending on your OS and installation.
  3. Press Enter. This will start the Python interactive shell (REPL).
$ python3
Python 3.11.5 (main, Sep  6 2023, 18:43:08) [Clang 14.0.0] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello, world!")
Hello, world!

To exit the Python shell, type:

>>> exit()

Note: On Windows, use python. On Mac/Linux, it may be python3 if both Python 2 and 3 are installed.

Running a Python File

Using the Command Line

To run a script saved as a .py file:

$ python3 script_name.py

Example:

$ python3 hello.py

This will execute the content of hello.py and print any output to the terminal.

Check Python Version

Verify Installation

Use the following command:

$ python3 --version

This will display the installed Python version:

Python 3.11.5