How to… Python

Introduction

Python is a programming language designed for its simple readability and ease of use.

Python is primarily executed as an interpreted language. This means that when you run your Python code, the Python Interpreter (which you get when you install Python) translates the code line by line into machine code that the computer understands at runtime (while the program is running).

What you install: Installing Python primarily downloads the Python Interpreter – the “translator” that makes your scripts work.

Using Python

You can use Python in two main ways: ad-hoc in your terminal or as a script in a file.

Interactively in the Terminal

The terminal (also called Shell, PowerShell, or Command Line) is a text-based interface to your computer.

By running python3 (or just python on some systems), you start the interactive interpreter and can directly enter and immediately execute Python commands: Bash

python3

To exit the Python interpreter and return to the regular terminal, use the quit() function: Python

quit()

As a Python Script (File)

If you want to save code and run it repeatedly, you write it into a Python file (file extension .py). Python code is plain text, so you can create and edit it with any text editor.

To run a script, you enter the interpreter command followed by the path to your file in the terminal:

# my_script.py

for i in range(1, 4):
    print(i)
1
2
3
python3 path/to/my_script.py

Integrated Development Environment (IDE)

For faster and more efficient coding, you should use an Integrated Development Environment (IDE).

An IDE is a special application that makes writing code easier – comparable to the difference between a simple text editor and a program like Microsoft Word. It offers features such as:

  • Syntax Highlighting: Colors to better distinguish code elements.
  • Auto-completion: Suggestions for commands and variable names.
  • Debugging Tools: Tools for finding and fixing errors.

There are many excellent IDEs available. Choose one you like, for example: VS Code, Spyder, or PyCharm. Essentially, they all serve the same purpose with different extra features.