Writing and Running Python Scripts
Python is indeed an exciting and powerful language. It has the right combination of performance and features that make writing programs in Python both fun and easy.
From Terminal to Python
Python can be used through an interactive interpreter: a live environment where you enter a statement and see Python respond immediately. The first important skill is recognizing the transition from your operating system terminal into the Python interpreter. The two environments use different prompts and understand different kinds of instructions.
The operating system terminal is where you launch Python. The >>> prompt is the signal that Python is now ready to receive Python statements.
Recognizing the Two Prompts
When you open a terminal, you see a prompt belonging to the operating system. The source describes examples such as $ or C:\>. That prompt understands operating-system commands such as cd, ls, or dir. It does not understand Python code as Python code. To enter the Python environment, type python at the operating system prompt and press Enter. The prompt then changes to >>>, which belongs to Python.
Opening the Interpreter
- Open the operating system terminal so its command prompt is visible.
- Type python at the operating system prompt.
- Press Enter.
- Look for the >>> prompt.
- When >>> appears, enter Python statements at that prompt.
Launching Python
How do you move from the operating system terminal into the Python interpreter?
Start at the terminal: The operating system prompt is waiting for an instruction.
Enter python: Type the word python at the operating system prompt and press Enter.
Check the new prompt: The prompt changes to >>>. This indicates that the Python interpreter is ready.
You are now inside the Python interpreter and can enter Python statements directly.
The Immediate Feedback Cycle
At the >>> prompt, type a Python statement and press Enter. Python immediately processes that statement and displays the result when there is a result to display. The >>> prompt then appears again. This means you can enter another statement without first creating a complete program or waiting for a separate run step.
REPL means Read-Eval-Print Loop. The name describes the repeating sequence: Python reads your statement, evaluates it, prints the result, and loops back to the prompt.
Entering a Statement
This source example shows the complete feedback cycle. The statement is entered after >>>. After Enter is pressed, Python displays Hello World on the next line. The >>> prompt then reappears, showing that the interpreter is ready for another statement.
Returning to the Terminal
The REPL continues accepting statements until you exit the interpreter. When you finish experimenting, type exit() at the >>> prompt and press Enter. Python closes, and the operating system terminal prompt appears again. You can then use operating-system commands or launch Python again.
Mistakes with Prompts
Typing Python code at the operating system prompt
The operating system prompt understands operating-system commands, not Python code.
Fix:
Type python first, press Enter, and wait for >>> before entering Python statements.Assuming the command python has already opened Python
The important signal is the prompt change to >>>.
Fix:
Confirm that >>> appears before entering Python code.Expecting the prompt to disappear after one statement
The REPL loops back to the Python prompt after each statement.
Fix:
Treat the returning >>> as an invitation to enter the next statement.Forgetting how to return to the operating system terminal
The interpreter remains active until you exit it.
Fix:
Type exit() at >>> and press Enter.
Practice the Transition
Imagine that your terminal currently shows an operating-system prompt. Describe the sequence you would use to enter Python, identify the signal that confirms the transition, enter one Python statement, and return to the operating-system terminal.
Hints
- Begin with the command typed at the operating-system prompt.
- Name the exact prompt that signals Python is ready.
- Include what happens after you press Enter on a Python statement.
- Finish with the command used to leave the interpreter.
What do you think happens?
After Python displays the result of a statement in the interpreter, what should you expect to see next?
Reveal answer
Answer: The >>> prompt
The REPL loops back to the Python prompt after reading, evaluating, and printing a statement, so another statement can be entered immediately.
Key Takeaways
- The operating-system terminal uses a prompt such as $ or C:\>, while the Python interpreter uses >>>.
- Type python at the operating-system prompt and press Enter to open the Python interpreter.
- At >>>, Python statements are processed immediately.
- The REPL means Read-Eval-Print Loop: read, evaluate, print, and return to the prompt.
- Type exit() at >>> to close Python and return to the operating-system terminal.