0% found this document useful (0 votes)
5 views6 pages

Python Working With Libraries Tutorial

This document is a step-by-step tutorial on using Python libraries such as math, random, datetime, and itertools, providing detailed examples for each. It covers mathematical operations, random number generation, date and time manipulation, and advanced iterator functions. Additionally, it includes a real-world mini project example for generating lottery numbers with timestamps and combinations.

Uploaded by

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

Python Working With Libraries Tutorial

This document is a step-by-step tutorial on using Python libraries such as math, random, datetime, and itertools, providing detailed examples for each. It covers mathematical operations, random number generation, date and time manipulation, and advanced iterator functions. Additionally, it includes a real-world mini project example for generating lottery numbers with timestamps and combinations.

Uploaded by

sittong.10.24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

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(math.pi) # 3.141592653589793
print(math.e) # 2.718281828459045

🔸 Basic Functions:

print(math.sqrt(16)) # 4.0
print(math.pow(2, 3)) # 8.0 (float result)
print(math.factorial(5)) # 120
print(math.fabs(-4.5)) # 4.5 (absolute value)

🔸 Rounding:

print(math.floor(4.7)) #4
print(math.ceil(4.3)) #5
print(math.trunc(3.99)) #3

🔸 Logarithmic Functions:

print(math.log(100, 10)) # 2.0


print(math.log2(8)) # 3.0
print(math.log10(1000)) # 3.0

🔸 Trigonometry:

print(math.sin(math.pi / 2)) # 1.0


print(math.cos(0)) # 1.0
print(math.tan(math.pi / 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(random.random()) # Float between 0.0 and 1.0


print(random.uniform(1, 10)) # Float between 1 and 10
print(random.randint(1, 100)) # Integer between 1 and 100 (inclusive)

🔸 Choice and Shuffle:

items = ['apple', 'banana', 'cherry']


print(random.choice(items)) # Random element
random.shuffle(items)
print(items) # List is shuffled

🔸 Sample:

print(random.sample(range(1, 50), 6)) # List of 6 unique numbers

🔸 Seeding:

random.seed(5)
print(random.random()) # 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 = datetime.datetime.now()
print(now)
print(now.strftime("%Y-%m-%d %H:%M:%S"))

🔸 Create Custom Date:

dt = datetime.datetime(2025, 6, 24, 14, 30)


print(dt.strftime("%A, %d %B %Y"))

🔸 Date Arithmetic:

from datetime import timedelta


today = datetime.date.today()
yesterday = today - timedelta(days=1)
tomorrow = today + timedelta(days=1)
print("Yesterday:", yesterday)
print("Tomorrow:", tomorrow)

🔸 Time Difference:

d1 = datetime.datetime(2025, 6, 24, 12, 0)


d2 = datetime.datetime(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 itertools.count(10, 2):
print(i)
if i > 20:
break
# cycle()
for i, item in zip(range(5), itertools.cycle(['A', 'B'])):
print(item)

# repeat()
for i in itertools.repeat('Hello', 3):
print(i)

🔸 product(), permutations(), combinations():

# Cartesian product
print(list(itertools.product([1, 2], ['a', 'b'])))

# Permutations
print(list(itertools.permutations([1, 2, 3], 2)))

# Combinations
print(list(itertools.combinations([1, 2, 3], 2)))

🔸 accumulate():

import operator
data = [1, 2, 3, 4]
print(list(itertools.accumulate(data))) # Cumulative sum
print(list(itertools.accumulate(data, operator.mul))) # Cumulative product

🔸 groupby():

data = [('apple', 1), ('apple', 2), ('banana', 3), ('banana', 4)]


grouped = itertools.groupby(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(random.sample(range(1, 50), 6))
timestamp = datetime.datetime.now().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(itertools.combinations(lottery_numbers, 2))
print("Pairs:", pairs)

You might also like