0% found this document useful (0 votes)
9 views26 pages

06 20241021 LibraryModules

The document outlines various Python library modules that provide pre-written code for common programming tasks, including modules such as random, math, time, os, shutil, sys, glob, re, and statistics. Each module is described with key functions and examples of usage, demonstrating how they can be utilized to enhance Python's functionality. Additionally, it explains how to create custom modules for specific needs.

Uploaded by

scs623170
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)
9 views26 pages

06 20241021 LibraryModules

The document outlines various Python library modules that provide pre-written code for common programming tasks, including modules such as random, math, time, os, shutil, sys, glob, re, and statistics. Each module is described with key functions and examples of usage, demonstrating how they can be utilized to enhance Python's functionality. Additionally, it explains how to create custom modules for specific needs.

Uploaded by

scs623170
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
You are on page 1/ 26

Python Library Modules

Prof. Murali Krishna Gurram


Dept. of Geo-Engineering & RDT
Centre for Remote Sensing, AUCE
Andhra University, Visakhapatnam – 530 003
Dt. 21/10/2024
Basic Elements of Python

Python Library Modules


a) Random
b) Math
c) Time
d) OS
e) Shutil
f) Sys
g) Glob
h) Re
i) Statistics
j) Creating a Custom module
Python Library Modules
Python Library Modules
Library Modules in Python:
 In Python, Library Modules are pre-written code files that
contain functions, classes, and variables.

 Modules help perform specific tasks or solve common


programming problems, so developers don’t need to write code
from scratch.

 By using library modules, you can access various functionalities


like file handling, mathematical operations, system
interactions, and more.

 By importing these modules into your program, you can extend


Python’s built-in functionality to suit a wide range of tasks.
Python Library Modules
Important Library Modules in Python:

 random: Generate random numbers and choices.

 math: Perform mathematical operations.


 time: Manipulate time values and delays.os: Interact with the
operating system.
 shutil: Perform high-level file operations.sys: Access system
parameters and functions.
 glob: Search for file patterns.re: Work with regular expressions.
 statistics: Perform statistical operations.
 Custom Modules: Create and import your own Python modules.
Python Library Modules
1. 'random' Module:
• The random module provides functions for generating random
numbers and performing random operations.

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

print(random.random()) # Random float between 0 and 1


print(random.randint(10, 50)) # Random integer between 10 and 50
my_list = [1, 2, 3, 4, 5]
random.shuffle(my_list) # Shuffling the list
print(my_list)
Python Library Modules
1. 'math' Module:
• The math module provides mathematical functions and
constants like pi (π), e (e), square root, and trigonometric
functions.

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

print(time.time()) # Current time in seconds


time.sleep(2) # Sleep for 2 seconds
print(time.ctime()) # Current time in a readable format
Python Library Modules
1. 'os' Module:
• The os module provides functions to interact with the operating
system.

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

print(os.getcwd()) # Current working directory


os.mkdir('new_folder') # Create a new folder
print(os.listdir('.')) # List files in the current directory
Python Library Modules
5. 'shutil' Module:
• The shutil module offers high-level file operations such as
copying and removing files or directories.

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

shutil.copy('source.txt', 'destination.txt') # Copy file


shutil.move('source.txt', 'new_location/source.txt') # Move file
Python Library Modules
6. 'sys' Module:
• The sys module provides access to system-specific parameters
and functions.

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

print(sys.argv) # Command-line arguments


sys.exit() # Exit the program
Python Library Modules
7. 'glob' Module:
• The glob module is used to find file names matching a pattern.

Key Functions:
• glob(pattern): Returns a list of file paths that match the pattern.
Python Library Modules
7. 'glob' Module:

Example
import glob

files = glob.glob('*.txt') # Get all .txt files in the current directory


print(files)
Python Library Modules
8. 're' Module:
• The re module handles regular expressions, enabling pattern
matching in strings.

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

text = "Python is amazing. I love Python."


matches = re.findall(r'Python', text) # Find all occurrences of 'Python'
print(matches)
new_text = re.sub(r'Python', 'Java', text) # Replace 'Python' with 'Java'
print(new_text)
Python Library Modules
9. 'statistics' Module:
• The statistics module provides functions for mathematical
statistics on numerical data.

Key Functions:
• mean(data): Returns the arithmetic mean.

• median(data): Returns the median.

• variance(data): Returns the variance of data.


Python Library Modules
9. 'statistics' Module:

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.

• Import it into another Python file using the import statement.


Python Library Modules
10. Creating a Custom Module:

Example:
Create a module (mymodule.py)
def greet(name):
return f"Hello, {name}!"

Use the module in another file


import mymodule

print(mymodule.greet('Alice')) # Output: Hello, Alice!


Q&A

You might also like