The Python Standard Library is a collection of built-in modules that come pre-installed with Python.
You do not need to install them using pip.
They are available immediately after installing Python.
The standard library helps you:
- Work with files and folders
- Handle dates and time
- Perform mathematical operations
- Generate random numbers
- Work with system and OS features
- Process JSON data
- And much more
WHY USE STANDARD LIBRARY?
• No installation required
• Reliable and well-tested
• Saves development time
• Covers most common programming tasks
COMMONLY USED STANDARD LIBRARY MODULES
1. math
Provides mathematical functions.
import mathprint(math.sqrt(16))
print(math.pi)
2. random
Generates random numbers.
import randomprint(random.randint(1, 10))
3. datetime
Works with dates and time.
import datetimenow = datetime.datetime.now()
print(now)
4. os
Interacts with the operating system.
import osprint(os.getcwd())
5. sys
Provides system-specific parameters.
import sysprint(sys.version)
6. json
Handles JSON data.
import jsondata = {"name": "Hira", "age": 25}
json_data = json.dumps(data)
print(json_data)
7. statistics
Performs statistical calculations.
import statisticsnumbers = [10, 20, 30, 40]
print(statistics.mean(numbers))
8. itertools
Provides tools for working with iterators.
import itertoolsnumbers = [1, 2, 3]
for pair in itertools.combinations(numbers, 2):
print(pair)
HOW TO SEE ALL STANDARD LIBRARY MODULES
You can explore documentation at:
https://docs.python.org/3/library/
Or inside Python:
help("modules")
STANDARD LIBRARY VS THIRD-PARTY LIBRARIES
| Standard Library | Third-Party Library |
|---|---|
| Comes with Python | Installed using pip |
| No installation needed | Must install manually |
| Official & built-in | Created by developers worldwide |
BEST PRACTICES
• Use standard library whenever possible
• Avoid installing external libraries if built-in modules can do the job
• Read official documentation for deeper understanding
KEY TAKEAWAY
The Python Standard Library is powerful and comprehensive.
It provides ready-to-use tools that allow you to build efficient and professional applications without installing extra packages.