Working with the Python Shell
$ is the prompt of the shell. It will be different for you depending on the settings of the operating system on your computer, hence I will indicate the prompt by just the $ symbol.
What Is the Python Shell?
The Python shell is an interactive environment where you can type Python commands one at a time and see the results immediately. Unlike writing a Python script in a file (which you run all at once), the shell lets you experiment, test ideas, and get instant feedback. When you open your terminal or command prompt and start Python, you enter this interactive mode. The shell is waiting for your input, and it will execute whatever you type as soon as you press Enter.
The shell is a conversation between you and Python. You type a command, Python responds, and you can type another command. This makes it perfect for learning, debugging, and quick calculations.
Understanding the Shell Prompt
The shell prompt is a symbol or text that appears on your screen to show that the shell is ready for you to type a command. In this course, we use the dollar sign $ to represent the shell prompt in examples. However, your actual prompt will likely look different depending on your operating system and how your system is configured. On some systems you might see a greater-than sign >, a hash #, or other characters. The exact appearance does not matter—what matters is that you recognize it as the signal that the shell is waiting for your input.
Entering the Python Shell
To start the Python shell, open your terminal (on GNU/Linux or macOS) or command prompt (on Windows), and type the command python or python3, depending on your system. After you press Enter, Python will start, and you will see a new prompt—the Python interpreter prompt. This is different from the shell prompt. The Python prompt typically looks like >>> and indicates that you are now inside Python, ready to type Python code.
The Python interpreter is now active and waiting for your input.Notice the difference: the shell prompt ($) is where you start, and the Python prompt (>>>) is where you end up after typing python3. The >>> prompt means you are inside the Python interpreter.
Distinguishing Prompt from Input
When you see examples of shell or Python code, it is crucial to understand which part is the prompt and which part is what you actually type. The prompt is printed by the system—you do not type it. You only type the command that comes after the prompt. For example, if you see $ python3, the $ is the prompt (printed by your system), and python3 is what you type. Similarly, if you see >>> print('Hello'), the >>> is the prompt (printed by Python), and print('Hello') is what you type.
When reading examples in documentation or tutorials, always look for the prompt symbol first. Everything that comes after the prompt on the same line is your input. The next line, if it does not start with a prompt, is usually the output from your command.
Exiting the Python Shell
When you are finished using the Python shell, you need to exit the interpreter to return to the regular shell prompt. There are two main ways to do this, and the method depends on your operating system.
- On GNU/Linux or macOS: Press Ctrl+D (hold the Control key and press D) to exit immediately.
- On any operating system: Type exit() (including the parentheses) and press Enter to exit the interpreter.
You are back at the shell prompt ($) and have exited Python.Common Mistakes When Using the Shell
Typing the prompt symbol as part of your command
The $ is printed by your system, not something you type. Including it in your input will cause an error because the shell will try to execute a command called '$'.
Fix:
Type only the command itself: python3Forgetting parentheses when exiting Python
exit is a function in Python, and functions must be called with parentheses. Without them, Python will just show you information about the exit function but will not actually exit.
Fix:
Always type exit() with parentheses to properly exit the interpreter.Confusing the shell prompt with the Python prompt
The $ prompt means you are in the shell, not in Python. You must first type python3 to enter the Python interpreter, where you will see the >>> prompt.
Fix:
Type python3 at the $ prompt to enter Python, then type your Python code at the >>> prompt.Trying to use Ctrl+D on Windows
Ctrl+D is a Unix/Linux/macOS shortcut. Windows does not recognize it for exiting Python.
Fix:
On Windows, use exit() instead, or try Ctrl+Z followed by Enter.
Why the Shell Matters
The Python shell is one of the most powerful tools for learning and debugging. When you encounter an error in a script, you can use the shell to test small pieces of code in isolation. For example, if a line of code is not working as expected, you can type it into the shell and see exactly what happens. This immediate feedback helps you understand what went wrong and how to fix it. Professional programmers use the shell constantly to explore libraries, test ideas, and verify that code behaves the way they expect before adding it to a larger program.
The shell is not just for beginners. It is a fundamental tool that programmers of all levels use daily. Becoming comfortable with the shell early will make you a more effective and confident programmer.
Practice
Open your terminal or command prompt and start the Python shell. Once inside the shell (you should see the >>> prompt), type a simple command like 2 + 2 and press Enter. Observe the result. Then, type exit() and press Enter to return to the shell prompt. What did you observe at each step?
Hints
- Remember that you do not type the $ or >>> symbols—those are printed by your system.
- Make sure you type exit() with parentheses, not just exit.
- After typing exit(), you should see the $ prompt again, indicating you are back in the shell.
Key Takeaways
- The Python shell is an interactive environment where you type commands and see results immediately, making it ideal for learning and testing code.
- The shell prompt (such as $) indicates that the system is ready for input; you do not type the prompt itself, only the command that follows it.
- The Python interpreter prompt (>>>) appears after you type python3 or python, and it indicates you are inside Python and ready to type Python code.
- Exit the Python interpreter by typing exit() with parentheses, or by pressing Ctrl+D on GNU/Linux and macOS.
- The shell prompt varies by operating system, but the core concept remains the same: it signals that the system is waiting for your input.