How to Execute Python Code: Interactive REPL, Script Mode & VS Code

Python offers multiple flexible ways to write and execute code depending on your goals. When testing a quick math calculation or exploring a new library function, you can use the Interactive REPL Mode. When developing complete software applications, college lab assignments, or web scrapers, you write your code into a saved text file and run it in Script Mode. In this lesson, we cover all primary execution methods.


Method 1: Interactive REPL Mode (Read-Eval-Print-Loop)

To launch the interactive Python shell, open Command Prompt, PowerShell, or Terminal, and simply type python and press Enter. You will be greeted by the triple chevron prompt (>>>):

# Launch interactive shell:
$ python
Python 3.12.2 (tags/v3.12.2:6abddd9, Feb  6 2024, 21:26:36) [MSC v.1937 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 45 * 8
360
>>> message = "Veewom Python Tutorial"
>>> message.upper()
'VEEWOM PYTHON TUTORIAL'
>>> exit()

When to use Interactive Mode: For rapid prototyping, debugging short algorithms, inspecting object attributes using dir(object), or reading documentation via help(str).


Method 2: Script Mode (Creating and Running .py Files)

Script mode is how all professional software is built. You write your Python instructions into a text file saved with the .py extension, which the Python interpreter reads and executes sequentially from top to bottom.

Step-by-Step Guide:

• Step 1: Open any text editor (Notepad, VS Code, Sublime Text).

• Step 2: Type the following code:

# File: student_report.py
# Purpose: Calculate total marks and pass/fail status

name = "Aakash Patel"
roll_number = 2045
marks = [78, 85, 92, 69, 88]

total = sum(marks)
average = total / len(marks)
is_passed = average >= 40

print(f"--- Student Grade Card ---")
print(f"Roll No: {roll_number} | Name: {name}")
print(f"Total Marks: {total} / 500")
print(f"Average: {average:.1f}%")
print(f"Status: {'PASSED' if is_passed else 'FAILED'}")

• Step 3: Save the file: Save it as student_report.py in a convenient folder, e.g. C:\PythonProjects\.

• Step 4: Execute in Terminal: Open CMD or PowerShell, navigate to that directory, and run:

cd C:\PythonProjects
python student_report.py

Expected Output:

--- Student Grade Card ---
Roll No: 2045 | Name: Aakash Patel
Total Marks: 412 / 500
Average: 82.4%
Status: PASSED

Method 3: Executing in Visual Studio Code (VS Code)

VS Code is the world's most popular code editor for Python development:

• 1. Install Python Extension: In VS Code, click Extensions (Ctrl + Shift + X), search for Python (by Microsoft), and click Install.

• 2. Open Folder: Go to File > Open Folder and choose your project folder.

• 3. One-Click Run: Open any .py file, and click the Play button (▸) located in the top-right corner of the editor. VS Code automatically opens an integrated terminal and executes your script.


University Exam & Viva Questions

Q1 (BCA Exam Question): Explain the internal execution steps that occur when running a Python script.
Answer: (1) The CPython compiler parses the .py source file and checks for syntax errors. (2) It compiles source code into intermediate bytecode instructions saved inside the __pycache__ directory as .pyc files. (3) The Python Virtual Machine (PVM) reads and interprets the bytecode, converting it into native machine CPU instructions.

Q2: How do you exit the Python interactive shell?
Answer: You can type exit() or quit(), or press Ctrl + Z followed by Enter on Windows, or Ctrl + D on macOS/Linux.


Frequently Asked Questions (FAQ)

What is a Shebang line (#!) in Python?

On Linux and macOS, the very first line of a script can specify the interpreter path, e.g., #!/usr/bin/env python3. This allows executing the script directly as an executable program (./script.py) without prefixing the command with python3.

What is the __pycache__ folder created next to my script?

The __pycache__ folder contains pre-compiled Python bytecode (.pyc files). When you re-run an imported module that has not changed, Python skips re-compilation and loads the cached bytecode directly, accelerating program startup speed.
Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!