06 20241021 LibraryModules
06 20241021 LibraryModules
Key Functions:
• random(): Generates a random float number between 0 and 1.
• randint(a, b): Generates a random integer between a and b.
• choice(seq): Returns a random element from a sequence.
• shuffle(list): Shuffles the elements of a list.
• uniform(a, b): Generates a random float between a and b.
Python Library Modules
1. 'random' Module:
Example
import random
Key Functions:
• sqrt(x) : Returns the square root of x.
• sin(x), cos(x), tan(x) : Trigonometric functions.
• factorial(x) : Returns the factorial of x.
• pi and e : Mathematical constants.
Python Library Modules
1. 'math' Module:
Example
import math
print(math.pi) # Value of Pi
print(math.sqrt(16)) # Square root of 16
print(math.factorial(5)) # Factorial of 5
Python Library Modules
1. 'time' Module:
• The time module allows you to work with time in Python.
• time module provides various functions for manipulating time
intervals and formats.
Key Functions:
• time(): Returns the current time in seconds since the epoch.
• sleep(seconds): Pauses the program for seconds.
• ctime(): Converts a time value to a human-readable string.
Python Library Modules
1. 'time' Module:
Example
import time
Key Functions:
• getcwd(): Returns the current working directory.
• listdir(path): Returns a list of files in the directory.
• remove(path): Deletes a file at the given path.
• mkdir(path): Creates a new directory.
Python Library Modules
1. 'os' Module:
Example
import os
Key Functions:
• copy(src, dst): Copies a file from src (source) to dst (destination).
• move(src, dst): Moves a file or directory from src to dst.
• rmtree(path): Deletes an entire directory tree.
Python Library Modules
5. 'shutil' Module:
Example
import shutil
Key Functions:
• argv: List of command-line arguments passed to a script.
• exit(): Exits the Python interpreter.
• platform: Identifies the current platform (Windows, Linux, etc.).
Python Library Modules
6. 'sys' Module:
Example
import sys
Key Functions:
• glob(pattern): Returns a list of file paths that match the pattern.
Python Library Modules
7. 'glob' Module:
Example
import glob
Key Functions:
• search(pattern, string): Searches for a pattern in a string.
• findall(pattern, string): Finds all occurrences of a pattern.
• sub(pattern, repl, string): Replaces occurrences of a pattern
with repl.
Python Library Modules
8. 're' Module:
Example
import re
Key Functions:
• mean(data): Returns the arithmetic mean.
Example
import statistics
data = [1, 2, 3, 4, 5, 6, 7]
print(statistics.mean(data)) # Mean
print(statistics.median(data)) # Median
print(statistics.variance(data)) # Variance
Python Library Modules
10. Creating a Custom Module:
• Python allows you to create your own custom modules.
• A module is simply a Python file (.py) containing functions and
variables.
Steps:
• Create a Python file with the required functions and variables.
Example:
Create a module (mymodule.py)
def greet(name):
return f"Hello, {name}!"