Skip to main content

Python

Python is a high-level, general-purpose programming language known for its readability and versatility. This guide covers basic setup and usage.

Installation​

Most Linux distributions come with Python pre-installed. You can verify the installation by running:

python3 --version

If Python is not installed, you can install it using your system's package manager. For Debian-based systems, use:

sudo apt-get update
sudo apt-get install -y python3

Virtual Environments​

It is a best practice to use virtual environments to manage dependencies for different projects separately.

  1. Install the venv module if it's not already included:

    sudo apt-get install -y python3-venv
  2. Create a new virtual environment:

    python3 -m venv my-project-env
  3. Activate the environment:

    source my-project-env/bin/activate

    Your shell prompt will change to indicate that you are now in the virtual environment.

  4. To deactivate the environment, simply run:

    deactivate

Package Management with Pip​

Pip is the standard package installer for Python.

  • Install a package:

    pip install <package-name>
  • Install packages from a requirements file:

    pip install -r requirements.txt
  • Freeze dependencies: Create a requirements.txt file containing all the packages in the current environment.

    pip freeze > requirements.txt

"Hello, World!" Example​

  1. Create a file named hello.py with the following content:

    print("Hello, World!")
  2. Run the script from your terminal:

    python3 hello.py

    The output will be: Hello, World!.

References​