This module includes:


1. Introduction

Python is a widely used, general-purpose programming language that is easy to learn, read, and write.

  • Popular among researchers and developers for its simplicity and readability
  • Used by major deep learning frameworks (e.g., PyTorch)
  • Supported by an active open-source community and a vast ecosystem of libraries

▷ How Python works

Python is an interpreted language. Here’s a simplified view of what happens when you run Python code:

  1. Your code (saved as .py) is first interpreted into bytecode (e.g., .pyc files).
  2. This bytecode is then executed by a Python Virtual Machine (VM).
  3. Most Python implementations (like CPython) are written in C, which ultimately translates instructions into machine code.

▷ Python is strongly typed

Python keeps track of the type of each variable and does not automatically convert between types unless explicitly told to.

  • The interpreter respects types and will raise errors if you use variables in incompatible ways.
  • You can read more about variable types here.

▷ Tools to run Python code

Before jumping into text processing, let’s warm up using the Python interpreter. While Python comes with a basic interface called IDLE, you might prefer more powerful tools:

IDE: Visual Studio Code (VSCode)
  • Lightweight but powerful code editor
  • Supports Python through extensions
  • Integrated terminal, code linting, debugging, and version control
Notebook environment: Google Colab
  • Browser-based, no installation needed
  • Built on Jupyter notebooks (interactive)
  • Comes pre-loaded with many popular Python libraries
  • Supports GPU acceleration
  • Easily integrates with Google Drive

2. Installation: Python3

  1. Download installer
    • Go to https://www.python.org/downloads/
    • Click “Download Python 3.x.x” for your OS.
  2. Run installer
    • Windows:
      • Double-click the .exe
      • Check “Add Python to PATH”
      • Click Install Now
    • macOS:
      • Open the downloaded .pkg
      • Follow the on-screen prompts
  3. Verify installation
    python3 --version
    python3 -v
    python3 -vv
    

▷ Install Visual Studio Code

  1. Download VS Code

  2. Run installer

    • Windows: launch the .exe and follow defaults
    • macOS: drag Visual Studio Code.app to /Applications
  3. Open VS Code

    • Launch the app from your Start Menu / Applications folder / launcher.
  4. Install Python extension

    • Press Ctrl+Shift+X (Cmd+Shift+X on macOS)
    • Search for Python (by Microsoft)
    • Click Install
  5. Verify in integrated terminal

    • Open the built-in terminal (Ctrl+`)
    • Run:

      python3 --version
      

3. Three ways to run code

▷ Example: Python as a Calculator

You’ll see how Python can serve as a quick interactive calculator for arithmetic operations.

>>> 1 + 5 * 2 - 3
8
>>> (2 + 3) * 4
20
>>> 1.0 / 3.0
0.3333333333333333

You can execute these operations in three ways:

  1. Interactive terminal

    $ python3
    >>> 1 + 5 * 2 - 3
    8
    
  2. Script file

    # calculator.py
    print(1 + 5 * 2 - 3)
    $ python3 calculator.py
    # → Hello, World!
    
  3. IDE or Notebook

    • a VSCode Python file with the built-in terminal or Python extension
    • a Colab notebook cell

4. Environment management

Managing your Python environment is important, especially when working on multiple projects with different requirements.

Problem Solution
Multiple versions of Python may be needed Create isolated environments with different Python versions
Countless Python packages and dependencies Manage dependencies within separate environments
Different projects require different (even conflicting) package versions Use virtual environments to keep project-specific packages and versions
Version conflicts can break code Avoid conflicts by isolating environments per project
Hard to share or reproduce setups Virtual environments make it easy to replicate and share environments

▷ Solution 1: venv

  • venv is the built-in tool in Python for creating virtual environments (Python Docs: venv).
python -m venv myenv
  • This creates a new virtual environment in a folder named myenv.
  • That folder is the environment — you can choose any name you like.
  • The environment is based on your current Python installation (called the “base”).
  • The created directory includes:

    • A Python interpreter
    • Standard libraries
    • Scripts and binaries specific to that environment
  • Completely isolated from global or system-installed packages

To activate the virtual environment (on macOS/Linux), run the following from the directory where the environment was created:

  source myenv/bin/activate

To deactivate the environment:

   deactivate

▷ Solution 2: Anaconda / Miniconda

Conda is a widely used package and environment manager.

  • Manages both Python and non-Python dependencies
  • Can create and manage isolated environments

To create a new environment:

conda create -n myenv
conda create -n myenv python=3.10

To activate/deactivate the environment:

conda activate myenv
conda deactivate

To export environment:

conda activate myenv
$ conda env export > environment.yml

▷ Installing packagges

Using conda:

conda install -n myenv package_name
# Optional: specify version
conda install -n myenv package_name=1.2.3

Using pip in a Conda environment:

Sometimes a package isn’t available via conda. In that case, activate your conda environment and use:

pip install package_name

Tip: Install as many packages as possible using conda first. Use pip only for packages not available through conda, to avoid dependency issues.