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.
-
Install the
venvmodule if it's not already included:sudo apt-get install -y python3-venv -
Create a new virtual environment:
python3 -m venv my-project-env -
Activate the environment:
source my-project-env/bin/activateYour shell prompt will change to indicate that you are now in the virtual environment.
-
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.txtfile containing all the packages in the current environment.pip freeze > requirements.txt
"Hello, World!" Exampleβ
-
Create a file named
hello.pywith the following content:print("Hello, World!") -
Run the script from your terminal:
python3 hello.pyThe output will be:
Hello, World!.