0% found this document useful (0 votes)
22 views13 pages

Python Modules, Packages, and File Handling

This document provides comprehensive notes on Python modules, packages, standard libraries, and file handling. It covers user-defined modules, standard library modules like random, NumPy, and SciPy, as well as file handling techniques including creating, opening, and renaming files. Additionally, it discusses regular expressions and various built-in functions for string and mathematical operations.

Uploaded by

xdfggyz1234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views13 pages

Python Modules, Packages, and File Handling

This document provides comprehensive notes on Python modules, packages, standard libraries, and file handling. It covers user-defined modules, standard library modules like random, NumPy, and SciPy, as well as file handling techniques including creating, opening, and renaming files. Additionally, it discusses regular expressions and various built-in functions for string and mathematical operations.

Uploaded by

xdfggyz1234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Notes: Modules, Packages, Standard

Library, and File Handling


Table of Contents
1. User-Defined Modules and Packages
2. Standard Library Modules
o random Module
o NumPy random
o SciPy Overview
o sys Module
o math Module
o string Module
o List Methods
o Date & Time Module
3. Regular Expressions
4. File Handling
o Introduction
o File Types
o Creating Files
o Opening and Closing Files
o Renaming Files
o Accessing Files
o Deleting Files
o File Pointers
o File Modes
o Binary Files

User-Defined Modules and Packages {#user-defined-


modules-and-packages}
Definition and Purpose

A module is a file containing Python definitions and statements, with the file name serving as the
module name (appended with .py). Modules enable code reuse, organization, and maintenance by
allowing programs to be split into multiple files.

 Purpose:
o Reuse functions/classes across programs without duplication.
o Organize code logically.
o Avoid namespace pollution in the main script.
Creating a User-Defined Module

Create a .py file with functions, classes, or variables. Example: [Link]

python
# [Link] - Fibonacci numbers module
def fib(n):
"""Write Fibonacci series up to n."""
a, b = 0, 1
while a < n:
print(a, end=' ')
a, b = b, a + b
print()

def fib2(n):
"""Return Fibonacci series up to n as a list."""
result = []
a, b = 0, 1
while a < n:
[Link](a)
a, b = b, a + b
return result

if __name__ == "__main__":
# Test the module
fib(1000)

 The __name__ variable is '__main__' when run directly, but 'fibo' when imported.

Importing Modules

Use import statements at the top of your script.

 Basic Import:

python

 import fibo
[Link](1000) # Output: 0 1 1 2 3 5 8 ... 987
 Import Specific Names:
python
 from fibo import fib, fib2
fib(500) # Direct access without prefix
 Import All (Avoid in Production):
python
 from fibo import *
fib2(100) # [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
 As Alias:
python
 import fibo as fib
 [Link](500)

Modules are imported once per session for efficiency. To reload: import importlib;
[Link](fibo).

Packages

A package is a directory containing modules and an __init__.py file (can be empty).

 Structure Example:

text

 mypackage/
├── __init__.py
├── [Link]
└── [Link]
 Importing from Package:
python
 from mypackage import module1
 module1.some_function()
 Subpackages: Nested directories with their own __init__.py.

Packages allow hierarchical organization of modules.

Standard Library Modules {#standard-library-modules}


random Module {#random-module}
The random module generates pseudo-random numbers using the Mersenne Twister algorithm.
Not suitable for cryptography (use secrets instead).

Key Functions

Return
Function Description Parameters Example
Type

Initializes the RNG a: Seed value (int, [Link](42);


seed(a=None,
with a seed for str, etc.); version: None print([Link]()) #
version=2)
reproducibility. 1 or 2. Consistent output

Random integer N a, b: Bounds


randint(a, b) int [Link](1, 10) # e.g., 7
where a ≤ N ≤ b. (inclusive).
randrange(stop) or Random element
[Link](0, 101, 2) #
randrange(start, stop[, from range(start, Similar to range(). int
Even number 0-100
step]) stop, step).
Random element
seq: List, tuple, Element [Link](['a', 'b', 'c']) #
choice(seq) from non-empty
etc. from seq e.g., 'b'
sequence.
population:
choices(population, k weighted samples Sequence; List of k [Link](['a', 'b'],
weights=None, k=1) with replacement. weights: List of elements weights=[10, 1], k=5)
weights.
lst = [1,2,3];
Shuffles list x in x: Mutable
shuffle(x) None [Link](lst); print(lst)
place. sequence.
# e.g., [3,1,2]
Float uniformly in [Link]() # e.g.,
random() None float
[0.0, 1.0). 0.37444887175646646
Integer with k [Link](4) # e.g.,
getrandbits(k) k: Number of bits. int
random bits. 13 (1101 in binary)

 State Management: getstate() and setstate() to save/restore RNG state.

NumPy random {#numpy-random}

NumPy's [Link] provides efficient random sampling for arrays, using Generator
(preferred over legacy RandomState).

Key Components

 Generator: Create with default_rng(seed=None).

python
 import numpy as np
 rng = [Link].default_rng(42)

Distributions and Utilities

Function Description Parameters Example


Uniform [0,1) size: Shape of [Link](3) # array([0.77395605,
random(size=None)
floats. output array. 0.461479, 0.438127])

integers(low, high, Uniform integers low, high: Bounds; [Link](0, 10, 5) # e.g., [5 3 7 2
size=None) [low, high). size: Shape. 9]

Gaussian (mean=0, rng.standard_normal(3) # e.g., [-


standard_normal(size=None) size: Shape.
std=1). 1.137 0.518 0.185]

 Seeding: Use large seeds like [Link](128) for uniqueness.


 Use Case: Simulations, statistical modeling.

SciPy Overview {#scipy-overview}

SciPy builds on NumPy for scientific computing. Key submodules:

 integrate: Numerical integration (e.g., quad for definite integrals).


 optimize: Optimization (e.g., minimize for function minimization).
 stats: Statistical functions/distributions (e.g., [Link] for normal PDF).
 linalg: Linear algebra (e.g., solve for systems of equations).

Example:

python
from scipy import integrate
result = [Link](lambda x: x**2, 0, 1) # ∫x² dx from 0 to 1 = 0.333...
print(result) # (0.3333333333333333, 3.70074341541756e-18)

SciPy is ideal for advanced computations like signal processing and interpolation.

sys Module {#sys-module}

Provides access to system-specific parameters and functions.

Key Attributes and Functions


Item Description Example

[Link] Module search path (list of strings). [Link]('/custom/path')

[Link] Command-line arguments (list). print([Link][1:]) # Script args

[Link](arg) Exit interpreter (raises SystemExit). [Link](0) # Success

[Link] Standard input file object. for line in [Link]: print([Link]())

[Link] Platform identifier (e.g., 'linux'). if [Link]('win'): print('Windows')

 Usage: Script arguments, path management, platform checks.

math Module {#math-module}

Mathematical functions for floats.

Constants

Constant Value Example


pi 3.14159... [Link]
e 2.71828... math.e
tau 6.28318... (2π) [Link]
inf Positive infinity [Link]
nan Not a number [Link]([Link]) # True

Trigonometric Functions

Function Description Parameters Example


acos(x) Arc cosine (radians, [0, π]). x: [-1,1] [Link](0) # π/2 ≈ 1.57
asin(x) Arc sine (radians, [-π/2, π/2]). x: [-1,1] [Link](0) # 0.0
atan(x) Arc tangent (radians, [-π/2, π/2]). x: Any float [Link](1) # π/4 ≈ 0.785
atan2(y, x) Arc tangent of y/x (radians, [-π, π]). y, x: Coordinates math.atan2(1,1) # π/4
cos(x) Cosine (radians). x: Angle [Link](0) # 1.0
sin(x) Sine (radians). x: Angle [Link]([Link]/2) # 1.0

Other Categories

 Hyperbolic: cosh(x), sinh(x), tanh(x).


 Logarithmic: log(x, base=e), log2(x), log10(x).
 Power: pow(x, y), sqrt(x), exp(x).
Example:

python
import math
print([Link](16)) # 4.0
print([Link](8, 2)) # 3.0

string Module {#string-module}

Provides string constants and utilities.

Constants

 ascii_letters: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
 digits: '0123456789'

Example: import string; print([Link]) # 0123456789

Template Class

For simple substitutions:

python
from string import Template
t = Template('$who likes $what')
print([Link](who='Tim', what='kung pao')) # Tim likes kung pao
print(t.safe_substitute(who='Tim')) # Tim likes $what (safe for missing keys)

Functions

 capwords(s, sep=None): Capitalizes words.

python

 import string
 print([Link]('hello world')) # Hello World

List Methods {#list-methods}

Lists are mutable sequences. Key methods:

Method Description Example


append(value) Add to end. [1,2].append(3) # [1,2,3]
Method Description Example
extend(iterable) Add multiple to end. [1,2].extend([3,4]) # [1,2,3,4]
insert(index, value) Insert at index. [1,3].insert(1,2) # [1,2,3]
remove(value) Remove first occurrence. [1,2,2].remove(2) # [1,2]
pop(index=-1) Remove/return at index. lst=[1,2]; [Link]() # 2, lst=[1]
index(value) First index of value. [1,2,2].index(2) # 1
count(value) Occurrences of value. [1,2,2].count(2) # 2
sort(key=None, reverse=False) Sort in place. [3,1].sort() # [1,3]
reverse() Reverse in place. [1,2,3].reverse() # [3,2,1]
clear() Remove all. [1,2].clear() # []
copy() Shallow copy. lst=[1,2]; new=[Link]() # [1,2]

Date & Time Module {#date-time-module}

The datetime module handles dates, times, and durations.

Classes

Class Description Constructor Example


date Year, month, day. date(year, month, day) date(2025,9,18)

time Hour, minute, second, microsecond. time(hour=0, ...) time(12,30)

datetime Date + time. datetime(year, month, day, hour=0, ...) [Link]()

timedelta Duration. timedelta(days=1, hours=2) dt + timedelta(days=7)

Functions

 today() / now(): Current date/datetime.


 strftime(format): Format as string (e.g., %Y-%m-%d).
 strptime(string, format): Parse string to datetime.

Example:

python
from datetime import datetime, timedelta
now = [Link]()
print([Link]("%Y-%m-%d %H:%M")) # 2025-09-18 12:00
future = now + timedelta(days=30)
print(future)

Supports arithmetic (e.g., subtraction for timedelta) and comparisons.

Regular Expressions {#regular-expressions}


The re module supports pattern matching on strings.

Basics

 Patterns: Use raw strings r'pattern'. Special chars: . (any), ^ (start), $ (end), * (0+), + (1+),
? (0/1), {m,n} (repeats), [] (class), | (or), () (group).
 Flags: [Link], [Link], [Link].
 Groups: () for capture; (?P<name>...) for named.

Key Functions

Function Description Parameters Example


match(pattern, [Link](r'\d+',
Match at start. pattern, string.
string) '123abc').group() # '123'
search(pattern, [Link](r'abc',
First match anywhere. Same.
string) 'xyzabc').group() # 'abc'
findall(pattern, All non-overlapping [Link](r'\d+', 'a1b2c3') #
Same.
string) matches. ['1','2','3']
sub(pattern, repl, repl: String or
Replace matches. [Link](r'\d', 'X', 'a1b2') # 'aXbX'
string) func.

Example:

python
import re
text = "Contact: alice@[Link] or bob@[Link]"
emails = [Link](r'\w+@\w+\.\w+', text)
print(emails) # ['alice@[Link]', 'bob@[Link]']

# Replace with groups


[Link](r'(\w+)@(\w+)\.(\w+)', r'\2.\3', text) # '[Link] or [Link]'

 Match Object: [Link](), [Link](), [Link](), [Link]().


File Handling {#file-handling}
Introduction {#file-handling-introduction}

File handling involves creating, reading, writing, and managing files. Use open() to access files,
which returns a file object.

File Types {#file-types}

 Text Files (.txt, .csv): Human-readable, line-terminated by \n. CSV: Comma-separated for
tabular data.
o Example CSV Read:

python


o import csv
o with open('[Link]', 'r') as f:
o reader = [Link](f)
o for row in reader:
o print(row)
 Binary Files (.jpg, .exe): Non-text, machine-readable (0s/1s). No line endings; use 'b'
mode.

Creating Files {#creating-files}

Opening in write mode ('w' or 'wb') creates if non-existent.

python
with open('[Link]', 'w') as f:
[Link]('Hello, World!\n')

Opening and Closing Files {#opening-closing-files}

 open(filename, mode='r', encoding=None): Returns file object.


 Always use with for auto-closing.

python
with open('[Link]', 'r', encoding='utf-8') as f:
content = [Link]()
# f is closed here

 Manual: f = open(...); [Link]() (risky without try-finally).

Renaming Files {#renaming-files}

Use [Link](src, dst).

python
import os
[Link]('[Link]', '[Link]')

Accessing Files {#accessing-files}

Reading/Writing:

 read(size=-1): All or size bytes/chars.


 readline(): One line.
 write(string): Write content.

python
with open('[Link]', 'r') as f:
print([Link]()) # First line
with open('[Link]', 'a') as f:
[Link]('Append line\n')

Deleting Files {#deleting-files}

Use [Link](path).

python
import os
if [Link]('[Link]'):
[Link]('[Link]')

For directories: [Link](empty_dir) or [Link](non_empty).

File Pointers {#file-pointers}

 tell(): Current position (bytes from start).


 seek(offset, whence=0): Move pointer.
o whence=0: From start.
o whence=1: From current.
o whence=2: From end.

Example:

python
with open('[Link]', 'r') as f:
print([Link]()) # 0
[Link](5)
print([Link]()) # 5
[Link](0) # Back to start
print([Link](5)) # First 5 chars

In text mode, only certain seeks work; use binary for full control.

File Modes {#file-modes}

Mode Description
'r' Read (default).
'w' Write (truncate if exists).
'a' Append.
'r+' Read+write.
'b' Binary (e.g., 'rb', 'wb').
't' Text (default).

Binary Files {#binary-files}

Open with 'b' mode; read/write bytes.

python
with open('[Link]', 'rb') as f:
data = [Link]() # bytes
with open('[Link]', 'wb') as f:
[Link](data)

 Use pickle for serializing objects:

python

import pickle
data = {'key': 'value'}
with open('[Link]', 'wb') as f:
[Link](data, f)
with open('[Link]', 'rb') as f:
loaded = [Link](f)

Common questions

Powered by AI

Seeding in random number generation is crucial for reproducibility. In Python's random module, 'seed()' ensures that the same sequence of numbers can be regenerated by starting with the same initial conditions. Similarly, NumPy's 'default_rng(seed)' in its random module accomplishes this in the context of arrays. This is particularly important in simulations and testing, where reproducibility is needed for consistent results across different runs .

The __init__.py file in a Python package directory indicates that the directory should be treated as a package. This file can be used for package initialization, defining the available modules, and executing package-level code upon import. Its presence is crucial for Python to differentiate between a package directory and a regular directory, specifically influencing how modules are imported from the package .

The random module in Python utilizes the Mersenne Twister algorithm to generate pseudo-random numbers, which is efficient for most simulations and sampling needs. However, it is not suitable for cryptographic purposes due to its predictable nature when the seed is known. For cryptographic security, Python's 'secrets' module should be used, as it generates numbers that are more secure .

Regular expressions in Python provide a powerful means of searching, modifying, and parsing strings. They enable complex pattern matching and replacement, greatly simplifying tasks like data extraction and validation. Functions such as 'match', 'search', and 'sub' facilitate these operations, allowing efficient manipulation of text. This capability is essential in processing natural language data and other administrative tasks involving text .

Python's 'sys' module provides functionalities for interacting with system-specific parameters and functions. For platform-specific operations, it allows checking the current platform with 'sys.platform', enabling conditional code execution based on the environment (e.g., differentiating between Windows vs. Linux operations). Other common uses include managing the system path for module imports and handling script arguments with 'sys.argv' .

User-defined modules in Python serve to reuse functions and classes across different programs, organize code logically, and avoid namespace pollution in the main script. By splitting code into multiple files, these modules enhance maintainability and readability .

The 'integrate' submodule of SciPy focuses on numerical integration, providing functions like 'quad' for computing definite integrals, which is useful in solving differential equations and evaluating areas under curves. On the other hand, the 'optimize' submodule assists with finding minima, maxima, and zeros of functions using methods like 'minimize'. It is essential in optimization tasks across scientific computing, including parameter tuning and resource allocation. Both submodules leverage powerful mathematical algorithms distinctly suited for their specific problems .

The numpy.random Generator is an improvement over the legacy RandomState in terms of both design and capability. It offers a more flexible interface and better performance for handling a wide variety of statistical distributions. The Generator supports broader functionality such as the random operation of arrays and is generally preferred for new code bases due to its efficiency and modernized API .

Python's file handling capabilities allow management of text and binary file types using different access methods. Text files can be accessed with modes like 'r' for reading and 'w' for writing, including special support for CSV files using the 'csv' module. Binary files require 'b' mode for operations. The 'open()' function returns a file object, which can be used with methods like 'read()' and 'write()' depending on the mode, ensuring flexibility in handling various file types .

Python's 'string' module provides string constants like 'ascii_letters' and 'digits', facilitating operations that depend on character classifications. For simple substitutions, the 'Template' class is used, allowing placeholders to be replaced with variables through 'substitute()' or 'safe_substitute()'. This feature is useful for templating text output where you need variable substitution without dealing with full formatting complexity of other Python string methods .

You might also like