Python Working with Libraries – Step-
by-Step Tutorial
Complete Micro-Level Detailed Tutorial with Examples
Prepared for: Satyajit Mukherjee
1. math Library
🔹 Purpose: Used for mathematical operations like trigonometry, logarithms, constants,
rounding, etc.
✅ Step-by-step Concepts with Examples:
🔸 Importing the Library:
import math
🔸 Constants:
print([Link]) # 3.141592653589793
print(math.e) # 2.718281828459045
🔸 Basic Functions:
print([Link](16)) # 4.0
print([Link](2, 3)) # 8.0 (float result)
print([Link](5)) # 120
print([Link](-4.5)) # 4.5 (absolute value)
🔸 Rounding:
print([Link](4.7)) #4
print([Link](4.3)) #5
print([Link](3.99)) #3
🔸 Logarithmic Functions:
print([Link](100, 10)) # 2.0
print(math.log2(8)) # 3.0
print(math.log10(1000)) # 3.0
🔸 Trigonometry:
print([Link]([Link] / 2)) # 1.0
print([Link](0)) # 1.0
print([Link]([Link] / 4)) # 1.0
2. random Library
🔹 Purpose: Used to generate random numbers, shuffle data, and select elements randomly.
✅ Step-by-step Concepts with Examples:
🔸 Importing:
import random
🔸 Random Numbers:
print([Link]()) # Float between 0.0 and 1.0
print([Link](1, 10)) # Float between 1 and 10
print([Link](1, 100)) # Integer between 1 and 100 (inclusive)
🔸 Choice and Shuffle:
items = ['apple', 'banana', 'cherry']
print([Link](items)) # Random element
[Link](items)
print(items) # List is shuffled
🔸 Sample:
print([Link](range(1, 50), 6)) # List of 6 unique numbers
🔸 Seeding:
[Link](5)
print([Link]()) # Same result every time with seed
3. datetime Library
🔹 Purpose: Work with date and time formats, manipulate and format date/time objects.
✅ Step-by-step Concepts with Examples:
🔸 Importing:
import datetime
🔸 Current Date and Time:
now = [Link]()
print(now)
print([Link]("%Y-%m-%d %H:%M:%S"))
🔸 Create Custom Date:
dt = [Link](2025, 6, 24, 14, 30)
print([Link]("%A, %d %B %Y"))
🔸 Date Arithmetic:
from datetime import timedelta
today = [Link]()
yesterday = today - timedelta(days=1)
tomorrow = today + timedelta(days=1)
print("Yesterday:", yesterday)
print("Tomorrow:", tomorrow)
🔸 Time Difference:
d1 = [Link](2025, 6, 24, 12, 0)
d2 = [Link](2025, 6, 24, 15, 30)
diff = d2 - d1
print(diff) # 3:30:00
print(diff.total_seconds()) # 12600.0
4. itertools Library
🔹 Purpose: Efficient looping tools and advanced iterator functions.
✅ Step-by-step Concepts with Examples:
🔸 Importing:
import itertools
🔸 count(), cycle(), repeat():
# count()
for i in [Link](10, 2):
print(i)
if i > 20:
break
# cycle()
for i, item in zip(range(5), [Link](['A', 'B'])):
print(item)
# repeat()
for i in [Link]('Hello', 3):
print(i)
🔸 product(), permutations(), combinations():
# Cartesian product
print(list([Link]([1, 2], ['a', 'b'])))
# Permutations
print(list([Link]([1, 2, 3], 2)))
# Combinations
print(list([Link]([1, 2, 3], 2)))
🔸 accumulate():
import operator
data = [1, 2, 3, 4]
print(list([Link](data))) # Cumulative sum
print(list([Link](data, [Link]))) # Cumulative product
🔸 groupby():
data = [('apple', 1), ('apple', 2), ('banana', 3), ('banana', 4)]
grouped = [Link](data, key=lambda x: x[0])
for key, group in grouped:
print(key, list(group))
Real-World Mini Project Example
🎯 Task: Generate Lottery Numbers with Timestamp and Store Combinations
import random
import datetime
import itertools
# Generate 6 unique numbers between 1 to 49
lottery_numbers = sorted([Link](range(1, 50), 6))
timestamp = [Link]().strftime("%Y-%m-%d %H:%M:%S")
print(f"Lottery Numbers: {lottery_numbers}")
print(f"Generated On: {timestamp}")
# Generate all 2-number combinations
pairs = list([Link](lottery_numbers, 2))
print("Pairs:", pairs)