KAHIBARO Bookshelves
Discord Login Register

2.2. 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.

  • 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 Win key, type cmd, press Enter → opens Command Prompt
  • Or press Win key, type powershell, 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, type Terminal, 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.1

If 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:

  1. Type Python code.
  2. Press Enter.
  3. 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:
  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:

  • exit() then Enter
    or
  • Press Ctrl + Z then 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
50

A 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
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

  • Start interactive Python (try in this order):
    • python
    • py
  • Check version:
    • python --version
    • py --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:

  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:

  • 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 like if, for, etc.)

What this means for you:

  • When you see instructions like
    python3 or python
    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:

  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:

  • On Windows, try py instead of python.
  • On macOS / Linux, try python3 instead of python.

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:

  • Try typing exit() and pressing Enter.
  • If that doesn’t work or you’re stuck, try:
    • Ctrl + Z then 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:

  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: 0

Comments

Please login to add a comment.

Don't have an account? Register now!

Where to Move

Move chapter:

  • ☰ 1. Chapter 1: What Is Programming?
    • ☰ 1.1. What is a computer program?
      • ☰ 1.2. What is Python?
        • ☰ 1.3. Where is Python used?
          • ☰ 1.4. How programming helps solve problems
            • ☰ 1.5. Your first Python program
            • ☰ 2. Chapter 2: Setting Up Python
              • ☰ 2.1. Installing Python (Windows / macOS / Linux)
                • ☰ 2.2. Using Python in the terminal
                  • ☰ 2.3. Using an IDE (IDLE / VS Code)
                    • ☰ 2.4. Running Python scripts
                      • ☰ 2.5. Python interactive mode
                      • ☰ 3. Chapter 3: Variables and Data Types
                        • ☰ 3.1. What is a variable?
                          • ☰ 3.2. Creating variables
                            • ☰ 3.3. Numbers
                              • ☰ 3.4. Test
                                • ☰ 3.5. Boolean values
                                  • ☰ 3.6. Type checking
                                  • ☰ 4. Chapter 4: Basic Operations
                                    • ☰ 4.1. Arithmetic operations
                                      • ☰ 4.2. Working with numbers
                                        • ☰ 4.3. String operations
                                          • ☰ 4.4. Combining text and variables
                                            • ☰ 4.5. Comments in Python
                                            • ☰ 5. Chapter 5: User Input and Output
                                              • ☰ 5.1. Getting input from the user
                                                • ☰ 5.2. Converting input to numbers
                                                  • ☰ 5.3. Formatting output
                                                    • ☰ 5.4. Writing interactive programs
                                                    • ☰ 6. Chapter 6: Conditions
                                                      • ☰ 6.1. Comparison operators
                                                        • ☰ 6.2. Logical operators
                                                          • ☰ 6.3. if, elif, else
                                                            • ☰ 6.4. Nested conditions
                                                              • ☰ 6.5. Common beginner mistakes
                                                              • ☰ 7. Chapter 7: Loops
                                                                • ☰ 7.1. Why loops are useful
                                                                  • ☰ 7.2. for loops
                                                                    • ☰ 7.3. while loops
                                                                      • ☰ 7.4. Loop counters
                                                                        • ☰ 7.5. break and continue
                                                                          • ☰ 7.6. Loop exercises
                                                                          • ☰ 8. Chapter 8: Data Collections
                                                                            • ☰ 8.1. Lists
                                                                              • ☰ 8.2. Tuples
                                                                                • ☰ 8.3. Dictionaries
                                                                                  • ☰ 8.4. Sets (basics)
                                                                                    • ☰ 8.5. Choosing the right data structure
                                                                                    • ☰ 9. Chapter 9: Functions
                                                                                      • ☰ 9.1. Why functions matter
                                                                                        • ☰ 9.2. Defining functions
                                                                                          • ☰ 9.3. Parameters and arguments
                                                                                            • ☰ 9.4. Return values
                                                                                              • ☰ 9.5. Scope (local vs global)
                                                                                                • ☰ 9.6. Reusing code
                                                                                                • ☰ 10. Chapter 10: Working with Files
                                                                                                  • ☰ 10.1. What is a file?
                                                                                                    • ☰ 10.2. Reading text files
                                                                                                      • ☰ 10.3. Writing and appending files
                                                                                                        • ☰ 10.4. File paths
                                                                                                          • ☰ 10.5. Simple file-based programs
                                                                                                          • ☰ 11. Chapter 11: Errors and Debugging
                                                                                                            • ☰ 11.1. Types of errors
                                                                                                              • ☰ 11.2. Reading error messages
                                                                                                                • ☰ 11.3. Common beginner errors
                                                                                                                  • ☰ 11.4. try and except
                                                                                                                    • ☰ 11.5. Debugging strategies
                                                                                                                    • ☰ 12. Chapter 12: Using Python Libraries
                                                                                                                      • ☰ 12.1. What is a library?
                                                                                                                        • ☰ 12.2. Importing modules
                                                                                                                          • ☰ 12.3. Standard libraries
                                                                                                                            • ☰ 12.3.1. math
                                                                                                                              • ☰ 12.3.2. random
                                                                                                                                • ☰ 12.3.3. datetime
                                                                                                                                • ☰ 12.4. Installing external libraries with pip
                                                                                                                                • ☰ 13. Chapter 13: Object-Oriented Programming
                                                                                                                                  • ☰ 13.1. What is object-oriented programming?
                                                                                                                                    • ☰ 13.2. Classes and objects
                                                                                                                                      • ☰ 13.3. The `__init__` method
                                                                                                                                        • ☰ 13.4. Attributes and methods
                                                                                                                                          • ☰ 13.5. Encapsulation
                                                                                                                                            • ☰ 13.6. Inheritance (basics)
                                                                                                                                              • ☰ 13.7. Simple OOP examples
                                                                                                                                              • ☰ 14. Chapter 14: Automation with Python
                                                                                                                                                • ☰ 14.1. What is automation?
                                                                                                                                                  • ☰ 14.2. Automating simple tasks
                                                                                                                                                    • ☰ 14.3. Working with files and folders
                                                                                                                                                      • ☰ 14.4. Automating text processing
                                                                                                                                                        • ☰ 14.5. Scheduling scripts
                                                                                                                                                          • ☰ 14.6. Practical automation examples
                                                                                                                                                          • ☰ 15. Chapter 15: Web Development with Python
                                                                                                                                                            • ☰ 15.1. How the web works (basic overview)
                                                                                                                                                              • ☰ 15.2. Introduction to Flask
                                                                                                                                                                • ☰ 15.3. Creating a simple web server
                                                                                                                                                                  • ☰ 15.4. Routing and templates
                                                                                                                                                                    • ☰ 15.5. Handling user input (forms)
                                                                                                                                                                      • ☰ 15.6. Very basic APIs
                                                                                                                                                                      • ☰ 16. Chapter 16: Data Science with Python
                                                                                                                                                                        • ☰ 16.1. What is data science?
                                                                                                                                                                          • ☰ 16.2. Working with data
                                                                                                                                                                            • ☰ 16.3. Introduction to NumPy
                                                                                                                                                                              • ☰ 16.4. Introduction to pandas
                                                                                                                                                                                • ☰ 16.5. Data visualization with matplotlib
                                                                                                                                                                                  • ☰ 16.6. Simple data analysis examples
                                                                                                                                                                                  • ☰ 17. Chapter 17: Mini Projects
                                                                                                                                                                                    • ☰ 17.1. Number guessing game
                                                                                                                                                                                      • ☰ 17.2. Simple calculator
                                                                                                                                                                                        • ☰ 17.3. To-do list
                                                                                                                                                                                          • ☰ 17.4. Password generator
                                                                                                                                                                                            • ☰ 17.5. Quiz application
                                                                                                                                                                                              • ☰ 17.6. Small web or data project
                                                                                                                                                                                              • ☰ 18. Chapter 18: Next Steps
                                                                                                                                                                                                • ☰ 18.1. How to practice Python
                                                                                                                                                                                                  • ☰ 18.2. Writing clean and readable code
                                                                                                                                                                                                    • ☰ 18.3. Choosing a specialization
                                                                                                                                                                                                      • ☰ 18.3.1. Web development
                                                                                                                                                                                                        • ☰ 18.3.2. Data science
                                                                                                                                                                                                          • ☰ 18.3.3. Automation
                                                                                                                                                                                                            • ☰ 18.3.4. Software development
                                                                                                                                                                                                            • ☰ 18.4. Learning resources and communities
                                                                                                                                                                                                            • ☰ 19. Appendix
                                                                                                                                                                                                              • ☰ 19.1. Python syntax cheat sheet
                                                                                                                                                                                                                • ☰ 19.2. Common errors and solutions
                                                                                                                                                                                                                  • ☰ 19.3. Glossary of terms