Kahibaro
Discord Login Register

Using Python in the terminal

What “the terminal” means

The terminal (also called “command line” or “shell”) is a text-based way to talk to your computer.
You type commands, press Enter, and see text output.

This chapter focuses on how to start and use Python from the terminal, not how to install Python or use an IDE (those have their own sections).


Opening a terminal on your system

You only need to know how to open a terminal and type commands.

Windows

You can use Command Prompt or PowerShell.

Common ways:

The prompt usually looks like:

macOS

The prompt often looks like:

yourname@Your-Mac ~ %

or

yourname@Your-Mac ~ $

Linux

This depends on your desktop environment, but typical options:

Prompt often looks like:

yourname@computer:~$


Checking that Python works in the terminal

Before using Python from the terminal, verify that the python command is recognized.

Type one of these and press Enter:

You should see something like:

Python 3.12.1

If you get a message like:

then Python is not correctly set up in the terminal. That setup is handled in the installation section, not here; for now, just know you must use a command that exists on your system (python, python3, or py).


Starting Python’s interactive mode from the terminal

Python has an interactive mode (also called the REPL: Read–Eval–Print Loop).
In this mode you:

  1. Type Python code.
  2. Press Enter.
  3. Instantly see the result.

To start it, run one of these from your terminal:

    python
    py
  python3

or (if Python 3 is the default):

  python

If it works, you’ll see something like:

Python 3.12.1 (tags/...)
Type "help", "copyright", "credits" or "license" for more information.
>>>

The >>> is the Python prompt. It means Python is ready for your commands.

To leave interactive mode, use:

You’ll return to your normal terminal prompt (like C:\Users\...> or yourname@computer:~$).


Trying simple commands in interactive mode

Once you see the >>> prompt, you can type Python code.

Examples:

>>> 2 + 3
5
>>> "Hello, Python!"
'Hello, Python!'
>>> 10 * 5
50

A few things to notice:

You can also assign variables:

>>> name = "Alice"
>>> name
'Alice'
>>> age = 30
>>> age + 5
35

If you make a typo and see an error, Python prints a message; that’s normal. You’ll learn to read these later in the course.


Python’s primary terminal commands on each OS

Because different systems use different commands, it helps to know the main ones you’ll use in this course:

On Windows

On macOS / Linux

Using the terminal history and editing

Typing the same thing many times is annoying. The terminal gives you simple shortcuts:

These work:

This is useful if:

Example workflow:

  1. You type:
   >>> 123456 + 789
  1. You realize you meant 987 instead of 789.
  2. Press Up arrow to bring back:
   >>> 123456 + 789
  1. Use Left arrow to move to the 7, change to 9, press Enter.

Difference between terminal prompt and Python prompt

It’s easy to get confused between the system prompt and the Python prompt.

Typical system prompt examples:

Typical Python prompt:

What this means for you:

Example session (with comments explaining what’s happening):

# You are at the system prompt:
yourname@computer:~$ python3
# Now Python starts, and you see:
Python 3.12.1 (tags/...)
Type "help", "copyright", "credits" or "license" for more information.
>>> 1 + 2
3
>>> exit()
# You are back to system prompt:
yourname@computer:~$

You never type the # comments or the prompts themselves; they are shown here just for explanation.


Running a single Python command from the terminal

Sometimes you want to run a tiny piece of Python directly from the terminal without entering interactive mode. You can do this using the -c option.

The basic form is:

$$
\text{python command} \; -c \; " \text{python\_code\_here} "
$$

Replace python command with whatever works on your system (python, python3, or py).

Examples:

  python3 -c "print('Hello from one-line command!')"
  python -c "print('Hello from one-line command!')"

This will:

  1. Start Python.
  2. Run the code you provided.
  3. Exit immediately, returning you to the normal terminal prompt.

You won’t use this all the time as a beginner, but it’s useful to know it exists.


Common beginner issues when using Python in the terminal

1. “python: command not found” / “‘python’ is not recognized…”

Meaning: The python command isn’t set up in your system’s PATH or the name is different.

What to try:

If none of these work, check the installation chapter and make sure Python was added to PATH (Windows) or installed properly (macOS / Linux).

2. Typing Python code at the system prompt

Example:

yourname@computer:~$ 2 + 3

Result: The shell (not Python) doesn’t know what 2 + 3 means, so you get an error.

Fix: Start Python interactive mode first:

yourname@computer:~$ python3
Python 3.12.1 (tags/...)
>>>

Then type:

>>> 2 + 3
5

3. Forgetting how to exit the Python prompt

If you see >>> and don’t know how to leave:

You’ll know you’re out when you see the normal system prompt again.


Practice suggestions

To get comfortable with Python in the terminal, try this sequence:

  1. Open your terminal.
  2. Check Python:
    • python --version or python3 --version or py --version.
  3. Start interactive mode:
    • python or python3 (or py on Windows).
  4. At the >>> prompt, type:
   >>> 10 + 20
   >>> "YourName" * 3
   >>> x = 7
   >>> x * 2
  1. Use the Up arrow to repeat a line and change a value.
  2. Exit using exit() or the appropriate keyboard shortcut.
  3. Back at the system prompt, run a one-line command:
   python -c "print('Practicing from the terminal!')"

(Adjust python to python3 or py as needed.)

In later chapters, you’ll learn how to run full Python scripts from the terminal and combine this knowledge with variables, input, and more.

Views: 22

Comments

Please login to add a comment.

Don't have an account? Register now!