Table of Contents
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.
- On Windows, you’ll usually use Command Prompt, PowerShell, or the Windows Terminal app.
- On macOS, you’ll use the Terminal app.
- On Linux, you’ll use your system’s terminal (often just called Terminal, Konsole, GNOME Terminal, etc.).
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:
- Press
Winkey, typecmd, press Enter → opens Command Prompt - Or press
Winkey, typepowershell, press Enter → opens PowerShell - Or open Windows Terminal if you have it (it can host both cmd and PowerShell tabs).
The prompt usually looks like:
- Command Prompt:
C:\Users\YourName> - PowerShell:
PS C:\Users\YourName>
macOS
- Open Launchpad → search for Terminal → open it
or - Press
Cmd + Space, typeTerminal, press Enter.
The prompt often looks like:
yourname@Your-Mac ~ %
or
yourname@Your-Mac ~ $
Linux
This depends on your desktop environment, but typical options:
- Press
Ctrl + Alt + T - Or find Terminal in your applications menu.
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:
- On Windows (try in this order):
python --version- If that fails, try:
py --version - On macOS / Linux:
python3 --version- If that fails, try:
python --version
You should see something like:
Python 3.12.1If you get a message like:
'python' is not recognized as an internal or external command(Windows)command not found: python(macOS / Linux)
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:
- Type Python code.
- Press Enter.
- Instantly see the result.
To start it, run one of these from your terminal:
- Windows (Command Prompt or PowerShell):
- Try:
python- If that doesn’t work, try:
py- macOS / Linux:
python3or (if Python 3 is the default):
pythonIf 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:
exit()then Enter
or- Press
Ctrl + Zthen Enter (Windows, in cmd) - Press
Ctrl + D(macOS / Linux, and many terminals).
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
50A few things to notice:
- You don’t type
>>>yourself; Python prints it. - You type the code after
>>>. - Press Enter to run the line and see the result.
You can also assign variables:
>>> name = "Alice"
>>> name
'Alice'
>>> age = 30
>>> age + 5
35If 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
- Start interactive Python (try in this order):
pythonpy- Check version:
python --versionpy --version- Exit interactive mode:
- Type
exit()and press Enter - Or press
Ctrl + Z, then Enter
On macOS / Linux
- Start interactive Python:
python3- or (if Python 3 is the default):
python - Check version:
python3 --version- or:
python --version - Exit interactive mode:
- Press
Ctrl + D - Or type
exit()and press Enter
Using the terminal history and editing
Typing the same thing many times is annoying. The terminal gives you simple shortcuts:
- Up arrow: go to the previous command you typed.
- Down arrow: move forward in your history.
- Left / Right arrows: move the cursor within the line.
- Home / End keys (on many systems): jump to the start or end of the line.
These work:
- In the terminal itself (when you’re not inside Python).
- In the Python interactive prompt (
>>>) as well.
This is useful if:
- You mistyped something but don’t want to retype the whole line.
- You want to run a similar command again with a small change.
Example workflow:
- You type:
>>> 123456 + 789- You realize you meant
987instead of789. - Press Up arrow to bring back:
>>> 123456 + 789- Use Left arrow to move to the
7, change to9, 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:
- Windows cmd:
C:\Users\YourName> - PowerShell:
PS C:\Users\YourName> - macOS / Linux:
yourname@computer:~$
Typical Python prompt:
>>>(main prompt)...(continuation prompt for multi-line statements likeif,for, etc.)
What this means for you:
- When you see instructions like
python3orpython
you should type them at the system prompt. - When you see something starting with
>>>, that means
you are already inside Python.
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:
- macOS / Linux:
python3 -c "print('Hello from one-line command!')"- Windows (Command Prompt or PowerShell):
python -c "print('Hello from one-line command!')"This will:
- Start Python.
- Run the code you provided.
- 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:
- On Windows, try
pyinstead ofpython. - On macOS / Linux, try
python3instead ofpython.
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
53. Forgetting how to exit the Python prompt
If you see >>> and don’t know how to leave:
- Try typing
exit()and pressing Enter. - If that doesn’t work or you’re stuck, try:
Ctrl + Zthen Enter (Windows, cmd)Ctrl + D(macOS / Linux / many terminals)
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:
- Open your terminal.
- Check Python:
python --versionorpython3 --versionorpy --version.- Start interactive mode:
pythonorpython3(orpyon Windows).- At the
>>>prompt, type:
>>> 10 + 20
>>> "YourName" * 3
>>> x = 7
>>> x * 2- Use the Up arrow to repeat a line and change a value.
- Exit using
exit()or the appropriate keyboard shortcut. - 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.