To run Python code in the terminal, follow these steps.
- Open your terminal.
- Type
python
orpython3
to start the Python interpreter. - Type your Python code in the terminal and press
Enter
to run it.
For example, to print “Hello, World!” in Python, type the following in the terminal:
python3
>>> print("Hello, World!")
Hello, World!
Alternatively, you can also run a Python script saved in a file by typing python3 file_name.py
in the terminal, where file_name.py
is the name of the Python file you want to run.
Here is an example of executing Python on the command line to start the Python interpreter:
$ python3
Python 3.9.0 (default, Oct 5 2021, 14:01:47)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
In this example, $
represents the terminal prompt and python3
is the command to start the Python interpreter. After you run this command, you’ll see the Python prompt (>>>
) indicating that the interpreter is ready to accept your commands.
You can type Python code at the prompt and press Enter
to run it. For example:
>>> print("Hello, World!")
Hello, World!
>>>
You can exit the Python interpreter by typing exit()
or quit()
at the prompt, or by pressing Ctrl + D
.