1.5 Running Python Code (REPL, Scripts, and IDEs)

Once Python is installed, you can start writing and executing Python code in various ways depending on your workflow and the nature of your project. This section will cover the three main ways to run Python code: using the REPL (Read-Eval-Print Loop), executing Python scripts, and working within IDEs (Integrated Development Environments).


1.5.1 Running Python in the REPL (Interactive Mode)

The REPL (Read-Eval-Print Loop) is an interactive Python shell that lets you write Python code line by line and see immediate results. It's useful for testing small code snippets, experimenting with Python functions, or debugging.

1.5.1.1 Starting the Python REPL

    • On Windows, open the command prompt (press Win + R, type cmd, and press Enter).
    • On macOS/Linux, open the terminal.

To start the REPL:Then, type the following command:

python

or, depending on your system:

python3

This opens the Python interpreter, and you will see a prompt similar to:

Python 3.x.x (default, ...) 
Type "help", "copyright", "credits" or "license" for more information.
>>>

1.5.1.2 Using the REPL

At the >>> prompt, you can now enter Python commands, and the interpreter will immediately execute them. For example:

>>> print("Hello, World!")
Hello, World!

You can also perform basic calculations:

>>> 3 + 5
8

The REPL is especially useful for:

  • Quick testing: Test small snippets of code.
  • Learning: Beginners can experiment with Python syntax and get immediate feedback.
  • Debugging: Quickly test ideas and debug parts of your code.

1.5.1.3 Exiting the REPL

To exit the Python REPL, you can either:

  • Type exit() or quit(), and press Enter.
  • Press Ctrl + D on macOS/Linux or Ctrl + Z and then Enter on Windows.

1.5.2 Running Python Scripts

A Python script is a file containing Python code that you can save and run at any time. This is ideal for larger programs or projects where you want to save your code for future use or share it with others.

1.5.2.1 Writing a Python Script

To create a Python script:

  1. Open a text editor (e.g., Notepad on Windows, TextEdit on macOS, or any code editor like Visual Studio Code).
  2. Save the file with the .py extension, for example, my_script.py.

Write your Python code. For example:

# my_script.py
print("This is a Python script!")

1.5.2.2 Running the Python Script from the Command Line

To run the script:

  1. Open the command prompt (Windows) or terminal (macOS/Linux).

Run the script by typing:

python my_script.py

or

python3 my_script.py

The output should appear in the terminal, for example:

This is a Python script!

Navigate to the directory where your script is saved using the cd command. For example:

cd path_to_your_script_directory

1.5.2.3 Passing Command-Line Arguments to Scripts

You can pass arguments to your script when running it from the command line using the sys module.

Example script (my_script.py):

import sys

print("First argument:", sys.argv[1])
print("Second argument:", sys.argv[2])

Run the script with arguments:

python my_script.py argument1 argument2

Output:

First argument: argument1
Second argument: argument2

The sys.argv list stores the arguments passed to the script. The first element (sys.argv[0]) is the script name, and subsequent elements are the arguments.


1.5.3 Running Python Code in IDEs (Integrated Development Environments)

An Integrated Development Environment (IDE) provides a more feature-rich environment for writing, running, and debugging Python code. IDEs are particularly useful for larger projects or when you need advanced features like syntax highlighting, code suggestions, and built-in debugging tools.

1.5.3.1 Common Python IDEs

Here are some of the most popular Python IDEs:

  • Visual Studio Code (VS Code):
    • Lightweight and highly customizable with Python extensions.
    • Features include IntelliSense (code completion), debugging tools, and version control integration.
  • PyCharm:
    • A powerful IDE designed specifically for Python development.
    • Includes advanced features like code navigation, debugging, and testing integration.
  • Jupyter Notebook:
    • A web-based environment for interactive Python coding, primarily used for data science, machine learning, and scientific computing.

1.5.3.2 Running Python Code in Visual Studio Code (VS Code)

  1. Set up Python in VS Code:
    • Install the Python extension from the VS Code marketplace.
    • Select the Python interpreter by pressing Ctrl + Shift + P and typing "Python: Select Interpreter".
  2. Writing and Running Code:
    • Create a new file (e.g., my_script.py).
  3. Run the Code:
    • Click the "Run" button at the top-right or press Ctrl + F5 to run the script in the terminal inside VS Code.

Write your Python code:

print("Hello from VS Code!")

1.5.3.3 Running Python Code in PyCharm

  1. Set up a Project:
    • After installing PyCharm, create a new Python project.
    • Set the project interpreter (the Python installation or virtual environment you want to use).
  2. Writing and Running Code:
    • Create a new Python file (e.g., my_script.py).
  3. Run the Code:
    • Right-click the file and choose Run or press Shift + F10 to run the script.

Write your Python code:

print("Hello from PyCharm!")

1.5.3.4 Running Python Code in Jupyter Notebook

  1. Set up Jupyter Notebook:
  2. Start Jupyter:
    • This will open a web interface in your browser.
  3. Creating and Running Code:
    • Create a new notebook by selecting New > Python 3.
  4. Run the Code:
    • Press Shift + Enter to execute the cell and see the output directly below.

Write Python code in the cells:

print("Hello from Jupyter Notebook!")

In the terminal, type:

jupyter notebook

Install Jupyter with:

pip install notebook

1.5.4 Conclusion

There are multiple ways to run Python code, depending on your needs:

  • The REPL is great for experimenting with small pieces of code or testing functions interactively.
  • Python scripts are used for running saved programs and projects, especially when you want to reuse or distribute your code.
  • IDEs provide a comprehensive environment for larger projects, offering features like debugging, testing, and integrated tools to enhance productivity.

Depending on the scale of your project and your personal preferences, you can choose any of these methods to run Python code effectively.