Python Print and Browser Request Layers
Python interpreter execution
How Python Runs Your Code
You've learned how to make Python talk using the print() function. Now, let's look at the two main ways to give Python your instructions: interactively, one command at a time, or all at once with a script file.
In Python, you have two main ways to run your code: interactive mode and script mode.
Think of it like talking to a person. You can have a back-and-forth conversation (interactive mode) or hand them a complete set of written instructions to follow (script mode). Both methods use the same tool, the Python interpreter, but in slightly different ways.
The Interactive Loop
Interactive mode is often called the REPL. It's a handy acronym that perfectly describes what the interpreter does.
REPL stands for Read, Evaluate, Print, Loop.
Here’s how that four-step dance works:
- Read: Python reads the single line of code you just typed.
- Evaluate: It figures out what your code means and executes the instruction.
- Print: It prints the result of your command back to the screen.
- Loop: It waits for you to type the next command, starting the cycle all over again.
This immediate feedback is great for testing small ideas, doing quick calculations, or exploring how a function works without having to create a file.
>>> 5 + 3
8
>>> print("Hello, interactive world!")
Hello, interactive world!
>>> name = "Alice"
>>> name
'Alice'
Notice that when you just type 5 + 3 or name, the REPL automatically prints the result (8 and 'Alice'). This is a special feature of interactive mode. It shows you the value of any expression you evaluate.
Running from a Script
For anything more complex than a few lines, you'll want to save your code in a file. A Python file is just a plain text file that ends with the .py extension. This is called a script.
When you run a script, the interpreter’s process is different from the REPL. Instead of a loop, it’s a straight shot:
- It opens and reads the entire file from top to bottom.
- It executes the instructions in the order they appear.
- It exits when it reaches the end of the file.
Unlike the REPL, the interpreter won't automatically print the result of an expression in a script. If you want to see the value of 5 + 3 or the variable name, you must explicitly use the print() function.
# Save this code in a file named my_script.py
print(5 + 3)
name = "Alice"
print(name)
To run this, you'd open your computer's terminal or command prompt, navigate to where you saved the file, and type python my_script.py. The output would be 8 and Alice, each on a new line.
Comparing the Two Modes
Choosing between interactive mode and script mode depends entirely on what you're trying to accomplish.
| Feature | Interactive Mode (REPL) | Script Mode |
|---|---|---|
| Execution | One line at a time | Entire file at once, top to bottom |
| Best For | Quick tests, exploring, simple math | Building programs, reusable code |
| Code Persistence | Code is lost when you close the session | Code is saved permanently in a .py file |
| Output | Automatically prints expression results | Only prints what you specify with print() |
Most development happens in script files, but programmers use the REPL constantly as a scratchpad for trying things out. Getting comfortable with both will make you a more efficient coder.
What is the primary difference between running Python in interactive mode versus running a script file?
The acronym REPL describes the interactive mode's process. What does REPL stand for?
