-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
Python's standard library is a collection of modules and packages that come pre-installed with Python. These libraries provide essential functions and utilities, so you don't have to write everything from scratch. Here are some widely used standard libraries and examples of how you might use them: | ||
|
||
### 1. **os** - Operating System Interface | ||
The `os` module allows you to interact with the operating system. You can manage files, directories, and environment variables. | ||
|
||
**Example:** | ||
```python | ||
import os | ||
|
||
# Get the current working directory | ||
print("Current Directory:", os.getcwd()) | ||
|
||
# Create a new directory | ||
os.mkdir("new_folder") | ||
|
||
# List all files in a directory | ||
print("Files:", os.listdir(".")) | ||
``` | ||
|
||
### 2. **sys** - System-Specific Parameters and Functions | ||
The `sys` module provides functions to manipulate the Python runtime environment, such as reading command-line arguments and exiting the program. | ||
|
||
**Example:** | ||
```python | ||
import sys | ||
|
||
# Print command-line arguments | ||
print("Arguments:", sys.argv) | ||
|
||
# Exit the program with a specific code | ||
sys.exit(0) | ||
``` | ||
|
||
### 3. **datetime** - Date and Time Manipulation | ||
The `datetime` module is used to work with dates and times. You can create, manipulate, and format date and time objects. | ||
|
||
**Example:** | ||
```python | ||
from datetime import datetime | ||
|
||
# Current date and time | ||
now = datetime.now() | ||
print("Current Date and Time:", now) | ||
|
||
# Format date and time | ||
formatted = now.strftime("%Y-%m-%d %H:%M:%S") | ||
print("Formatted Date and Time:", formatted) | ||
``` | ||
|
||
### 4. **math** - Mathematical Functions | ||
The `math` module provides mathematical functions, constants, and complex calculations, such as square roots, trigonometry, and logarithmic functions. | ||
|
||
**Example:** | ||
```python | ||
import math | ||
|
||
# Calculate square root | ||
print("Square Root of 16:", math.sqrt(16)) | ||
|
||
# Calculate the sine of pi/2 | ||
print("Sine of pi/2:", math.sin(math.pi / 2)) | ||
``` | ||
|
||
### 5. **random** - Random Number Generation | ||
The `random` module allows you to generate random numbers, select random elements from a list, and more. It's commonly used in simulations and games. | ||
|
||
**Example:** | ||
```python | ||
import random | ||
|
||
# Generate a random integer between 1 and 10 | ||
print("Random Integer:", random.randint(1, 10)) | ||
|
||
# Select a random element from a list | ||
colors = ["red", "blue", "green"] | ||
print("Random Color:", random.choice(colors)) | ||
``` | ||
|
||
### 6. **json** - JSON Encoding and Decoding | ||
The `json` module enables you to work with JSON (JavaScript Object Notation) data, which is commonly used for data interchange. | ||
|
||
**Example:** | ||
```python | ||
import json | ||
|
||
# Convert Python dictionary to JSON string | ||
data = {"name": "Alice", "age": 30} | ||
json_string = json.dumps(data) | ||
print("JSON String:", json_string) | ||
|
||
# Convert JSON string to Python dictionary | ||
parsed_data = json.loads(json_string) | ||
print("Parsed Data:", parsed_data) | ||
``` | ||
|
||
### 7. **re** - Regular Expressions | ||
The `re` module provides functions to work with regular expressions, useful for pattern matching and text manipulation. | ||
|
||
**Example:** | ||
```python | ||
import re | ||
|
||
# Find all occurrences of 'cat' in a string | ||
text = "The cat sat on the mat with another cat." | ||
matches = re.findall(r"cat", text) | ||
print("Matches:", matches) | ||
|
||
# Replace 'cat' with 'dog' | ||
new_text = re.sub(r"cat", "dog", text) | ||
print("Replaced Text:", new_text) | ||
``` | ||
|
||
### 8. **urllib** - URL Handling | ||
The `urllib` module is useful for fetching data across the web and working with URLs. | ||
|
||
**Example:** | ||
```python | ||
from urllib import request | ||
|
||
# Fetch content from a URL | ||
url = "http://example.com" | ||
response = request.urlopen(url) | ||
html_content = response.read().decode('utf-8') | ||
print("HTML Content:", html_content[:100]) # Display only the first 100 characters | ||
``` | ||
|
||
### 9. **collections** - Specialized Data Structures | ||
The `collections` module provides alternative data structures like `Counter`, `deque`, `namedtuple`, and `OrderedDict`. | ||
|
||
**Example:** | ||
```python | ||
from collections import Counter | ||
|
||
# Count the frequency of elements in a list | ||
fruits = ["apple", "banana", "apple", "orange", "banana"] | ||
fruit_counts = Counter(fruits) | ||
print("Fruit Counts:", fruit_counts) | ||
``` | ||
|
||
### 10. **subprocess** - Running External Commands | ||
The `subprocess` module allows you to spawn new processes, connect to input/output/error pipes, and obtain return codes. | ||
|
||
**Example:** | ||
```python | ||
import subprocess | ||
|
||
# Run an external command and capture its output | ||
result = subprocess.run(["echo", "Hello from subprocess!"], capture_output=True, text=True) | ||
print("Output:", result.stdout) | ||
``` | ||
|
||
These are only a few examples of Python's rich standard library. Each library provides versatile and efficient tools to simplify tasks across various domains, making Python a powerful and accessible language. |