L
LLLOS.ai
Learn
L

Chapter 4 — Python Libraries

Class 12 · Computer Science

Overview

Chapter 4 — Python Libraries Master Diagram

This chapter introduces Python libraries — collections of prewritten code (modules and packages) that extend Python's functionality. It explains why libraries are essential for code reuse, faster development, modular design and accessing tested implementations for common tasks (mathematics, file handling, data processing, dates/times, regular expressions, etc.). Key themes include the Python Standard Library, importing modules and specific members, using third‑party packages, package installation (pip), reading documentation, and writing/organizing your own modules. Students will learn how to import and use built‑in modules (for example math, random, datetime, os, sys, json, csv, re), how to handle common data formats, how to install and use external libraries, and best practices for modular programming and code reuse.

Learning Objectives

  • Define module, package and library and state their roles in program modularity
  • Distinguish between a module and a package and give examples from the Python standard library
  • Demonstrate different import styles (import, from … import, import … as) and explain their effects on namespace
  • Apply functions from math, random, statistics and datetime libraries to solve typical exam problems (e.g., factorial, generating random values, mean/median, date differences)
  • Use os and shutil modules to perform file and directory operations (list, create, rename, remove) through code snippets
  • Use json and csv modules to read, parse and write JSON and CSV data in programs
  • Employ the re module to perform pattern matching and validate data (e.g., email, phone number patterns)
  • Create and import a custom module and a simple package (including __init__.py) and demonstrate reuse of functions

Topics in this chapter

15 topics · tap a topic title to jump straight to it.

💻1

Introduction to Python Libraries

💻 COMPUTER SCIENCE / IT

Introduction to Python Libraries

Key Point: Mean (arithmetic): μ = (1/n) * Σ (x_i) — Python: statistics.mean(data) or np.mean(arr)

What is a Python library?
A Python library is a collection of prewritten code—modules and packages—that provides functions, classes and utilities to perform common tasks without rewriting code from scratch. Libraries speed development, improve reliability and let you use well-tested solutions.

Types of libraries

  • Standard library: shipped with Python (examples: math, datetime, json).
  • Third-party libraries: installed via package managers like pip (examples: numpy, pandas, matplotlib, requests).
  • Domain-specific libraries: for web, data science, machine learning, etc. (examples: flask, scipy, sklearn).

How to install and import
Install with pip: pip install package_name (use virtual environments for projects). Import in code:

# import whole module
import math
# import with alias
import numpy as np
# import specific items
from math import sqrt, pi

Namespaces and aliasing
Using aliases (for example import numpy as np) keeps code readable and avoids long names. Modules create namespaces; use module.name to access items unless imported directly.

Why use libraries?

  • Save time: reuse existing, tested code.
  • Reliability: community-tested implementations.
  • Performance: many libraries (e.g., NumPy) use optimized C code for speed.
  • Readability and maintainability: high-level functions express intent clearly.

Simple examples (code)

# math: area of circle
from math import pi
def circle_area(r):
    return pi * r * r

# numpy: vectorized operations
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print('dot product =', np.dot(a, b))

# pandas: read CSV and show first rows
import pandas as pd
# df = pd.read_csv('data.csv')
# print(df.head())

# matplotlib: simple plot
import matplotlib.pyplot as plt
# plt.plot([1,2,3], [2,4,1])
# plt.show()

Best practices

  • Use virtual environments (venv, conda) so project dependencies are isolated.
  • Freeze dependencies with pip freeze > requirements.txt for reproducibility.
  • Read official docs for correct usage and performance tips.
  • Prefer high-level, well-maintained libraries for production code.

Common pitfalls

  • Version incompatibilities: different projects may require different library versions.
  • Name conflicts: avoid from module import * which can overwrite names.
  • Overusing many libraries for small tasks can bloat projects.

Summary
Python libraries are fundamental building blocks that provide tested, reusable functionality. Learning how to discover, install, import and apply libraries is a key skill in Class 12 Computer Science and real-world programming.

📌 Examples
  • NumPy for numerical arrays and fast vectorized computations: use np.array, np.mean, np.dot for operations on large datasets.
  • Pandas for tabular data: read CSV, filter rows, group data and compute aggregates with df.groupby() and df.describe().
  • Matplotlib/Seaborn for visualization: create line plots, bar charts and histograms to explore data trends.
  • Requests and BeautifulSoup for web tasks: fetch web pages with requests.get() and parse HTML with BeautifulSoup to extract information.
  • Scikit-learn for machine learning: train/test split, fit a model and predict (e.g., LinearRegression from sklearn.linear_model).
🧮 Formulas
  1. \[Mean (arithmetic): μ = (1/n) * Σ (x_i) — Python: statistics.mean(data) or np.mean(arr)\]
  2. \[Variance: σ^2 = (1/n) * Σ (x_i - μ)^2 — Python: statistics.pvariance(data) or np.var(arr)\]
  3. \[Standard deviation: σ = sqrt(σ^2) — Python: statistics.pstdev(data) or np.std(arr)\]
  4. \[Dot product of vectors a and b: a·b = Σ a_i * b_i — Python: np.dot(a\]
    \[b)\]
  5. \[Area of circle: A = π r^2 — Python: from math import pi\]
    \[A = pi * r * r\]
💻2

Modules and Packages

💻 COMPUTER SCIENCE / IT

Modules and Packages

Key Point: Import forms: import module import module as alias from module import name1, name2 from package.subpackage import module

What is a Module?

A module is a single Python file (with extension .py) that contains variables, functions, and classes which can be reused in other Python programs. Modules help organize code, avoid repetition, and provide namespaces.

Why use modules?

  • Reusability: write once, use many times.
  • Separation of concerns: group related functions together.
  • Namespace management: avoid name conflicts.

Creating and using a simple module

Example file: calculator.py

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

def subtract(a, b):
    return a - b

PI = 3.14159

if __name__ == "__main__":
    # Code here runs only when module is executed directly
    print("Calculator test:", add(2, 3))

Using the module in another file:

import calculator
print(calculator.add(5, 4))

from calculator import add, PI
print(PI)

import calculator as calc
print(calc.subtract(10, 3))

Import forms (common)

  • import module — imports module; access members with module.name.
  • from module import name — imports specific names into the current namespace.
  • from module import * — imports all public names (not recommended).
  • import module as alias — gives module an alias.
  • from package.subpackage import module — import from nested packages.

Module search and runtime behavior

  • When you import a module, Python searches directories in sys.path in order: current directory, PYTHONPATH entries, standard library directories, site-packages.
  • Imported modules are initialized once and cached in sys.modules. Subsequent imports reuse the cached module.
  • Use importlib.import_module('name') for dynamic imports.

What is a Package?

A package is a way of structuring Python’s module namespace by using “dotted module names”. A package is a directory that contains one or more modules or subpackages. Historically, a package directory contains an __init__.py file (which can be empty) to mark it as a package. (PEP 420 introduced namespace packages that may omit __init__.py.)

Package structure example

utilities/              # top-level package
    __init__.py
    text_utils.py        # module
    math_utils.py
    io/
        __init__.py
        file_ops.py

Importing from the package:

from utilities import math_utils
from utilities.io.file_ops import read_file

Relative imports inside a package

# inside utilities/io/file_ops.py
from .. import math_utils       # one level up
from . import helper_module     # same package

Key special names and techniques

  • __name__: a module’s name. If executed as main script, __name__ == "__main__".
  • __all__: list of public names to export when from module import * is used.
  • sys.path and sys.modules: inspect module search path and cache.
  • pip install package_name: install third-party packages to site-packages.

Best practices

  • Give modules short, meaningful names.
  • Avoid from module import * to prevent name collisions.
  • Keep packages logical and small; group related modules together.
  • Use virtual environments for project-specific packages.

In summary: modules are single files for organizing code; packages are directories that group modules and subpackages to form larger libraries. Together they enable modular, maintainable Python programs.

📌 Examples
  • Standard module: import math; print(math.sqrt(16)) # 4.0
  • Random module: import random; choices = random.choices(['A','B','C'], k=3)
  • Datetime module: from datetime import datetime; now = datetime.now()
  • Custom module: create calculator.py with add/subtract; import calculator and use calculator.add(2,3)
  • Package usage: utilities/io/file_ops.py providing read_file(); from utilities.io.file_ops import read_file
  • Using __name__: if __name__ == '__main__': run tests when module executed directly
🧮 Formulas
  1. \[Import forms: import module import module as alias from module import name1\]
    \[name2 from package.subpackage import module\]
  2. \[__name__ check: if __name__ == "__main__": # run only when executed directly\]
  3. \[Module search path (order): current directory -> PYTHONPATH -> standard library -> site-packages\]
  4. \[Relative imports inside package: from . import sibling_module from ..subpackage import module\]
💻3

Importing Modules

💻 COMPUTER SCIENCE / IT

Importing Modules

Key Point: import module

What is a module? A module is a file containing Python definitions (functions, classes, variables) and statements saved with a .py extension. A package is a folder of modules with an __init__.py (or implicit namespace package).

Why import modules? Importing lets you reuse code written elsewhere (standard library, third-party or your own). It keeps programs modular, readable and maintainable.

How import works (high-level):

  • When Python executes an import statement it: (1) locates the module, (2) compiles it to bytecode if needed, (3) executes it once creating a module object, and (4) caches it in sys.modules.
  • Subsequent imports reuse the cached module (no re-execution) unless explicitly reloaded.

Common import forms (explanations and behavior):

  • import module — import the module object; refer to members as module.name.
  • import module as alias — give a shorter/alternate name to the module.
  • from module import name1, name2 — import specific objects into current namespace.
  • from module import * — import all public names (not recommended in large programs due to namespace pollution).
  • from package import submodule and relative imports like from . import sibling or from ..subpackage import mod for packages.

Module search path: Python looks for modules in the order given by sys.path (script directory, PYTHONPATH entries, standard library directories, site-packages). You can view or modify sys.path at runtime if necessary.

Execution guard: To make a module both importable and executable as a script, use the idiom:

if __name__ == "__main__":
    # code that runs when module executed as a script

Reloading and dynamic import: Use importlib.reload(module) to reload an already imported module (useful in interactive sessions). Use importlib.import_module("modname") for dynamic import using a string module name.

Best practices:

  • Prefer explicit imports (from module import name) or qualified imports (import module) over wildcard imports.
  • Keep imports at the top of the file unless conditional imports are required for performance or to avoid circular imports.
  • Avoid circular imports by reorganizing code (use local imports inside functions or extract shared code into a third module).
  • Document third-party dependencies and manage them with pip and a requirements file.

Practical notes: Modules can define an __all__ list to control what from module import * brings into the namespace. Use dir(module) and help(module) for exploration.

📌 Examples
  • Example 1 — Basic import: import math print(math.sqrt(25)) # Output: 5.0
  • Example 2 — Import with alias: import numpy as np arr = np.array([1, 2, 3]) print(arr)
  • Example 3 — Import specific names: from datetime import date, timedelta today = date.today() print(today + timedelta(days=7))
  • Example 4 — Using __name__ guard (module usable as script and importable): # file: utils.py def greet(name): print(f"Hello, {name}") if __name__ == "__main__": greet('World') # When imported: only greet is available; when run: prints greeting.
  • Example 5 — Dynamic import and reload: import importlib m = importlib.import_module('random') importlib.reload(m)
  • Example 6 — Relative import in a package: # package structure: # mypkg/ # __init__.py # a.py # b.py # inside b.py from .a import some_function # import sibling module's function
🧮 Formulas
  1. \[import module\]
  2. \[import module as alias\]
  3. \[from module import name1\]
    \[name2\]
  4. \[from module import * # imports public names (use cautiously)\]
  5. \[from package import submodule\]
  6. \[from . import sibling # relative import within a package\]
💻4

Creating and Distributing Libraries

💻 COMPUTER SCIENCE / IT

Creating and Distributing Libraries

Key Point: Semantic versioning: VERSION = MAJOR.MINOR.PATCH (increment MAJOR for incompatible API changes, MINOR for added functionality backward-compatible, PATCH for backward-compatible bug fixes).

What is a library? A library (or package) is a reusable collection of modules, functions, classes and resources that other programs can import and use. Creating and distributing libraries lets you share code, apply modular design, and benefit the Python ecosystem.

Why create a library?

  • Reuse: avoid duplicate code across projects.
  • Share: others can use, test and improve your work.
  • Maintain: bug fixes and features delivered centrally.
  • Professional practices: documentation, testing, versioning and packaging.

Basic steps to create and distribute a Python library

  1. Design the package structure and write modules.
  2. Add metadata: README, LICENSE, version number and dependencies.
  3. Write tests and documentation.
  4. Create build configuration (pyproject.toml or setup.cfg/setup.py).
  5. Build distribution files: source distribution (sdist) and wheel (.whl).
  6. Publish to an index (PyPI or a private index) using twine.
  7. Install and use via pip.

Typical package structure

mypackage/            # repository root
  mypackage/           # package directory
    __init__.py
    core.py
    helpers.py
  tests/
    test_core.py
  README.md
  LICENSE
  pyproject.toml

Minimal pyproject.toml example

[build-system]
requires = ['setuptools', 'wheel']
build-backend = 'setuptools.build_meta'

[project]
name = 'mypackage'
version = '0.1.0'
description = 'Simple utility package'
readme = 'README.md'
license = {text = 'MIT'}
authors = [{name = 'Your Name', email = 'you@example.com'}]

Build and upload (commands)

  • Build distributions: python -m build (creates dist/ folder with sdist and wheel)
  • Upload to PyPI: python -m pip install --upgrade twine and python -m twine upload dist/*
  • Install: pip install your-package-name

Using the published library

from mypackage import core
core.do_something()

Best practices and extra considerations

  • Semantic versioning (MAJOR.MINOR.PATCH) for clear upgrade rules.
  • Include a clear LICENSE (MIT, Apache, GPL, ...).
  • Provide README, usage examples and API docs.
  • Automate tests and releases with CI (GitHub Actions, GitLab CI).
  • Use virtual environments when developing and testing.
  • Pin dependencies minimally and state compatibility in metadata.

Testing & CI

Write unit tests (pytest or unittest), run them on each push, and run packaging checks (e.g., check wheel install in a clean environment). Configure automatic builds and uploads (for example, only on tagged releases).

Publishing options

  • Public PyPI: for open-source distribution.
  • Test PyPI: to test uploads before making them public.
  • Private package indices or artifacts (for internal/proprietary code).

Security & maintenance

  • Keep credentials (API tokens) secure (use CI secrets).
  • Respond to issues and update dependencies to remove vulnerabilities.
📌 Examples
  • Real-world libraries: 'numpy' (numerical arrays and linear algebra), 'requests' (HTTP client), 'pandas' (dataframes) and 'matplotlib' (plotting). These began as code bases packaged, documented, versioned and published to PyPI so thousands use them by installing with pip.
  • Simple custom library workflow: 1) Create package folder mymath/ with __init__.py and functions like factorial and gcd. 2) Add pyproject.toml with metadata and version '0.1.0'. 3) Write tests in tests/test_mymath.py and run them locally. 4) Build with python -m build and upload to Test PyPI using twine to verify. 5) After verification, upload to PyPI and other projects install via pip install mymath.
  • Example small module code (inside mymath/__init__.py): def factorial(n): if n < 2: return 1 result = 1 for i in range(2, n+1): result *= i return result # Usage after install: # from mymath import factorial # print(factorial(5)) # prints 120
🧮 Formulas
  1. \[Semantic versioning: VERSION = MAJOR.MINOR.PATCH (increment MAJOR for incompatible API changes\]
    \[MINOR for added functionality backward-compatible\]
    \[PATCH for backward-compatible bug fixes).\]
  2. \[Import path form: import_reference = package_name[.subpackage][.module] (e.g.\]
    \[from package.subpackage import module_function).\]
  3. \[Build flow (conceptual): Source files + pyproject.toml -> build tool (setuptools/poetry) -> artifacts (sdist + wheel) -> upload (twine) -> index (PyPI) -> pip install -> runtime import\]
💻5

math Module

💻 COMPUTER SCIENCE / IT

math Module

Key Point: Area of circle: A = pi * r^2 (use math.pi)

The Python math module provides efficient, reliable mathematical functions and constants implemented in C. It is part of the standard library and is commonly used in Class 12 Computer Science for calculations involving trigonometry, logarithms, powers, rounding, combinatorics, and more. To use it: import math.

Key points:

  • Constants: math.pi, math.e.
  • Rounding and integer helpers: ceil, floor, trunc, fabs.
  • Power and roots: sqrt, pow, exp.
  • Logarithms: log (natural or base-n), log10, log2.
  • Trigonometry: sin, cos, tan, and conversions radians/degrees. Also inverse functions asin, acos, atan2.
  • Combinatorics & products (Python 3.8+): factorial, comb, perm, prod.
  • Numeric utilities: gcd, lcm, hypot, fsum (accurate float sum), isfinite, isnan, isclose.

Examples of usage:

import math
# basic
r = 2.5
area = math.pi * r**2
# trig (angles in radians)
theta_deg = 30
theta = math.radians(theta_deg)
sin30 = math.sin(theta)
# distance and hypotenuse
d = math.hypot(3, 4)    # 5.0
# log / exp
val = math.log(10)      # natural log
# combinatorics
ways = math.comb(5, 2)  # 10 (choose 2 from 5)

Notes:

  • Trigonometric functions expect radians; convert with math.radians() or math.degrees().
  • Many functions raise ValueError for invalid domains (e.g., math.sqrt(-1)); use cmath for complex results.
  • Use math.fsum instead of sum when high-precision floating-point accumulation is required.
  • Prefer math.hypot for Euclidean distance to avoid overflow/underflow issues.
📌 Examples
  • Compute area of a circle: import math; r=3; area=math.pi*r**2 # 28.274333882308138
  • Rounding: import math; math.ceil(2.3) -> 3, math.floor(2.8) -> 2, math.trunc(-2.9) -> -2
  • Trigonometry: import math; deg=45; math.sin(math.radians(deg)) -> 0.7071067811865475
  • Distance between points (x1,y1) and (x2,y2): import math; d = math.hypot(x2-x1, y2-y1)
  • Factorial and combinations: import math; math.factorial(6) -> 720; math.comb(6,2) -> 15
  • Compound interest (continuous): A = P * math.exp(r * t); where r is rate, t is time in years
🧮 Formulas
  1. \[Area of circle: A = pi * r^2 (use math.pi)\]
  2. \[Circumference: C = 2 * pi * r\]
  3. \[Euclidean distance: d = sqrt((x2 - x1)^2 + (y2 - y1)^2) (use math.hypot or math.sqrt)\]
  4. \[Pythagoras (hypotenuse): c = sqrt(a^2 + b^2) (use math.hypot(a,b))\]
  5. \[Quadratic formula: x = (-b ± sqrt(b^2 - 4ac)) / (2a) (use math.sqrt for discriminant)\]
  6. \[Compound interest (n compounding/year): A = P * (1 + r/n)^(n*t) (use math.pow or **)\]
💻6

random Module

💻 COMPUTER SCIENCE / IT

random Module

Key Point: Continuous uniform distribution on [a, b]: PDF f(x) = 1 / (b - a) for a <= x <= b; mean μ = (a + b) / 2; variance σ^2 = (b - a)^2 / 12

Overview: The Python random module provides functions to generate pseudo-random numbers and perform random selections. It is deterministic for a given seed (pseudo-random), so results can be reproduced using random.seed(). The module is commonly used for simulations, games, sampling, randomized algorithms and testing.

Key functions (short summary):

  • random.random() — float in [0.0, 1.0)
  • random.uniform(a, b) — float in [a, b] (continuous uniform)
  • random.randint(a, b) — integer in [a, b] inclusive (discrete uniform)
  • random.randrange(start, stop, step) — integer from range()
  • random.choice(seq) — single element from sequence
  • random.choices(population, weights=None, k=...) — list of k elements with optional weights (sampling with replacement)
  • random.sample(population, k) — k unique elements (sampling without replacement)
  • random.shuffle(list) — in-place random permutation of a list
  • random.seed(a) — initialize generator for reproducible results
  • random.gauss(mu, sigma) / random.normalvariate(mu, sigma) — draw from normal distribution

Behavior and notes:

  • The module uses a deterministic algorithm (Mersenne Twister) by default; it is not suitable for cryptographic purposes — use secrets for secure randomness.
  • seed() with the same argument produces the same sequence of values — useful for testing and demonstrations.
  • For weighted or distributional sampling, use random.choices or draw from distribution-specific functions (e.g., gauss).

Short code examples:

# basic float
x = random.random()          # 0.0 <= x < 1.0

# integer and uniform
n = random.randint(1, 6)     # simulate a die (1 to 6)
y = random.uniform(-1, 1)    # continuous between -1 and 1

# choice, sample, shuffle
winner = random.choice(['A', 'B', 'C'])
team = random.sample(students, k=3)  # 3 distinct students
random.shuffle(deck)                   # shuffle a deck list

# reproducibility
random.seed(42)
print(random.random())
📌 Examples
  • Simulate a six-sided die: import random; roll = random.randint(1, 6)
  • Shuffle a deck of cards: import random; random.shuffle(deck) # deck is a list of 52 card strings
  • Pick a random student for a prize: winner = random.choice(students)
  • Sampling without replacement for a survey of 5 people: sample = random.sample(population, k=5)
  • Reproducible experiment: random.seed(123); values = [random.random() for _ in range(5)] # same every run
  • Simulate coin tosses (real-life example): Use random.random() < 0.5 as 'heads' (probability 0.5) and repeat N times to estimate frequency of heads.
🧮 Formulas
  1. \[Continuous uniform distribution on [a\]
    \[b]: PDF f(x) = 1 / (b - a) for a <= x <= b\]
    \[mean μ = (a + b) / 2\]
    \[variance σ^2 = (b - a)^2 / 12\]
  2. \[Discrete uniform on integers {a\]
    \[a+1, ...\]
    \[b}: mean μ = (a + b) / 2\]
    \[variance σ^2 = ((b - a + 1)^2 - 1) / 12\]
  3. \[Binomial PMF (useful when modeling repeated independent Bernoulli trials): P(X = k) = C(n\]
    \[k) p^k (1 - p)^(n - k)\]
  4. \[Normal (Gaussian) PDF: f(x) = (1 / (σ sqrt(2π))) * exp(- (x - μ)^2 / (2 σ^2)) — random.gauss(mu\]
    \[sigma) samples from this distribution\]
  5. \[Law of Large Numbers (applied informally): as sample size N → ∞\]
    \[sample mean of independent draws → theoretical mean (explains convergence of averages in simulations)\]
📊7

statistics Module

💻 COMPUTER SCIENCE / IT

statistics Module

Key Point: Arithmetic mean (μ or x̄): mean = (Σ xi) / n

The Python statistics module (part of the standard library) provides functions to compute common statistical measures—mean, median, mode, variance, standard deviation, and more—on numeric data. It is intended for simple descriptive statistics on small to medium datasets. The module accepts any iterable of numbers (lists, tuples, etc.) and raises StatisticsError for empty inputs or undefined results (for example, mode on multimodal data).

Key functions:

  • mean(data) — arithmetic mean (works with ints and floats).
  • fmean(data) — faster floating-point mean (useful when performance matters).
  • median(data), median_low, median_high — median for odd/even lengths; median_grouped for grouped data.
  • mode(data), multimode(data) — most common value(s).
  • pstdev(data), pvariance(data) — population standard deviation/variance (divide by n).
  • stdev(data), variance(data) — sample standard deviation/variance (divide by n-1).
  • geometric_mean(data), harmonic_mean(data) — means for multiplicative or rate-based data.
  • covariance(x, y), correlation(x, y) — relationship between two numeric variables (added in recent Python versions).

Practical notes:

  • Use fmean when you only need float precision and want speed for large iterables.
  • Use population (pvariance/pstdev) when your data represent the entire population; use sample (variance/stdev) when your data are a sample from a larger population.
  • mode raises StatisticsError if no unique mode exists; use multimode to get all modes.
  • For robust descriptions of distributions, combine these statistics with visualisations (histograms, box plots, scatter plots).

Example code (quick reference):

import statistics as stats
data = [10, 20, 20, 30, 40]
print('mean:', stats.mean(data))
print('median:', stats.median(data))
print('mode:', stats.mode(data))
print('variance (sample):', stats.variance(data))
print('stdev (population):', stats.pstdev(data))
# covariance and correlation (if available)
x = [1, 2, 3, 4]
y = [2, 4, 5, 8]
print('covariance:', stats.covariance(x, y))
print('correlation:', stats.correlation(x, y))
📌 Examples
  • Example 1 — Class test scores (arithmetic mean and median): Given marks [45, 62, 78, 90, 55], use statistics.mean() to get average score and statistics.median() to find the middle student score. Code: import statistics as stats; scores = [45,62,78,90,55]; stats.mean(scores) -> 66.0; stats.median(scores) -> 62.
  • Example 2 — Most common shoe size (mode): In a survey [7,8,7,9,8,7], stats.mode() returns 7 as the most frequent size. If multiple sizes tie, use stats.multimode() to list all modes.
  • Example 3 — Population vs sample variance: A factory knows diameters of all 1000 produced parts (population) -> use stats.pvariance(); if you measure a random sample of 10 parts (sample) -> use stats.variance() to estimate population variance (divides by n-1).
  • Example 4 — Average speed (harmonic mean): For round trips with speeds 60 km/h (going) and 40 km/h (return), the correct average speed is harmonic mean: stats.harmonic_mean([60,40]) = 48 km/h (not simple arithmetic mean).
  • Example 5 — Investment returns (geometric mean): Annual returns [1.10, 0.90, 1.20] (factors) -> geometric mean stats.geometric_mean([1.10,0.90,1.20]) gives average growth factor per year used to compute compounded return.
  • Example 6 — Relationship between two variables: Use stats.covariance(x,y) and stats.correlation(x,y) to measure how two variables move together (e.g., hours studied vs. marks obtained).
🧮 Formulas
  1. \[Arithmetic mean (μ or x̄): mean = (Σ xi) / n\]
  2. \[Median: sort data\]
    \[if n odd -> middle value\]
    \[if n even -> average of two middle values\]
  3. \[Mode: value(s) with highest frequency in the dataset\]
  4. \[Population variance (σ^2): pvariance = (Σ (xi - μ)^2) / n\]
  5. \[Population standard deviation (σ): pstdev = sqrt(pvariance)\]
  6. \[Sample variance (s^2): variance = (Σ (xi - x̄)^2) / (n - 1)\]
💻8

os and sys Modules

💻 COMPUTER SCIENCE / IT

os and sys Modules

Key Point: os.getcwd() -> returns current working directory (string)

Overview

The os and sys modules are part of Python's standard library and give programs access to operating system services and interpreter-level information. They are commonly used in scripts that interact with the filesystem, manage processes, or handle command-line arguments.

os module (Operating System interfaces)

  • Purpose: Interact with the file system and environment (create/delete files and directories, examine file attributes, work with environment variables, run shell commands).
  • Common categories of functionality:
    • Filesystem navigation: os.getcwd(), os.chdir(path), os.listdir(path)
    • Directory/file creation & removal: os.mkdir(name), os.makedirs(path), os.remove(path), os.rmdir(path)
    • Path utilities (via os.path): os.path.join(a, b), os.path.exists(path), os.path.isfile(path), os.path.isdir(path), os.path.splitext(name), os.path.abspath(path)
    • Environment & process: os.environ (mapping of environment variables), os.getenv('VAR'), os.system(cmd) (run a shell command)

sys module (Interpreter and runtime information)

  • Purpose: Access data and functions that interact with the Python interpreter itself.
  • Common uses:
    • Command-line arguments: sys.argv (list of command-line strings; sys.argv[0] is the script name)
    • Exit and status: sys.exit([status]) to terminate with an exit code
    • I/O streams: sys.stdin, sys.stdout, sys.stderr
    • Interpreter info: sys.version, sys.platform
    • Module & path management: sys.modules, sys.path (list used for module search)
    • Recursion control & limits: sys.getrecursionlimit(), sys.setrecursionlimit(n)

Key differences (quick)

  • os talks to the operating system (files, directories, environment), while sys talks to the Python interpreter (arguments, I/O streams, import paths).
  • They are often used together in scripts (e.g., read command-line args with sys.argv, then perform file operations with os).

Best practices

  • Use os.path.join() instead of string concatenation to build paths so code works cross-platform.
  • Prefer os.makedirs(path, exist_ok=True) when creating nested directories safely.
  • Use sys.exit(code) to indicate success (0) or failure (nonzero) to the calling process.
  • Use with open(...) for file handling; combine with os.path checks if needed.

Small example explanation

Typical flow: a Python script takes filenames from sys.argv, checks if they exist using os.path.exists(), processes them (reading/writing), and writes output to a directory created via os.makedirs(). This makes scripts portable and robust.

📌 Examples
  • Example 1 — List all files in a directory and print absolute paths: import os path = 'my_folder' for name in os.listdir(path): full = os.path.join(path, name) if os.path.isfile(full): print(os.path.abspath(full))
  • Example 2 — Script using command-line arguments to copy a file name (simple demonstration): import sys import os if len(sys.argv) != 3: print('Usage: python script.py source_file dest_file') sys.exit(1) src, dst = sys.argv[1], sys.argv[2] if not os.path.exists(src): print('Source file not found') sys.exit(2) with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst: fdst.write(fsrc.read()) print('Copied successfully')
  • Example 3 — Create nested directories safely and write a log using environment variable: import os out_dir = os.path.join(os.getcwd(), 'output', 'logs') os.makedirs(out_dir, exist_ok=True) username = os.environ.get('USER', os.environ.get('USERNAME', 'unknown')) log_path = os.path.join(out_dir, 'run.log') with open(log_path, 'a') as f: f.write(f'Run by {username}\n')
🧮 Formulas
  1. \[os.getcwd() -> returns current working directory (string)\]
  2. \[os.chdir(path) -> change current working directory\]
  3. \[os.listdir(path) -> list of names in the directory\]
  4. \[os.path.join(a\]
    \[b, ...) -> safely join path components into one path (string)\]
  5. \[os.path.exists(path) -> True if path exists\]
  6. \[os.path.isfile(path) / os.path.isdir(path) -> booleans to check type\]
📊9

File Formats and Data Modules (csv, json, pickle)

💻 COMPUTER SCIENCE / IT

File Formats and Data Modules (csv, json, pickle)

Key Point: csv.reader(fileobj, delimiter=',', quotechar='"') # returns iterator of rows as lists

Overview: Python commonly uses three file/data formats for persisting and exchanging information: CSV (Comma Separated Values) for tabular data, JSON (JavaScript Object Notation) for structured text data interoperable across languages, and Pickle for Python-native binary serialization of arbitrary objects. Each has a dedicated module: csv, json, and pickle.

CSV (csv module): CSV is plain-text, row/column oriented. The csv module helps read/write rows as lists or dictionaries. Use csv.reader/csv.writer for list-of-values and csv.DictReader/csv.DictWriter for mapping column names to values. Important: open files with newline='' in Python to avoid extra blank lines on Windows. CSV is best for spreadsheets, logs, and simple tabular export/import. Limitations: no nested structures or native types (everything is text), so numbers/dates require conversion.

JSON (json module): JSON is a text format representing objects (dicts), arrays (lists), strings, numbers, booleans and null. Python maps JSON objects to dict, arrays to list, true/false to True/False, and null to None. Use json.dump/json.load for files and json.dumps/json.loads for strings. JSON is language-independent and common in web APIs, configuration files, and data interchange. Limitations: cannot represent arbitrary Python objects (like custom class instances) without conversion.

Pickle (pickle module): Pickle converts (serializes) nearly any Python object to a binary byte stream and back (deserialization). Use pickle.dump(obj, file) and pickle.load(file) with files opened in binary mode ('wb', 'rb'). Pickle is convenient for caching models, session state, or complex Python objects. Major caution: unpickling data from untrusted sources is a security risk (it can execute arbitrary code). Pickle format is Python-specific and generally not interoperable with other languages.

When to use which:

  • CSV: simple tabular data, human-readable spreadsheets, CSV import/export.
  • JSON: structured data for configuration, web APIs, exchange between systems.
  • Pickle: fast persistence of Python-only complex objects, ML models, caches (only when data source is trusted).

Practical tips:

  • CSV: specify delimiter, quotechar, and use DictReader/DictWriter for column names.
  • JSON: use indent to pretty-print, set ensure_ascii=False for non-ASCII text, and convert non-JSON types (e.g., datetime) to strings or custom encoders.
  • Pickle: choose protocol (default is fine), always open files in binary mode, never unpickle data from the network or untrusted files.

Security & Interoperability: JSON and CSV are text formats suitable for inter-system exchange. Pickle is internal to Python and unsafe across untrusted boundaries.

📌 Examples
  • # CSV: write and read using DictWriter/DictReader import csv with open('students.csv', 'w', newline='') as f: writer = csv.DictWriter(f, fieldnames=['id', 'name', 'marks']) writer.writeheader() writer.writerow({'id': 1, 'name': 'Asha', 'marks': 89}) writer.writerow({'id': 2, 'name': 'Ravi', 'marks': 92}) with open('students.csv', 'r', newline='') as f: reader = csv.DictReader(f) for row in reader: # row is a dict: convert types as needed print(row['id'], row['name'], int(row['marks']))
  • # JSON: save and load structured config import json config = { 'app': 'Calc', 'version': 1.2, 'features': ['add', 'subtract'] } with open('config.json', 'w', encoding='utf-8') as f: json.dump(config, f, indent=2, ensure_ascii=False) with open('config.json', 'r', encoding='utf-8') as f: cfg = json.load(f) print(cfg['features'])
  • # Pickle: serialize and deserialize Python object (trusted use only) import pickle model = {'weights': [0.1, 0.2, 0.3], 'bias': 0.01} with open('model.pkl', 'wb') as f: pickle.dump(model, f) # binary write with open('model.pkl', 'rb') as f: loaded = pickle.load(f) print(loaded['weights']) # WARNING: never unpickle data from untrusted sources
🧮 Formulas
  1. \[csv.reader(fileobj\]
    \[delimiter=','\]
    \[quotechar='"') # returns iterator of rows as lists\]
  2. \[csv.writer(fileobj\]
    \[delimiter=','\]
    \[quotechar='"') # write rows as lists\]
  3. \[csv.DictReader(fileobj) -> yields dicts mapping header->value\]
  4. \[csv.DictWriter(fileobj\]
    \[fieldnames=[...]) -> writer.writeheader()\]
    \[writer.writerow(dict)\]
  5. \[json.dump(obj\]
    \[fileobj, *\]
    \[indent=None\]
    \[ensure_ascii=True) # write JSON to file\]
  6. \[json.dumps(obj) -> str\]
    \[json.load(fileobj) -> obj\]
    \[json.loads(str) -> obj\]
💻10

re (Regular Expressions)

💻 COMPUTER SCIENCE / IT

re (Regular Expressions)

Key Point: Character classes: \d = digit [0-9], \w = word char [A-Za-z0-9_], \s = whitespace

What are regular expressions? Regular expressions (regex) are compact patterns used to match, search, and manipulate text. In Python the re module provides functions to work with regex: match, search, findall, finditer, sub, split, and compile.

Key concepts

  • pattern: the regex string that describes what to search for.
  • Character classes (e.g. \d, \w, \s) and custom classes (e.g. [A-Za-z0-9]).
  • Anchors: ^ (start of string/line), $ (end of string/line).
  • Quantifiers: *, +, ?, {m,n} (how many times an element repeats).
  • Groups and capturing: (...) capture subpatterns; (?:...) is a non-capturing group.
  • Greedy vs non-greedy: * and + are greedy by default; add ? to make them non-greedy (e.g. .*?).
  • Flags: re.IGNORECASE (re.I), re.MULTILINE (re.M), re.DOTALL (re.S).

Common functions

  • re.match(pattern, text): checks for a match at the beginning of text.
  • re.search(pattern, text): searches anywhere in text for the first match.
  • re.findall(pattern, text): returns a list of all non-overlapping matches (strings or tuples if groups present).
  • re.finditer(pattern, text): returns an iterator of match objects (useful to get positions).
  • re.sub(pattern, repl, text): substitutes matches with repl.
  • re.split(pattern, text): splits text around matches.
  • re.compile(pattern, flags): compiles a pattern into a regex object for repeated use (better performance).

Best practices

  • Use raw string literals for patterns: r"\d+", so backslashes are not processed by Python string escapes.
  • Use re.escape() when inserting literal text into patterns.
  • Compile frequently used patterns with re.compile() to improve performance.
  • Aim to anchor patterns (^, $) when possible to avoid accidental slow backtracking.
📌 Examples
  • 1) Simple search and match: import re text = "Hello 2025" print(re.search(r"\d+", text).group()) # '2025' print(bool(re.match(r"Hello", text))) # True
  • 2) Find all words: import re s = "This is a test. Test123 and more_tests" print(re.findall(r"\w+", s)) # ['This','is','a','test','Test123','and','more_tests']
  • 3) Email validation (basic): import re pattern = re.compile(r"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$") print(bool(pattern.match('alice@example.com'))) # True print(bool(pattern.match('bad@.com'))) # False
  • 4) Extract dates (DD-MM-YYYY) and capture groups: import re text = 'Event on 05-10-2025 and 12-11-2024.' for m in re.finditer(r"(\d{2})-(\d{2})-(\d{4})", text): day, month, year = m.groups() print(day, month, year) # prints: 05 10 2025 and 12 11 2024
  • 5) Replace multiple spaces and trim: import re s = 'This is spaced\n' print(re.sub(r"\s+", ' ', s).strip()) # 'This is spaced'
  • 6) Log parsing — find IP addresses: import re log = 'Client 192.168.1.10 requested /index.html' ip = re.search(r"\b(?:\d{1,3}\.){3}\d{1,3}\b", log) if ip: print(ip.group()) # '192.168.1.10'
🧮 Formulas
  1. \[Character classes: \d = digit [0-9]\]
    \[\w = word char [A-Za-z0-9_]\]
    \[\s = whitespace\]
  2. \[Custom classes: [abc] (one of a\]
    \[b\]
    \[or c), [^abc] (not a\]
    \[b\]
    \[or c)\]
  3. \[Anchors: ^ (start), $ (end)\]
    \[\b (word boundary)\]
    \[\B (not word boundary)\]
  4. \[Quantifiers: * (0 or more), + (1 or more), ? (0 or 1), {m} (exactly m), {m,n} (between m and n)\]
  5. \[Groups: (pattern) captures\]
    \[(?:pattern) non-capturing\]
    \[(?P<name>pattern) named group\]
  6. \[Alternation: a|b (matches a or b)\]
💻11

GUI Programming with tkinter

💻 COMPUTER SCIENCE / IT

GUI Programming with tkinter

Key Point: Create window: root = Tk(); root.title('Title'); root.geometry('WxH')

What is tkinter?
tkinter is Python's standard library for creating graphical user interfaces (GUIs). It provides a thin object-oriented layer on top of Tcl/Tk. With tkinter you build windows, dialogs and widgets (buttons, labels, text entry, canvas, menus) and handle user events in an event-driven loop.

Key concepts

  • Root window: The main application window created with Tk().
  • Widgets: UI elements such as Label, Button, Entry, Text, Frame, Canvas, Menu.
  • Geometry managers: Control placement using pack(), grid(), or place().
  • Event-driven programming: The program waits for events (clicks, keypresses) and calls callback functions. The loop is started with mainloop().
  • Control variables: StringVar, IntVar, BooleanVar connect widget state to Python variables for two-way updates.

Basic structure (typical program)

import tkinter as tk

root = tk.Tk()               # create main window
root.title('My App')
root.geometry('300x200')     # optional: set size

label = tk.Label(root, text='Hello')
label.pack()                 # use a geometry manager

button = tk.Button(root, text='Click', command=lambda: print('clicked'))
button.pack()

root.mainloop()              # start event loop

Widgets & common options

  • Label(parent, text='...') — display text or images.
  • Button(parent, text='...', command=callback) — clickable button.
  • Entry(parent) — single-line input; use .get() to read.
  • Text(parent) — multi-line text area.
  • Listbox, Radiobutton, Checkbutton — selection widgets.
  • Canvas(parent) — draw shapes, handle mouse drawing and simple games.
  • Menu — application menus and context menus.

Geometry managers

  • pack() — simple stacking (top/bottom/left/right); options: side, fill, expand.
  • grid() — place widgets in rows and columns; good for forms and calculators.
  • place() — absolute positioning (x, y) and relative sizes; less commonly used for responsive UIs.

Event binding & callbacks

# Button uses 'command' for click
button = tk.Button(root, text='OK', command=on_ok)

# Generic event binding (mouse, keyboard)
root.bind('', on_enter)    # on_enter(event) will be called
canvas.bind('', on_click)

Common additions

  • messagebox: showinfo, showwarning, askyesno for dialogs.
  • filedialog: askopenfilename, asksaveasfilename for file selection.
  • ttk: Themed tkinter widgets (ttk.Button, ttk.Label) for modern look.

Event loop model (brief)
Tkinter runs a loop that waits for user or system events and dispatches them to your callback functions. Your callbacks should be short and non-blocking; long tasks should run in threads or use asynchronous techniques.

Good practices

  • Use grid() for form-like layouts (avoid mixing pack() and grid() in same container).
  • Keep UI responsive: avoid heavy computation in callback directly.
  • Use StringVar/IntVar where widgets need to share state.
  • Organize UI into Frames for clarity and reuse.

Short complete example — simple calculator UI (layout only)

import tkinter as tk

root = tk.Tk()
root.title('Calculator')

entry = tk.Entry(root, width=16, justify='right')
entry.grid(row=0, column=0, columnspan=4)

buttons = [
    ('7',1,0), ('8',1,1), ('9',1,2), ('/',1,3),
    ('4',2,0), ('5',2,1), ('6',2,2), ('*',2,3),
    ('1',3,0), ('2',3,1), ('3',3,2), ('-',3,3),
    ('0',4,0), ('.',4,1), ('=',4,2), ('+',4,3),
]
for (text,r,c) in buttons:
    tk.Button(root, text=text, width=4).grid(row=r, column=c)

root.mainloop()
📌 Examples
  • Login form: Labels + Entry for username/password, Button to validate; use StringVar to read entries and messagebox to show results.
  • Simple calculator: Use Entry for display and Buttons arranged in a grid to build numeric operations; use eval carefully or implement parsing.
  • Text editor: Menu (File->Open/Save), Text widget for editing, filedialog to load/save files, messagebox for confirmations.
  • Drawing app: Canvas widget with mouse bindings (<Button-1>, <B1-Motion>) to draw freehand lines; Useful for signature pad or simple paint.
  • To-do list app: Listbox to show tasks, Entry to add tasks, Buttons to add/remove, and persistence with a text file.
🧮 Formulas
  1. \[Create window: root = Tk()\]
    \[root.title('Title')\]
    \[root.geometry('WxH')\]
  2. \[Create widget: widget = WidgetClass(parent\]
    \[option1=value1, ...)\]
    \[widget.pack()/grid()/place()\]
  3. \[Button callback: btn = Button(parent\]
    \[text='OK'\]
    \[command=callback) # callback takes no args\]
  4. \[Event binding: widget.bind('<EventPattern>'\]
    \[handler) # handler(event) receives event object\]
  5. \[Control variable: var = StringVar()\]
    \[entry = Entry(parent\]
    \[textvariable=var)\]
    \[value = var.get()\]
    \[var.set('new')\]
📈12

Graphics with turtle

💻 COMPUTER SCIENCE / IT

Graphics with turtle

Key Point: Exterior angle of a regular n-sided polygon = 360° / n

Overview

The Python turtle module provides a simple drawing/graphics environment that is ideal for teaching programming, geometry and animation. It models a pen (the turtle) that moves on a 2D canvas: commands move the turtle and draw lines.

How it works (core concepts)

  • Turtle and Screen: Create a Screen object (the window/canvas) and one or more Turtle objects (the pens that draw).
  • Movement commands: forward(dist), backward(dist), left(angle), right(angle), goto(x, y).
  • Pen control: penup()/pendown(), pensize(), pencolor(), and fill control with begin_fill()/end_fill().
  • Drawing arcs/circles: circle(radius, extent=None) draws circular arcs (extent in degrees).
  • Heading and coordinates: Default heading is 0° (pointing east). Angles increase counterclockwise: 90° points north. The screen origin (0,0) is the center by default.
  • Animation control: speed(), tracer()/update() for fast drawing.
  • Interactivity: onclick(), onkey(), textinput() support event-driven programs.

Coordinate system & orientation

  • Origin (0,0) is centered by default (can be changed with setworldcoordinates()).
  • X increases to the right, Y increases upward.
  • Heading 0° → east (right), 90° → north (up), 180° → west (left), 270° → south (down).

Useful tips

  • Use tracer(0) and update() to draw many shapes quickly without animation overhead.
  • Use multiple turtles to draw different parts concurrently (or to keep pens with different styles).
  • Use begin_fill()/end_fill() to draw filled shapes.
  • Use clear() to erase drawing (keeping turtle state) and reset() to reset the turtle.

Where it's useful (real-life / pedagogic examples)

  • Teaching geometry: visualize polygons, angles, symmetry, transformations (translation/rotation/scaling).
  • Algorithm visualization: show pathfinding, recursion (fractal trees, Koch snowflake), sorting algorithms pictorially.
  • Simple GUIs and interactive games: click-to-draw, turtle-controlled games.
  • Art and design: generative art, spirographs, colourful spirals and patterns.

Minimal example (draw a square)

import turtle
screen = turtle.Screen()
pen = turtle.Turtle()
for _ in range(4):
    pen.forward(100)
    pen.right(90)
screen.mainloop()

Performance/animation example

To draw complex patterns fast, disable animation and update at the end:

screen.tracer(0)
# ... many drawing operations ...
screen.update()
📌 Examples
  • Draw a regular polygon (n sides): import turtle pen = turtle.Turtle() def polygon(n, side): turn = 360 / n for _ in range(n): pen.forward(side) pen.right(turn) polygon(6, 80) # draws a hexagon
  • Colorful spiral (visual art): import turtle t = turtle.Turtle(); t.speed(0) colors = ['red','orange','yellow','green','blue','purple'] for i in range(200): t.pencolor(colors[i % len(colors)]) t.forward(i * 2 / 3) t.right(59) # Use screen.mainloop() or turtle.done()
  • Draw a simple bar chart with turtle: import turtle data = [50, 120, 80, 170] pen = turtle.Turtle(); pen.penup(); pen.goto(-150, -150); pen.pendown() for value in data: pen.begin_fill() pen.forward(40) pen.left(90) pen.forward(value) pen.left(90) pen.forward(40) pen.left(90) pen.forward(value) pen.left(90) pen.end_fill() pen.forward(10)
  • Spirograph-like circles (using circle()): import turtle t = turtle.Turtle(); t.speed(0) for i in range(36): t.circle(80) t.right(10)
🧮 Formulas
  1. \[Exterior angle of a regular n-sided polygon = 360° / n\]
  2. \[Interior angle of a regular n-sided polygon = 180° - (360° / n) = (n-2)*180 / n\]
  3. \[Distance between two points (x1,y1) and (x2,y2): sqrt((x2-x1)^2 + (y2-y1)^2)\]
  4. \[Circle circumference = 2 * π * r (useful to relate circle radius to length drawn by turtle)\]
  5. \[Arc length for angle θ (in radians) = r * θ (if θ in degrees\]
    \[convert: θ_rad = θ_deg * π/180)\]
  6. \[Side length of a regular n-gon inscribed in circle radius r: side = 2 * r * sin(π / n)\]
🌍13

Package Management (pip) and Virtual Environments

💻 COMPUTER SCIENCE / IT

Package Management (pip) and Virtual Environments

Key Point: Location of installed packages in a venv (Unix-like): installed_location = /lib/pythonX.Y/site-packages

Overview
Package management with pip and virtual environments (venv/virtualenv) are essential for managing third‑party Python libraries and isolating project dependencies. pip installs, uninstalls, lists and inspects packages. Virtual environments create per‑project, isolated Python runtimes so packages and versions for one project don't interfere with others or the system Python.

Why use them?

  • Avoid version conflicts: two projects can require different versions of the same library (for example, Django 2.x vs 3.x).
  • Reproducibility: using requirements.txt ensures other developers or deployment servers can install the same package set.
  • Safety: do not modify system Python packages which may be used by OS components.

Common tools/commands

  • python -m venv env — create a virtual environment named env (builtin in Python 3).
  • source env/bin/activate (Linux/macOS) or env\Scripts\activate (Windows) — activate venv.
  • deactivate — leave the virtual environment.
  • pip install package — install a package into the active environment.
  • pip install -r requirements.txt — install packages listed in a requirements file.
  • pip freeze — show exact package versions (useful to create requirements.txt).
  • pip list, pip show package, pip uninstall package, pip install --upgrade package.

Typical workflow

  1. Create a venv:
    python -m venv myenv
  2. Activate it:
    source myenv/bin/activate  # macOS/Linux
    myenv\Scripts\activate     # Windows
  3. Install packages:
    pip install requests pandas
  4. Freeze exact versions:
    pip freeze > requirements.txt
  5. Share requirements.txt; others run:
    pip install -r requirements.txt
  6. When done:
    deactivate

Dependency resolution
When you install a package, pip will resolve and download dependencies (other packages required). The result is a dependency graph where nodes are packages and directed edges mean "requires". Conflicts occur when two packages require incompatible versions of the same dependency; tools or manual changes are needed to resolve them.

Best practices

  • Create one virtual environment per project.
  • Use pip freeze > requirements.txt for deployment / sharing.
  • Prefer exact pinned versions for production (package==x.y.z) and looser ranges (>=, ~=) during development where appropriate.
  • Keep pip updated: python -m pip install --upgrade pip.
  • For complex dependency management consider higher‑level tools (pipenv, poetry) which add lockfiles and workspace management.

Notes for CBSE exam style
Be able to write commands, explain purpose of venv and pip, and to show how to create, activate, install, freeze, and deactivate an environment. Explain with a small example workflow and the role of requirements.txt.

📌 Examples
  • Create and use a virtual environment: python -m venv env; activate (source env/bin/activate or env\Scripts\activate); pip install requests; python script.py; deactivate.
  • Share dependencies: pip freeze > requirements.txt (gives lines like requests==2.31.0); another developer runs pip install -r requirements.txt to reproduce same setup.
  • Two projects with conflicting versions: Project A needs pandas==1.5.3 and Project B needs pandas==2.0.1. Create separate venvs (envA, envB) and install each required version inside its own venv to avoid conflicts.
  • Upgrade safely: inside venv run pip install --upgrade package. If upgrade breaks code, revert by installing a previous pinned version: pip install package==x.y.z.
🧮 Formulas
  1. \[Location of installed packages in a venv (Unix-like): installed_location = <venv_path>/lib/pythonX.Y/site-packages\]
  2. \[requirements.txt entry format (pin exact version): package==major.minor.patch e.g.\]
    \[requests==2.31.0\]
  3. \[Command relation: pip freeze > requirements.txt -> pip install -r requirements.txt reproduces same versions\]
  4. \[Dependency graph: Graph G = (V\]
    \[E) where V = {packages}\]
    \[E = {(A\]
    \[B) | A requires B}\]
    \[conflict if exists packages X\]
    \[Y with required versions for Z that are incompatible.\]
💻14

Common Third-party Libraries (overview)

💻 COMPUTER SCIENCE / IT

Common Third-party Libraries (overview)

Key Point: NumPy array creation and element-wise ops: a = np.array([1,2,3]); b = a * 2

What are third-party libraries? Third-party libraries are pre-written packages created by the community that extend Python's capabilities (for data handling, plotting, web access, machine learning, image processing, etc.). They are not part of the Python standard library and are usually installed using pip (for example: pip install numpy).

Why use them?

  • Save time: provide well-tested functions and data structures.
  • Specialised functionality: numerical computing, visualization, ML, web scraping, image I/O.
  • Interoperability: many libraries work together (e.g., pandas + matplotlib + scikit-learn).

Overview of commonly used third-party libraries (Class 12 focus)

  • NumPy — numerical arrays, vectorized operations, linear algebra. Use for fast number crunching and array math.
  • pandas — tabular data structure (Series, DataFrame), data cleaning, grouping and aggregation.
  • matplotlib — basic plotting library for line, bar, scatter, histogram, etc.
  • seaborn — statistical data visualization built on matplotlib; easier attractive plots like heatmaps and boxplots.
  • scipy — scientific computing: integration, optimization, signal processing (built on NumPy).
  • scikit-learn — machine learning: classification, regression, clustering, model selection and evaluation.
  • requests — simple HTTP library for making web requests (APIs, REST calls).
  • BeautifulSoup (bs4) — HTML/XML parsing for web scraping (extracting content from web pages).
  • Pillow (PIL) — image processing: open, resize, save, basic transforms.
  • OpenCV (cv2) — advanced image and video processing, computer vision tasks.
  • Flask — lightweight web framework for building simple web apps and APIs (useful for deploying models).

How they are typically used together

  • Load tabular data with pandas, compute numeric arrays with NumPy, plot results with matplotlib/seaborn.
  • Scrape web data with requests + BeautifulSoup, store in pandas DataFrame, clean and visualize.
  • Preprocess data with pandas/numpy, train models with scikit-learn, evaluate and plot metrics with matplotlib.

Best practices

  • Read official docs and examples for each library.
  • Use virtual environments (venv) to avoid dependency conflicts.
  • Prefer vectorized NumPy/pandas operations over Python loops for performance.
📌 Examples
  • Data analysis with pandas + matplotlib: Read a CSV, compute averages, plot a line. Example: <pre><code>import pandas as pd import matplotlib.pyplot as plt df = pd.read_csv('students_marks.csv') avg = df.groupby('subject')['marks'].mean() avg.plot(kind='bar') plt.show()</code></pre>
  • Numerical operations with NumPy: fast element-wise math and linear algebra. Example: <pre><code>import numpy as np A = np.array([[1,2],[3,4]]) b = np.array([5,6]) x = np.linalg.solve(A,b) # solves A x = b</code></pre>
  • Web access and scraping: fetch HTML with requests and parse with BeautifulSoup. Example: <pre><code>import requests from bs4 import BeautifulSoup r = requests.get('https://example.com') soup = BeautifulSoup(r.text, 'html.parser') headings = [h.text for h in soup.find_all('h2')]</code></pre>
  • Machine learning with scikit-learn: train/test and fit a classifier. Example: <pre><code>from sklearn.model_selection import train_test_split from sklearn.linear_model import LogisticRegression X_train, X_test, y_train, y_test = train_test_split(X, y) clf = LogisticRegression().fit(X_train, y_train) print(clf.score(X_test, y_test))</code></pre>
  • Image processing with Pillow: open, resize, save. Example: <pre><code>from PIL import Image img = Image.open('photo.jpg') img2 = img.resize((300,300)) img2.save('photo_small.jpg')</code></pre>
🧮 Formulas
  1. \[NumPy array creation and element-wise ops: a = np.array([1,2,3])\]
    \[b = a * 2\]
  2. \[Matrix multiplication: C = A.dot(B) or C = A @ B\]
  3. \[pandas: read and basic operations: df = pd.read_csv('file.csv')\]
    \[df.head()\]
    \[df['col'].mean()\]
  4. \[pandas grouping: df.groupby('key')['value'].agg(['mean','sum'])\]
  5. \[matplotlib plotting: plt.plot(x\]
    \[y)\]
    \[plt.xlabel('X')\]
    \[plt.ylabel('Y')\]
    \[plt.show()\]
  6. \[seaborn histogram: sns.histplot(data=df\]
    \[x='column'\]
    \[bins=20)\]
💻15

Best Practices, Documentation and Error Handling

💻 COMPUTER SCIENCE / IT

Best Practices, Documentation and Error Handling

Key Point: Structure of exception handling: try -> except [SpecificError] -> else -> finally

Overview: Best practices, documentation and error handling together make Python code maintainable, readable and robust. Best practices guide how you write and structure code; documentation explains how to use it; error handling ensures graceful responses to runtime problems.

Best Practices (concise):

  • Follow style conventions — use PEP 8 naming, indentation and line length for readability.
  • Modular design — split code into small functions and modules (single responsibility).
  • Use virtual environments and pin dependencies (requirements.txt or Pipfile) to ensure reproducible environments.
  • Avoid code duplication (DRY) — reuse functions and utilities.
  • Write tests — unit tests for logic, integration tests for components; run tests automatically (CI).
  • Prefer clear names and docstrings — descriptive function/variable names and module/function docstrings.
  • Use version control (git) and semantic versioning for libraries.
  • Use logging instead of print for runtime diagnostics and configurable verbosity.
  • Handle resources safely — use context managers (with) for files, network connections, DB transactions.

Documentation:

  • Docstrings — every module, class and public function should have a docstring describing purpose, parameters, return values and exceptions. Use e.g., Google/Numpy/Sphinx style consistently.
  • README — quick start, installation, examples and license.
  • API Reference — auto-generate with Sphinx or pdoc from docstrings.
  • Examples and tutorials — short runnable examples that show common use-cases.
  • CHANGELOG — document breaking changes and feature additions across versions.
  • Type hints — optional static typing (PEP 484) improves readability and tooling support.

Error Handling:

  • Use try/except/else/finally to catch and handle anticipated errors. Avoid bare except:; catch specific exceptions.
  • Raise informative exceptions with clear messages; create custom exception classes when needed.
  • Clean up resources in finally or use context managers to ensure deterministic cleanup.
  • Log exceptions with traceback for debugging; present user-friendly messages to end users.
  • Fail fast for programming errors (let assertions or exceptions surface) and catch only expected runtime errors (I/O, network, user input).
  • Validate inputs early to avoid deep failures later (defensive programming).
  • Graceful degradation — where possible, provide fallback behaviour rather than crashing.

Why this matters: Well-documented, well-structured code with robust error handling reduces bugs, eases collaboration, speeds onboarding and improves user trust.

Quick example of good structure (HTML-preserved):

<!-- Module: file_utils.py -->
"""
file_utils — small utilities for reading configuration files.

Functions
---------
read_config(path: str) -> dict
    Read JSON config from the given path and return a dict.
"""

import json
import logging
from typing import Dict

logger = logging.getLogger(__name__)

class ConfigError(Exception):
    """Raised when configuration cannot be loaded."""


def read_config(path: str) -> Dict:
    """Read JSON configuration from path.

    Args:
        path: Path to the JSON config file.

    Returns:
        Parsed configuration dictionary.

    Raises:
        ConfigError: If file not found or JSON is invalid.
    """
    try:
        with open(path, 'r', encoding='utf-8') as f:
            return json.load(f)
    except FileNotFoundError as e:
        logger.exception("Config file not found: %s", path)
        raise ConfigError(f"Config file not found: {path}") from e
    except json.JSONDecodeError as e:
        logger.exception("Invalid JSON in config: %s", path)
        raise ConfigError(f"Invalid JSON in config: {path}") from e
📌 Examples
  • 1) Docstring and usage example: """ calculate.py """ def mean(values: list[float]) -> float: """Return arithmetic mean of values. Args: values: non-empty list of numbers Returns: The arithmetic mean as float Raises: ValueError: if values is empty """ if not values: raise ValueError("mean() requires at least one value") return sum(values) / len(values) # Example usage: # >>> mean([1,2,3]) # 2.0
  • 2) Error handling for file I/O with logging: import logging logger = logging.getLogger(__name__) try: with open('data.csv') as f: process(f) except FileNotFoundError: logger.error('data.csv not found; please provide the file') except Exception as e: logger.exception('Unexpected error while processing data: %s', e) raise
  • 3) Custom exception and re-raising: class DatabaseError(Exception): pass try: commit_transaction() except DatabaseConnectionError as e: # wrap low-level error in domain-specific exception raise DatabaseError('Transaction failed: connection lost') from e
  • 4) Using context managers to ensure cleanup: from contextlib import contextmanager @contextmanager def open_db(path): db = connect(path) try: yield db db.commit() except Exception: db.rollback() raise finally: db.close()
🧮 Formulas
  1. \[Structure of exception handling: try -> except [SpecificError] -> else -> finally\]
  2. \[Raise syntax: raise ExceptionType('message') (optionally: raise ... from original_exception)\]
  3. \[Custom exception class: class MyError(Exception): pass\]
  4. \[Logging levels (common): CRITICAL(50) > ERROR(40) > WARNING(30) > INFO(20) > DEBUG(10)\]
  5. \[Type hint style (function signature): def fn(a: int\]
    \[b: str) -> bool:\]

Key Concepts

Library
A collection of prewritten code (modules and packages) that provides reusable functionality to be used in programs.
Module
A single .py file that contains functions, classes, and variables which can be imported into other Python programs.
Package
A directory containing multiple Python modules and a special __init__.py file, used to organize related modules.
Standard Library
The set of modules and packages distributed with Python that provide commonly used functionality without extra installation.
Third-party Library
A library developed outside the Python standard distribution that must be installed (usually via pip) before use.
pip
Python's package installer used to download and install third-party libraries from the Python Package Index (PyPI).
Virtual Environment (venv)
An isolated Python environment that keeps project-specific dependencies separate from system-wide packages.
import statement
A statement used to bring modules or packages into the current namespace so their contents can be used.
from ... import
A form of import that brings specific attributes (functions, classes) from a module into the current namespace.
aliasing (as)
Giving a shorter or different name to an imported module or attribute to simplify references in code.
dir()
A built-in function that lists the attributes (names) defined by a module, class, or object.
help()
A built-in function that displays the documentation (docstring) for modules, functions, classes, or objects.
NumPy
A popular third-party library for numerical computing in Python, providing arrays and mathematical functions.
Pandas
A third-party library for data manipulation and analysis, offering DataFrame and Series data structures.
Matplotlib
A plotting library for creating static, animated, and interactive visualizations in Python.
tkinter
Python's standard GUI (graphical user interface) library for building desktop applications.
requests
A third-party library that simplifies making HTTP requests (GET, POST, etc.) in Python.
json (module)
A standard library module for encoding (dumping) and decoding (loading) JSON data.
os (module)
A standard library module for interacting with the operating system: file paths, environment variables, processes.
sys (module)
A standard library module that provides access to interpreter variables and functions (arguments, exit, path).

Practice Questions

  1. Differentiate between a module and a package in Python with one example each. / पायथन में मॉड्यूल और पैकेज में अंतर बताइए, प्रत्येक का एक उदाहरण दीजिए।
    Show answer

    A module is a single .py file (e.g., math), while a package is a directory of modules marked by an __init__.py file (e.g., the utilities package). / मॉड्यूल एक अकेली .py फ़ाइल है (जैसे math), जबकि पैकेज मॉड्यूलों की निर्देशिका है जिसे __init__.py फ़ाइल चिह्नित करती है (जैसे utilities पैकेज)।

  2. Explain the three common import forms and their effect on the namespace. / तीन सामान्य import रूप और नामस्थान पर उनके प्रभाव को समझाइए।
    Show answer

    import math accesses members as math.name; from math import sqrt brings sqrt directly into the namespace; import numpy as np gives the module an alias. / import math सदस्यों को math.name से एक्सेस करता है; from math import sqrt sqrt को सीधे नामस्थान में लाता है; import numpy as np मॉड्यूल को उपनाम देता है।

  3. What is the purpose of the 'if __name__ == "__main__":' idiom? / 'if __name__ == "__main__":' मुहावरे का उद्देश्य क्या है?
    Show answer

    It lets code run only when the file is executed directly as a script, but not when the file is imported as a module. / यह कोड को केवल तब चलाता है जब फ़ाइल सीधे स्क्रिप्ट के रूप में चलाई जाए, मॉड्यूल के रूप में import करने पर नहीं।

  4. Write code using the math module to compute the area of a circle of radius r and the distance between points (3,4) and (0,0). / त्रिज्या r के वृत्त का क्षेत्रफल और बिंदु (3,4) व (0,0) के बीच दूरी ज्ञात करने के लिए math मॉड्यूल का कोड लिखिए।
    Show answer

    import math; area = math.pi*r*r; d = math.hypot(3,4) gives d = 5.0. / import math; area = math.pi*r*r; d = math.hypot(3,4) से d = 5.0 प्राप्त होता है।

  5. When should you use random.sample() instead of random.choices()? / random.choices() के बजाय random.sample() का प्रयोग कब करना चाहिए?
    Show answer

    Use random.sample(population, k) for sampling without replacement (k unique elements); use random.choices() for sampling with replacement and optional weights. / जब बिना प्रतिस्थापन के k अद्वितीय तत्व चाहिए तब random.sample(population, k), और प्रतिस्थापन सहित व वैकल्पिक भार के लिए random.choices() प्रयोग करें।

  6. Distinguish between statistics.pstdev() and statistics.stdev(). / statistics.pstdev() और statistics.stdev() में अंतर कीजिए।
    Show answer

    pstdev computes population standard deviation (divides by n) for entire-population data; stdev computes sample standard deviation (divides by n-1) for a sample. / pstdev संपूर्ण जनसंख्या के लिए जनसंख्या मानक विचलन (n से विभाजन) देता है; stdev नमूने के लिए नमूना मानक विचलन (n-1 से विभाजन) देता है।

  7. Why is pickle unsafe for untrusted data, and how do json/csv differ in interoperability? / अविश्वसनीय डेटा के लिए pickle असुरक्षित क्यों है, और json/csv अंतर-संचालनीयता में कैसे भिन्न हैं?
    Show answer

    Unpickling untrusted data can execute arbitrary code and is Python-specific; json and csv are text formats safe and interoperable across languages and systems. / अविश्वसनीय डेटा को unpickle करने से मनमाना कोड चल सकता है और यह पायथन-विशिष्ट है; json और csv पाठ प्रारूप हैं जो भाषाओं व सिस्टमों में सुरक्षित व अंतर-संचालनीय हैं।

  8. Write a re pattern to validate a basic email address and explain why raw strings are used. / एक मूल ईमेल पते को सत्यापित करने हेतु re पैटर्न लिखिए और बताइए कि raw strings क्यों प्रयोग होती हैं।
    Show answer

    r"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$"; raw strings (r"...") prevent Python from processing backslash escapes, keeping regex metacharacters intact. / r"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$"; raw strings (r"...") पायथन को बैकस्लैश एस्केप संसाधित करने से रोकती हैं जिससे regex मेटाकैरेक्टर बने रहते हैं।

Related Laws & Principles

Explore all

Foundational laws & principles connected to this chapter — tap to open in the Laws Explorer.

Loading related laws…
Sourced from 225 content files · LLOS Learn · browse all chapters