Standard Library
Harness the full power of built-in modules
Python Standard Library
One of Python’s greatest strengths is its rich standard library, which offers a collection of ready-to-use modules covering a wide range of functionality. These modules are available in every Python installation without any additional downloads, which makes Python a “batteries included” language.
In this tutorial, we’ll explore some of the most useful modules in the Python standard library and how to use them to solve a variety of common problems.
Exploring the Essential Modules
datetime - Working with dates and times
The datetime module provides classes for manipulating dates and times.
from datetime import datetime, timedelta, date
# Current date and time
maintenant = datetime.now()
print(f"Date et heure actuelles : {maintenant}")
# Format a date
date_formatee = maintenant.strftime("%d/%m/%Y %H:%M:%S")
print(f"Date formatée : {date_formatee}")
# Create a specific date
date_specifique = datetime(2023, 12, 31, 23, 59, 59)
print(f"Réveillon du Nouvel An : {date_specifique}")
# Calculate the difference between two dates
difference = date_specifique - maintenant
print(f"Jours jusqu'au Nouvel An : {difference.days}")
# Add a time interval to a date
dans_une_semaine = maintenant + timedelta(days=7)
print(f"Dans une semaine : {dans_une_semaine}")
random - Generating random numbers
The random module is useful for generating random numbers and making random selections.
import random
# Random number between 0 and 1
nombre_aleatoire = random.random()
print(f"Nombre aléatoire entre 0 et 1 : {nombre_aleatoire}")
# Random integer within a range
entier_aleatoire = random.randint(1, 100)
print(f"Entier aléatoire entre 1 et 100 : {entier_aleatoire}")
# Random choice from a list
fruits = ["pomme", "banane", "orange", "fraise", "kiwi"]
fruit_aleatoire = random.choice(fruits)
print(f"Fruit aléatoire : {fruit_aleatoire}")
# Shuffle a list
random.shuffle(fruits)
print(f"Liste mélangée : {fruits}")
# Select k unique random elements
selection = random.sample(range(1, 51), 6) # 6 numbers for a lottery draw
print(f"Tirage du loto : {selection}")
math - Mathematical functions
The math module provides advanced mathematical functions.
import math
# Mathematical constants
print(f"Pi : {math.pi}")
print(f"e : {math.e}")
# Trigonometric functions (in radians)
print(f"sin(π/2) : {math.sin(math.pi/2)}")
print(f"cos(π) : {math.cos(math.pi)}")
# Logarithms
print(f"log(10) (base e) : {math.log(10)}")
print(f"log10(100) : {math.log10(100)}")
# Square root and power
print(f"Racine carrée de 16 : {math.sqrt(16)}")
print(f"2 puissance 10 : {math.pow(2, 10)}")
# Rounding
print(f"Arrondi inférieur de 3.7 : {math.floor(3.7)}")
print(f"Arrondi supérieur de 3.7 : {math.ceil(3.7)}")
collections - Specialized data structures
The collections module provides alternatives to the built-in data structures.
from collections import Counter, defaultdict, namedtuple, deque
# Counter - Count occurrences
texte = "Mississippi"
compteur = Counter(texte)
print(f"Occurrences des lettres : {compteur}")
print(f"Lettres les plus fréquentes : {compteur.most_common(2)}")
# defaultdict - Dictionary with a default value
dd = defaultdict(list) # The default value is an empty list
dd["fruits"].append("pomme")
dd["fruits"].append("banane")
dd["legumes"].append("carotte")
print(f"defaultdict : {dict(dd)}")
# namedtuple - Tuples with named fields
Point = namedtuple('Point', ['x', 'y'])
p = Point(11, y=22)
print(f"Point : {p}, Coordonnées : x={p.x}, y={p.y}")
# deque - Double-ended queue
d = deque(["a", "b", "c"])
d.append("d") # Add to the right
d.appendleft("z") # Add to the left
print(f"deque : {d}")
re - Regular expressions
The re module lets you work with regular expressions to search and manipulate text.
import re
texte = "Contactez-moi à john.doe@example.com ou jane_smith@company.org"
# Search for a match
if re.search(r"@example\.com", texte):
print("Adresse e-mail example.com trouvée!")
# Find all email addresses
pattern = r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b"
emails = re.findall(pattern, texte)
print(f"Adresses e-mail trouvées : {emails}")
# Replace text
texte_anonymise = re.sub(pattern, "[EMAIL REMOVED]", texte)
print(f"Texte anonymisé : {texte_anonymise}")
# Split a string
date = "2023-11-24"
annee, mois, jour = re.split(r"-", date)
print(f"Année : {annee}, Mois : {mois}, Jour : {jour}")
os - Operating system interface
The os module provides functions for interacting with the operating system.
import os
# System information
print(f"Nom du système d'exploitation : {os.name}")
print(f"Répertoire de travail actuel : {os.getcwd()}")
print(f"Contenu du répertoire : {os.listdir('.')}")
# Environment variables
print(f"Variable PATH : {os.environ.get('PATH')}")
# Path manipulation (see also the os.path module)
chemin = os.path.join("dossier", "sous_dossier", "fichier.txt")
print(f"Chemin joint : {chemin}")
print(f"Nom de base : {os.path.basename(chemin)}")
print(f"Répertoire : {os.path.dirname(chemin)}")
sys - System-specific configuration
The sys module provides functions and variables specific to the Python system.
import sys
# Python interpreter information
print(f"Version de Python : {sys.version}")
print(f"Plateforme : {sys.platform}")
# Command-line arguments
print(f"Arguments : {sys.argv}")
# Module search paths
print(f"Chemins de recherche : {sys.path}")
# Standard input/output
sys.stdout.write("Ceci est écrit sur la sortie standard\n")
sys.stderr.write("Ceci est un message d'erreur\n")
json - Working with JSON data
The json module lets you work with data in JSON format.
import json
# Python dictionary
donnees = {
"nom": "Alice",
"age": 30,
"langages": ["Python", "JavaScript", "Go"],
"adresse": {
"rue": "123 Rue Principale",
"ville": "Paris",
"code_postal": "75001"
}
}
# Python -> JSON conversion (serialization)
json_str = json.dumps(donnees, indent=4)
print(f"JSON formaté :\n{json_str}")
# JSON -> Python conversion (deserialization)
obj = json.loads(json_str)
print(f"Prénom : {obj['nom']}, Âge : {obj['age']}")
# Write to a JSON file
with open("donnees.json", "w") as f:
json.dump(donnees, f, indent=4)
# Read from a JSON file
with open("donnees.json", "r") as f:
donnees_lues = json.load(f)
print(f"Données lues du fichier : {donnees_lues['nom']}")
time - Time-related functions
The time module provides functions for working with time.
import time
# Current timestamp (seconds since the Unix epoch)
timestamp = time.time()
print(f"Timestamp actuel : {timestamp}")
# Convert timestamp to local time
temps_local = time.localtime(timestamp)
print(f"Année : {temps_local.tm_year}, Mois : {temps_local.tm_mon}, Jour : {temps_local.tm_mday}")
# Format the time
temps_formate = time.strftime("%Y-%m-%d %H:%M:%S", temps_local)
print(f"Temps formaté : {temps_formate}")
# Measure execution time
debut = time.time()
# Simulate a long-running operation
time.sleep(1)
fin = time.time()
print(f"Temps d'exécution : {fin - debut} secondes")
argparse - Parsing command-line arguments
The argparse module makes it easy to write user-friendly command-line interfaces.
import argparse
def main():
# Create an argument parser
parser = argparse.ArgumentParser(description='Un exemple de programme en ligne de commande')
# Add arguments
parser.add_argument('fichier', help='fichier à traiter')
parser.add_argument('-v', '--verbose', action='store_true', help='activer la verbosité')
parser.add_argument('-o', '--output', help='fichier de sortie')
parser.add_argument('-n', '--number', type=int, default=1, help='nombre d\'itérations')
# Parse the arguments
args = parser.parse_args()
# Use the arguments
print(f"Fichier à traiter : {args.fichier}")
if args.verbose:
print("Mode verbeux activé")
if args.output:
print(f"Fichier de sortie : {args.output}")
print(f"Nombre d'itérations : {args.number}")
if __name__ == "__main__":
main()
itertools - Functions for efficient iterators
The itertools module provides functions that create iterators for efficient looping.
import itertools
# Generate combinations
nombres = [1, 2, 3]
for combo in itertools.combinations(nombres, 2):
print(f"Combinaison : {combo}")
# Generate permutations
for perm in itertools.permutations([1, 2, 3], 2):
print(f"Permutation : {perm}")
# Cartesian product
for prod in itertools.product("AB", "12"):
print(f"Produit : {''.join(prod)}")
# Infinite cycles
cycleur = itertools.cycle(["rouge", "vert", "bleu"])
for _ in range(6):
print(f"Couleur : {next(cycleur)}")
# Group elements
donnees = [("A", 1), ("A", 2), ("B", 1), ("B", 2)]
for cle, groupe in itertools.groupby(donnees, lambda x: x[0]):
print(f"Groupe {cle} : {list(groupe)}")
logging - Logging
The logging module provides a flexible logging system.
import logging
# Basic configuration
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
filename='app.log'
)
# Different log levels
logging.debug("Ce message est au niveau DEBUG")
logging.info("Ce message est au niveau INFO")
logging.warning("Ce message est au niveau WARNING")
logging.error("Ce message est au niveau ERROR")
logging.critical("Ce message est au niveau CRITICAL")
# Use a specific logger
logger = logging.getLogger('mon_application')
logger.info("Information de mon_application")
# Exception logging
try:
1 / 0
except Exception as e:
logging.exception("Une erreur s'est produite")
urllib - URL handling modules
The urllib module lets you work with URLs and make HTTP requests.
import urllib.request
import urllib.parse
import json
# Perform a simple GET request
with urllib.request.urlopen('https://httpbin.org/get') as response:
data = json.loads(response.read().decode())
print(f"URL : {data['url']}")
# Perform a POST request
params = urllib.parse.urlencode({'name': 'John', 'age': 30}).encode()
req = urllib.request.Request('https://httpbin.org/post', data=params, method='POST')
with urllib.request.urlopen(req) as response:
data = json.loads(response.read().decode())
print(f"Données envoyées : {data['form']}")
# Parse a URL
url = "https://www.example.com/path?param1=value1¶m2=value2"
parsed_url = urllib.parse.urlparse(url)
print(f"Schéma : {parsed_url.scheme}, Domaine : {parsed_url.netloc}, Chemin : {parsed_url.path}")
print(f"Paramètres : {urllib.parse.parse_qs(parsed_url.query)}")
functools - Higher-order functions and operations on functions
The functools module provides tools for working with functions and callables.
import functools
# Decorator to cache a function's results
@functools.lru_cache(maxsize=None)
def fibonacci(n):
if n < 2:
return n
return fibonacci(n-1) + fibonacci(n-2)
print(f"Fibonacci de 30 : {fibonacci(30)}")
print(f"Statistiques du cache : {fibonacci.cache_info()}")
# Partial function (fixes some arguments)
def multiplier(x, y):
return x * y
doubler = functools.partial(multiplier, 2)
tripler = functools.partial(multiplier, 3)
print(f"Doubler 5 : {doubler(5)}")
print(f"Tripler 5 : {tripler(5)}")
# reduce - Apply a function cumulatively
nombres = [1, 2, 3, 4, 5]
somme = functools.reduce(lambda x, y: x + y, nombres)
print(f"Somme : {somme}")
Modules for Specific Use Cases
statistics - Statistical functions
The statistics module provides functions for computing basic statistics.
import statistics
donnees = [2, 4, 4, 4, 5, 5, 7, 9]
print(f"Moyenne : {statistics.mean(donnees)}")
print(f"Médiane : {statistics.median(donnees)}")
print(f"Mode : {statistics.mode(donnees)}")
print(f"Écart-type : {statistics.stdev(donnees)}")
print(f"Variance : {statistics.variance(donnees)}")
sqlite3 - SQLite interface
The sqlite3 module provides an interface for using the SQLite database.
import sqlite3
# Connect to a database (it will be created if it doesn't exist)
conn = sqlite3.connect('exemple.db')
cursor = conn.cursor()
# Create a table
cursor.execute('''
CREATE TABLE IF NOT EXISTS utilisateurs (
id INTEGER PRIMARY KEY,
nom TEXT NOT NULL,
email TEXT UNIQUE,
age INTEGER
)
''')
# Insert data
cursor.execute("INSERT INTO utilisateurs (nom, email, age) VALUES (?, ?, ?)",
("Alice", "alice@example.com", 30))
cursor.execute("INSERT INTO utilisateurs (nom, email, age) VALUES (?, ?, ?)",
("Bob", "bob@example.com", 25))
# Commit the changes
conn.commit()
# SELECT query
cursor.execute("SELECT * FROM utilisateurs")
for row in cursor.fetchall():
print(f"Utilisateur : {row}")
# Close the connection
conn.close()
uuid - Generating universally unique identifiers
The uuid module lets you generate universally unique identifiers.
import uuid
# Generate different types of UUIDs
print(f"UUID1 (basé sur l'horloge) : {uuid.uuid1()}")
print(f"UUID4 (aléatoire) : {uuid.uuid4()}")
# Create a UUID from a name
namespace = uuid.NAMESPACE_DNS
nom = "example.com"
uuid5 = uuid.uuid5(namespace, nom)
print(f"UUID5 (basé sur un nom) : {uuid5}")
hashlib - Cryptographic hash functions
The hashlib module implements various cryptographic hash functions.
import hashlib
# Hash a string with different algorithms
texte = "Bonjour, monde!"
print(f"MD5 : {hashlib.md5(texte.encode()).hexdigest()}")
print(f"SHA-1 : {hashlib.sha1(texte.encode()).hexdigest()}")
print(f"SHA-256 : {hashlib.sha256(texte.encode()).hexdigest()}")
print(f"SHA-512 : {hashlib.sha512(texte.encode()).hexdigest()}")
# Incremental hashing
h = hashlib.sha256()
h.update(b"Partie 1 du message")
h.update(b"Partie 2 du message")
print(f"Hachage incrémental : {h.hexdigest()}")
concurrent.futures - Parallel task execution
The concurrent.futures module provides interfaces for asynchronous task execution.
import concurrent.futures
import time
def tache_longue(n):
"""Simule une tâche qui prend du temps"""
time.sleep(n)
return f"Tâche {n} terminée après {n} secondes"
# Exécution en parallèle avec ThreadPoolExecutor
with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
taches = [1, 2, 3, 4, 5]
futurs = {executor.submit(tache_longue, t): t for t in taches}
for futur in concurrent.futures.as_completed(futurs):
tache = futurs[futur]
try:
resultat = futur.result()
print(f"Résultat de la tâche {tache}: {resultat}")
except Exception as e:
print(f"Tâche {tache} a généré une exception: {e}")
shutil - High-level file operations
The shutil module provides high-level operations on files and collections of files.
import shutil
import os
# Créer un répertoire pour les tests
os.makedirs("test_dossier", exist_ok=True)
with open("test_dossier/test.txt", "w") as f:
f.write("Contenu de test")
# Copier un fichier
shutil.copy("test_dossier/test.txt", "test_dossier/test_copie.txt")
# Copier un répertoire entier
shutil.copytree("test_dossier", "test_dossier_copie")
# Déplacer un fichier
shutil.move("test_dossier/test_copie.txt", "test_dossier/test_deplace.txt")
# Supprimer un répertoire et son contenu
shutil.rmtree("test_dossier_copie")
# Archiver un répertoire
shutil.make_archive("archive_test", "zip", "test_dossier")
Conclusion
Python’s standard library is vast and covers a wide range of functionality. This tutorial has only scratched the surface of what’s available. By getting familiar with these modules, you can solve many common problems without having to install third-party packages.
Here are a few tips for getting the most out of the standard library:
-
Consult the official documentation: The Python documentation contains detailed information about all the modules in the standard library.
-
Prefer standard solutions: Before installing an external package, check whether the standard library already offers a solution.
-
Explore what’s new: With each new version of Python, new modules or features are added to the standard library.
In the next and final tutorial in this series, we’ll put everything we’ve learned into practice by building a complete Python project that uses several of the concepts and modules we’ve explored so far.
Comments
Comments are powered by GitHub Discussions
Sign in with GitHub to join the discussion