Log In

Don't have an account? Sign up now

Lost Password?

Sign Up

Prev Next

MODULE 10: Python Standard Libraries

Python comes with a rich Standard Library that allows developers to perform common tasks without installing external packages.

1. datetime Module

Brief

Used to work with dates and time.


Common Classes

  • date
  • time
  • datetime
  • timedelta

Get Current Date & Time

from datetime import datetime

now = datetime.now()
print(now)

Date Formatting

print(now.strftime("%d-%m-%Y %H:%M:%S"))

Date Arithmetic

from datetime import timedelta

future_date = now + timedelta(days=10)
print(future_date)

Real-World Use

  • Attendance systems
  • Logs & timestamps
  • Expiry dates

2. math Module

Brief

Provides mathematical functions.


Common Functions

import math

print(math.sqrt(16))
print(math.pow(2, 3))
print(math.factorial(5))
print(math.ceil(4.3))
print(math.floor(4.9))

Constants

print(math.pi)
print(math.e)

Real-World Use

  • Scientific calculations
  • Finance & engineering apps

3. random Module

Brief

Used to generate random numbers.


Random Integer

import random
print(random.randint(1, 10))

Random Float

print(random.random())

Random Choice

colors = ["red", "blue", "green"]
print(random.choice(colors))

Shuffle List

random.shuffle(colors)
print(colors)

Real-World Use

  • Games
  • OTP generation
  • Simulations

4. os & sys Modules


4.1 os Module

Brief

Used to interact with operating system.


Common Operations

import os

print(os.getcwd())
os.mkdir("test_folder")
os.rename("test_folder", "demo")
os.remove("file.txt")

Environment Variables

print(os.environ)

4.2 sys Module

Brief

Used for system-level operations.


Examples

import sys

print(sys.version)
print(sys.argv)
sys.exit()

Real-World Use

  • Automation scripts
  • CLI tools

5. shutil Module

Brief

Used for high-level file operations.


Copy Files

import shutil

shutil.copy("source.txt", "destination.txt")

Copy Directories

shutil.copytree("src", "backup")

Delete Directory

shutil.rmtree("backup")

Real-World Use

  • Backup automation
  • File management systems

6. re (Regular Expressions)

Brief

Used for pattern matching and text processing.


Common Functions

  • findall()
  • search()
  • match()
  • sub()

Find Pattern

import re

text = "My number is 9876543210"
result = re.findall(r"\d+", text)
print(result)

Search Pattern

if re.search(r"\d{10}", text):
    print("Valid phone number")

Replace Text

data = "Python is easy"
print(re.sub("easy", "powerful", data))

Real-World Use

  • Form validation
  • Data cleaning
  • Log analysis

7. collections Module

Brief

Provides specialized data structures.


Counter

from collections import Counter

nums = [1, 2, 2, 3, 3, 3]
print(Counter(nums))

defaultdict

from collections import defaultdict

d = defaultdict(int)
d["a"] += 1
print(d)

deque

from collections import deque

dq = deque([1, 2, 3])
dq.appendleft(0)
dq.pop()
print(dq)

Real-World Use

  • Frequency counting
  • Queue & stack operations

8. itertools Module

Brief

Provides fast, memory-efficient iterators.


count()

from itertools import count

for i in count(1):
    if i == 5:
        break
    print(i)

cycle()

from itertools import cycle

colors = ["red", "blue"]
for color in cycle(colors):
    print(color)
    break

combinations()

from itertools import combinations

nums = [1, 2, 3]
print(list(combinations(nums, 2)))

Leave a Comment

    🚀 Join Common Jobs Pro — Referrals & Profile Visibility Join Now ×
    🔥