Learn Python: Running Python Scripts and The REPL
Python lets you write code in different ways. You can type quick commands in the Python REPL (Read-Evaluate-Print Loop), create scripts for later reuse, or even work with Jupyter Notebooks if you want an interactive approach. This lesson explains how to use each option in Visual Studio Code (VS Code).
What will be covered
• Entering the Python REPL and typing simple print statements.
• Writing scripts in VS Code and running them through a terminal window.
• Understanding when notebooks might be useful for exploration (optional for now).
• Deciding which approach suits different tasks you might have.
Why it’s important
The REPL is fast for quick tests. Scripts help you store and organize your work. Notebooks are often used for demonstrations or step-by-step experiments. Picking the right tool can save time, especially when using VS Code as your main environment.
Using the Python REPL
Type python in a terminal (or python3 on some systems) to enter the REPL. Each time you type a line with print("some text") and press Enter, it immediately shows the result. Exit by typing exit() or the key combination for your platform (Ctrl-D on macOS/Linux or Ctrl-Z+Enter on Windows).
Running a Script
A script is a file where you save your code. Create a file named example.py in VS Code:
print("I am learning Python")
print("It is fun to type new lines")
Open a terminal in VS Code (View > Terminal) and run:
python example.py
You will see your printed lines in the terminal window. Storing print statements in a script can be helpful if you want to keep them for future reference.
Using Jupyter Notebooks in VS Code
Jupyter Notebooks allow you to have code cells, run them one by one, and see the outputs immediately. They are popular for demonstration and detailed notes. If you install the Python and Jupyter extensions in VS Code, you can create a file with .ipynb as the extension. Then each cell can contain code:
- One cell might have print("Notebook example").
- Another cell might have print("Another cell here").
This is optional if you only want to learn the basics right now, but notebooks can be powerful for interactive learning.
Comparison of Approaches
These methods let you run Python in different ways:
Flow of Execution Options
This simple diagram shows how you might decide between methods:
Practice Exercises
- Open a REPL by typing python. Type print("REPL test one") and press Enter. Then type print("REPL test two"). Observe how each line appears right away.
- Make a file named greetings.py. In it, print two different lines, such as print("Hello, I am learning Python") and print("I can run scripts now"). Run the file in a terminal with python greetings.py.
- If you have installed Jupyter, create a notebook (.ipynb) and add two cells. One should have print("Cell one") and the other print("Cell two"). Run both cells and confirm you see two outputs.
Summary
You have explored three ways to execute Python code in VS Code: typing individual print statements in the REPL, writing a standalone script, or using a Jupyter Notebook. By mastering these options, you can tackle small tests quickly or build a collection of print statements that persist for later reference. This ensures you have a clear foundation for writing and running Python, preparing you for more advanced topics in subsequent lessons.