-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path19.Standard_Library_Overview.py
More file actions
223 lines (175 loc) Β· 6.71 KB
/
19.Standard_Library_Overview.py
File metadata and controls
223 lines (175 loc) Β· 6.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
"""
===========================================================
π PYTHON STANDARD LIBRARY β COMPLETE NOTES
===========================================================
The Python Standard Library is a collection of modules and packages
that are included with every Python installation. It provides ready-to-use
functions and classes for tasks like math, date/time manipulation,
file I/O, networking, concurrency, data persistence, and more.
This makes Python "batteries-included" πͺ«π β you can do a lot without installing anything extra.
-----------------------------------------------------------
π SYNTAX TO IMPORT MODULES
-----------------------------------------------------------
import module_name
import module_name as alias
from module_name import function_name
from module_name import * # Not recommended (pollutes namespace)
Example:
import math
print(math.sqrt(16))
from math import sqrt
print(sqrt(16))
"""
# ===========================================================
# 1οΈβ£ MATH MODULE β MATHEMATICAL OPERATIONS
# ===========================================================
import math
print("π Math Module Examples:")
print("Square root:", math.sqrt(25))
print("Ceil:", math.ceil(3.4))
print("Floor:", math.floor(3.4))
print("Factorial:", math.factorial(5))
print("Power:", math.pow(2, 3))
print("Pi constant:", math.pi)
print("-" * 60)
# ===========================================================
# 2οΈβ£ DATETIME MODULE β DATE AND TIME
# ===========================================================
import datetime
print("π Datetime Module Examples:")
today = datetime.date.today()
print("Today's date:", today)
print("Current year:", today.year)
print("Current month:", today.month)
print("Current weekday:", today.weekday())
now = datetime.datetime.now()
print("Current datetime:", now)
future = now + datetime.timedelta(days=10)
print("10 days from now:", future)
print("-" * 60)
# ===========================================================
# 3οΈβ£ CALENDAR MODULE β WORKING WITH CALENDARS
# ===========================================================
import calendar
print("π Calendar Module Examples:")
print(calendar.month(2025, 9))
print("Is 2024 a leap year?", calendar.isleap(2024))
print("Week header:", calendar.weekheader(3))
print("-" * 60)
# ===========================================================
# 4οΈβ£ RANDOM MODULE β GENERATE RANDOM NUMBERS
# ===========================================================
import random
print("π Random Module Examples:")
print("Random number [0,1):", random.random())
print("Random integer [1,10]:", random.randint(1, 10))
print("Random choice from list:", random.choice(['apple', 'banana', 'cherry']))
my_list = [1, 2, 3, 4, 5]
random.shuffle(my_list)
print("Shuffled list:", my_list)
print("Random sample:", random.sample(my_list, 3))
print("-" * 60)
# ===========================================================
# 5οΈβ£ STATISTICS MODULE β BASIC STATS
# ===========================================================
import statistics
data = [10, 20, 30, 40, 50]
print("π Statistics Module Examples:")
print("Mean:", statistics.mean(data))
print("Median:", statistics.median(data))
print("Variance:", statistics.variance(data))
print("-" * 60)
# ===========================================================
# 6οΈβ£ OS MODULE β INTERACT WITH OPERATING SYSTEM
# ===========================================================
import os
print("π OS Module Examples:")
print("Current working directory:", os.getcwd())
print("List of files:", os.listdir('.'))
print("Environment variables:", os.environ.get('USERNAME'))
print("-" * 60)
# ===========================================================
# 7οΈβ£ SYS MODULE β SYSTEM SPECIFIC PARAMETERS
# ===========================================================
import sys
print("π Sys Module Examples:")
print("Python Version:", sys.version)
print("Command Line Arguments:", sys.argv)
print("Platform:", sys.platform)
print("-" * 60)
# ===========================================================
# 8οΈβ£ JSON MODULE β WORKING WITH JSON DATA
# ===========================================================
import json
person = {"name": "Alice", "age": 25, "city": "Paris"}
print("π JSON Module Examples:")
json_data = json.dumps(person)
print("Dictionary -> JSON string:", json_data)
dict_data = json.loads(json_data)
print("JSON string -> Dictionary:", dict_data)
print("-" * 60)
# ===========================================================
# 9οΈβ£ COLLECTIONS MODULE β SPECIALIZED DATA STRUCTURES
# ===========================================================
from collections import Counter, namedtuple, defaultdict
print("π Collections Module Examples:")
# Counter
print("Counter:", Counter(['a', 'b', 'a', 'c', 'b', 'a']))
# NamedTuple
Point = namedtuple('Point', ['x', 'y'])
pt = Point(10, 20)
print("NamedTuple:", pt, pt.x, pt.y)
# DefaultDict
d = defaultdict(int)
d['a'] += 1
print("DefaultDict:", dict(d))
print("-" * 60)
# ===========================================================
# π TIME MODULE β TIME HANDLING
# ===========================================================
import time
print("π Time Module Examples:")
print("Current timestamp:", time.time())
print("Local time:", time.ctime())
print("Sleeping for 1 second...")
time.sleep(1)
print("Awake now!")
print("-" * 60)
# ===========================================================
# 1οΈβ£1οΈβ£ RE MODULE β REGULAR EXPRESSIONS
# ===========================================================
import re
print("π Re Module Examples:")
text = "Contact: test@example.com"
pattern = r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b"
match = re.search(pattern, text)
if match:
print("Email found:", match.group())
print("-" * 60)
# ===========================================================
# 1οΈβ£2οΈβ£ CSV MODULE β WORKING WITH CSV FILES
# ===========================================================
import csv
print("π CSV Module Examples:")
with open("sample.csv", "w", newline='') as file:
writer = csv.writer(file)
writer.writerow(["Name", "Age"])
writer.writerow(["Alice", 25])
writer.writerow(["Bob", 30])
with open("sample.csv", "r") as file:
reader = csv.reader(file)
for row in reader:
print(row)
print("-" * 60)
"""
===========================================================
π USE CASES OF STANDARD LIBRARY
===========================================================
β
Data Analysis (json, csv, statistics)
β
System Utilities (os, sys, shutil)
β
Web Scraping (re, urllib)
β
Automation Scripts (os, time, datetime)
β
AI/ML Preprocessing (random, math, itertools)
β
File Management & Logging (logging, pathlib, csv)
===========================================================
"""