Skip to content

Python Tools ​

πŸ”§ Development Environment ​

Python Installation and Management ​

bash
# Check Python version
python --version
python3 --version

# Install Python (macOS with Homebrew)
brew install python

# Install Python (Ubuntu/Debian)
sudo apt update
sudo apt install python3 python3-pip

# Install Python (Windows)
# Download from python.org or use Microsoft Store

Virtual Environments ​

bash
# Create virtual environment
python -m venv myenv

# Activate virtual environment
# On macOS/Linux:
source myenv/bin/activate

# On Windows:
myenv\Scripts\activate

# Deactivate virtual environment
deactivate

# Using conda
conda create --name myenv python=3.9
conda activate myenv
conda deactivate

Package Management with pip ​

bash
# Install packages
pip install requests
pip install numpy pandas matplotlib

# Install specific version
pip install Django==3.2.0

# Install from requirements file
pip install -r requirements.txt

# List installed packages
pip list
pip freeze

# Create requirements file
pip freeze > requirements.txt

# Upgrade packages
pip install --upgrade package_name
pip install --upgrade pip

# Uninstall packages
pip uninstall package_name

πŸ“ Code Editors and IDEs ​

python
# VS Code - Lightweight, extensible
# - Python extension by Microsoft
# - Jupyter notebook support
# - Integrated terminal and debugging

# PyCharm - Full-featured IDE
# - Professional and Community editions
# - Excellent debugging and refactoring
# - Built-in version control

# Jupyter Notebook/Lab - Interactive development
# - Great for data science and prototyping
# - Supports markdown and visualizations
# - Can export to various formats

Essential VS Code Extensions ​

json
{
  "recommendations": [
    "ms-python.python",
    "ms-python.flake8",
    "ms-python.black-formatter",
    "ms-toolsai.jupyter",
    "ms-python.pylint",
    "ms-python.mypy-type-checker"
  ]
}

πŸ§ͺ Testing Tools ​

unittest (Built-in) ​

python
import unittest

class TestMathOperations(unittest.TestCase):
    def test_addition(self):
        self.assertEqual(2 + 2, 4)
    
    def test_division(self):
        self.assertEqual(10 / 2, 5)
        
    def test_division_by_zero(self):
        with self.assertRaises(ZeroDivisionError):
            10 / 0

if __name__ == '__main__':
    unittest.main()
python
# Install: pip install pytest

def add(a, b):
    return a + b

def test_add():
    assert add(2, 3) == 5
    assert add(-1, 1) == 0
    assert add(0, 0) == 0

def test_add_strings():
    assert add("hello", "world") == "helloworld"

# Run tests: pytest test_file.py

Test Coverage ​

bash
# Install coverage
pip install coverage

# Run tests with coverage
coverage run -m pytest
coverage report
coverage html  # Generate HTML report

🎨 Code Formatting and Linting ​

Black - Code Formatter ​

bash
# Install black
pip install black

# Format a file
black my_script.py

# Format entire directory
black .

# Check without formatting
black --check .

# Configuration in pyproject.toml
[tool.black]
line-length = 88
target-version = ['py39']

Flake8 - Linter ​

bash
# Install flake8
pip install flake8

# Lint a file
flake8 my_script.py

# Configuration in .flake8
[flake8]
max-line-length = 88
exclude = .git,__pycache__,venv
ignore = E203,W503

isort - Import Sorting ​

bash
# Install isort
pip install isort

# Sort imports in a file
isort my_script.py

# Configuration in pyproject.toml
[tool.isort]
profile = "black"
multi_line_output = 3

πŸ” Debugging Tools ​

Built-in Debugger (pdb) ​

python
import pdb

def problematic_function(x, y):
    pdb.set_trace()  # Breakpoint
    result = x / y
    return result

# Debug commands:
# n - next line
# s - step into
# c - continue
# l - list code
# p variable_name - print variable
# q - quit

IPython Debugger ​

python
# Install: pip install ipython

# In code:
from IPython import embed
embed()  # Drop into IPython shell

# Or use magic command in IPython/Jupyter:
%pdb on  # Enable automatic debugging on exceptions

Logging for Debugging ​

python
import logging

# Configure logging
logging.basicConfig(
    level=logging.DEBUG,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)

logger = logging.getLogger(__name__)

def my_function(x, y):
    logger.debug(f"Input values: x={x}, y={y}")
    result = x * y
    logger.info(f"Calculation result: {result}")
    return result

# Usage
result = my_function(5, 3)

πŸ“Š Performance Profiling ​

cProfile ​

python
import cProfile

def slow_function():
    total = 0
    for i in range(1000000):
        total += i ** 2
    return total

# Profile a function
cProfile.run('slow_function()')

# Profile a script
# python -m cProfile my_script.py

line_profiler ​

bash
# Install
pip install line_profiler

# Use @profile decorator
@profile
def slow_function():
    total = 0
    for i in range(1000000):
        total += i ** 2
    return total

# Run profiler
kernprof -l -v my_script.py

Memory Profiling ​

bash
# Install memory_profiler
pip install memory_profiler

# Use @profile decorator
@profile
def memory_intensive_function():
    big_list = [i for i in range(1000000)]
    return sum(big_list)

# Run memory profiler
python -m memory_profiler my_script.py

πŸ“¦ Package Distribution ​

Creating a Package ​

my_package/
β”œβ”€β”€ setup.py
β”œβ”€β”€ README.md
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ my_package/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ module1.py
β”‚   └── module2.py
└── tests/
    β”œβ”€β”€ __init__.py
    └── test_module1.py

setup.py ​

python
from setuptools import setup, find_packages

setup(
    name="my_package",
    version="0.1.0",
    author="Your Name",
    author_email="your.email@example.com",
    description="A short description of the package",
    long_description=open("README.md").read(),
    long_description_content_type="text/markdown",
    url="https://github.com/yourusername/my_package",
    packages=find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    python_requires=">=3.6",
    install_requires=[
        "requests>=2.25.0",
        "numpy>=1.20.0",
    ],
)

Building and Publishing ​

bash
# Install build tools
pip install build twine

# Build package
python -m build

# Upload to TestPyPI
twine upload --repository testpypi dist/*

# Upload to PyPI
twine upload dist/*

Web Development ​

python
# Flask - Lightweight web framework
from flask import Flask, jsonify

app = Flask(__name__)

@app.route('/api/users')
def get_users():
    return jsonify({'users': ['Alice', 'Bob']})

# Django - Full-featured web framework
# FastAPI - Modern, fast API framework
# Requests - HTTP library

Data Science ​

python
# NumPy - Numerical computing
import numpy as np
array = np.array([1, 2, 3, 4, 5])

# Pandas - Data manipulation
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

# Matplotlib - Plotting
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])

# Scikit-learn - Machine learning
from sklearn.linear_model import LinearRegression

GUI Development ​

python
# Tkinter - Built-in GUI toolkit
import tkinter as tk

root = tk.Tk()
root.title("My App")
label = tk.Label(root, text="Hello, World!")
label.pack()
root.mainloop()

# PyQt/PySide - Advanced GUI
# Kivy - Cross-platform GUI

πŸ”„ Version Control Integration ​

Git Integration ​

bash
# Initialize git repository
git init

# Create .gitignore for Python
echo "__pycache__/
*.pyc
*.pyo
*.pyd
.Python
venv/
.venv/
.env
.DS_Store" > .gitignore

# Commit changes
git add .
git commit -m "Initial commit"

Pre-commit Hooks ​

yaml
# .pre-commit-config.yaml
repos:
  - repo: https://github.com/psf/black
    rev: 22.3.0
    hooks:
      - id: black
  - repo: https://github.com/pycqa/flake8
    rev: 4.0.1
    hooks:
      - id: flake8
  - repo: https://github.com/pycqa/isort
    rev: 5.10.1
    hooks:
      - id: isort

🐳 Docker Integration ​

Dockerfile for Python ​

dockerfile
FROM python:3.9-slim

WORKDIR /app

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

CMD ["python", "app.py"]

Docker Compose ​

yaml
version: '3.8'

services:
  web:
    build: .
    ports:
      - "5000:5000"
    volumes:
      - .:/app
    environment:
      - FLASK_ENV=development

🎯 Best Practices ​

Project Structure ​

my_project/
β”œβ”€β”€ README.md
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ setup.py
β”œβ”€β”€ .gitignore
β”œβ”€β”€ .env.example
β”œβ”€β”€ src/
β”‚   └── my_project/
β”‚       β”œβ”€β”€ __init__.py
β”‚       β”œβ”€β”€ main.py
β”‚       └── utils.py
β”œβ”€β”€ tests/
β”‚   β”œβ”€β”€ __init__.py
β”‚   └── test_main.py
β”œβ”€β”€ docs/
β”‚   └── index.md
└── scripts/
    └── deploy.sh

Configuration Management ​

python
# config.py
import os
from dataclasses import dataclass

@dataclass
class Config:
    debug: bool = os.getenv('DEBUG', 'False').lower() == 'true'
    database_url: str = os.getenv('DATABASE_URL', 'sqlite:///app.db')
    secret_key: str = os.getenv('SECRET_KEY', 'dev-secret-key')
    
    @classmethod
    def from_env(cls):
        return cls()

# Usage
config = Config.from_env()

🎯 Key Takeaways ​

  • Virtual environments isolate project dependencies
  • pip is the standard package manager for Python
  • Testing is essential - use unittest or pytest
  • Code formatting tools like Black improve code quality
  • Linting with flake8 catches common errors
  • Debugging tools help identify and fix issues
  • Profiling helps optimize performance bottlenecks
  • Version control with Git is essential for collaboration
  • Docker enables consistent deployment environments
  • Good project structure makes code maintainable
  • Configuration management keeps settings organized

This completes the Python Fundamentals section! Continue to: NLP Fundamentals β†’

Released under the MIT License.