3  Writing Python Code

To program in Python, you need an IDE or code editor. These are applications that provide tools to help you write and test your code productively (otherwise, you would be writing code in a text editor and running it in the terminal).

3.1 Tools for Writing Python Code

There are many tools available for writing Python code. Evaluate what works best for you! In this course, we recommend using the following tools:

Learn More

You can read more about Python IDEs and code editors in the following links:

Also, if you may want to try other IDEs, such as PyCharm (free student version) or Spyder.

3.2 Why Visual Studio Code (VS Code)?

VS Code is a popular code editor that is widely used by developers for writing code in various programming languages, including Python. Here are some reasons why you might consider using VS Code:

  • Lightweight and Powerful. Provides a balance between performance and functionality.
  • Rich Ecosystem. Extensive library of extensions and customization options.
  • Built-In Git Integration. Streamlines version control.

To install VS Code and set it up for Python programming, follow these steps:

  1. Download VS Code.
  2. Run the installer and follow the instructions.
  3. Launch VS Code.

Then, install the following extensions:

  • Python. Provides Python language support and enables you to run Python code directly from VS Code.
  • Jupyter Notebooks. Allows you to create and run Jupyter Notebooks within VS Code (watch a tutorial).
  • GitHub Copilot. Provides AI-powered code suggestions and completions (watch a tutorial). You will need a GitHub account to use this extension.
  • Theme Dracula Official (Optional). Changes the appearance of the editor to a dark theme.

3.3 Executing Python Code

In the following, you will run Python code using different methods. Each method is suitable for different scenarios.

3.3.1 Interactive Python Interpreter

To run Python code in the interactive Python interpreter:

  1. Open a terminal:
  • Windows Terminal: Press Win + R, type cmd, and press Enter.
  • VS Code Terminal: Press Ctrl + ~ or View -> Terminal.
  1. Type python and press Enter. You should see the Python version information, as shown below:
Python 3.13.0 (tags/v3.13.0:60403a5, Oct  7 2024, 09:38:07) [MSC v.1941 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 
  1. Enter print("Hello World!") and press Enter. You should see the output Hello World!.
  2. To exit the Python interpreter, type exit() and press Enter.

3.3.2 Python Script (extension .py)

To run a Python script in VS Code:

  1. Create a new project folder (File -> Open Folder).
  2. Create a hello.py file (File -> New File -> Python File).
  3. Enter print("Hello World!") and save.
  4. Run the file:
  • Option 1. Graphical Interface. Press “Run Python File” (look for a ‘play’ icon, in the top-right corner of the editor).
  • Option 2. Terminal
    • Open a terminal (Ctrl + ~ or View -> Terminal).
    • Navigate to the directory where your Python script is located. By default, the terminal opens in the root directory of the project.
    • Run the script by typing python <filename>.py (<filename> = hello.py).
  1. Check result on the terminal (you should see Hello World!).
What Is the Root Directory?

The root directory is the top-level directory of a project. It is the directory that contains all the files and subdirectories of the project. When you open a folder in VS Code, the root directory is the folder you selected.

3.3.3 Jupyter Notebooks (extension .ipynb)

To run code in Jupyter Notebooks using VS Code:

  1. Create a hello.ipynb file (File -> New File -> Jupyter Notebook).
  2. Add a code cell (+ Code Cell button).
  3. Enter print("Hello World!") in the code cell.
  4. Run the cell:
  • Press Shift + Enter.
  • Press Execute Cell (look for a ‘play’ icon at the left of the cell).
  • The code within that cell will execute, and the output will appear directly below the cell.

To add text to a Jupyter Notebook:

  1. Add a markdown cell (+ Markdown button)
  2. Add text using the Markdown syntax. For example, # Hello World!.
  3. Render the formatted text:
  • Press Shift + Enter or Esc.
  • Press Stop Editing the Cell (look for a ‘checkmark’ icon at the right of the cell).
  1. The text will be rendered below the cell.
The .ipynb Extension

The .ipynb extension stands for Interactive Python Notebook, IPython Notebook, or Jupyter Notebook. It is a file format that allows you to combine code execution, rich text, mathematics, plots, and rich media.

3.3.4 Executing Code Cells in .py Files

In VS Code, you can define Jupyter-like code cells within Python code (.py) files (see details). This feature allows you to write and test code in a more interactive way, similar to Jupyter Notebooks. You can define code cells using a # %% comment and markdown cells using a # %%[markdown] comment. For example, in a .py file, the code below:

# %%[markdown]
# This is a markdown cell.

# %%
print("Hello World!")

Would produce the output presented in Figure 3.1. By cliking on the Run Cell button, you can execute the code piecemeal, as you would in a Jupyter Notebook.

Figure 3.1: Example of code cells in a .py file. Upon running the code cells, the output is displayed in a separate window.