Top 20 Most Important Python Imports
with Examples
Module: os
Description: Provides a way of using operating system dependent functionality.
Important Methods: getcwd, listdir, mkdir
Example:
import os
print(os.getcwd())
Module: sys
Description: Provides access to some variables used or maintained by the interpreter.
Important Methods: argv, exit, path
Example:
import sys
print(sys.argv)
Module: math
Description: Provides access to mathematical functions.
Important Methods: sqrt, pow, ceil
Example:
import math
print(math.sqrt(16))
Module: random
Description: Implements pseudo-random number generators.
Important Methods: randint, choice, shuffle
Example:
import random
print(random.randint(1, 10))
Module: datetime
Description: Supplies classes for manipulating dates and times.
Important Methods: datetime.now, date.today
Example:
from datetime import datetime
print(datetime.now())
Module: time
Description: Provides various time-related functions.
Important Methods: sleep, time
Example:
import time
time.sleep(1)
print("Slept for 1 second")
Module: json
Description: Provides methods for parsing JSON.
Important Methods: load, dump
Example:
import json
print(json.dumps({'name': 'Alice'}))
Module: re
Description: Provides regular expression matching operations.
Important Methods: search, match, findall
Example:
import re
print(re.findall(r'\d+', 'My number is 12345'))
Module: collections
Description: Implements specialized container datatypes.
Important Methods: Counter, defaultdict, deque
Example:
from collections import Counter
print(Counter('hello'))
Module: itertools
Description: Implements iterator building blocks.
Important Methods: count, cycle, combinations
Example:
import itertools
print(list(itertools.combinations([1, 2, 3], 2)))
Module: functools
Description: Higher-order functions and operations on callable objects.
Important Methods: reduce, lru_cache
Example:
from functools import reduce
print(reduce(lambda x, y: x+y, [1, 2, 3, 4]))
Module: operator
Description: Standard operators as functions.
Important Methods: itemgetter, attrgetter
Example:
import operator
print(operator.add(3, 5))
Module: pathlib
Description: Object-oriented filesystem paths.
Important Methods: Path, exists
Example:
from pathlib import Path
print(Path('.').exists())
Module: statistics
Description: Functions for statistical operations.
Important Methods: mean, median
Example:
import statistics
print(statistics.mean([1, 2, 3, 4]))
Module: subprocess
Description: Spawns new processes, connects to their input/output/error pipes.
Important Methods: run, Popen
Example:
import subprocess
subprocess.run(['echo', 'Hello'])
Module: threading
Description: Higher-level threading interface.
Important Methods: Thread, Lock
Example:
import threading
thread = threading.Thread(target=print, args=("Hello",))
thread.start()
Module: logging
Description: Logging facility for Python.
Important Methods: basicConfig, info, error
Example:
import logging
logging.basicConfig(level=logging.INFO)
logging.info('Info message')
Module: argparse
Description: Command-line option and argument parsing.
Important Methods: ArgumentParser
Example:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--name')
Module: requests
Description: HTTP library for making requests.
Important Methods: get, post
Example:
import requests
response = requests.get('https://example.com')
print(response.status_code)
Module: pandas
Description: Data analysis and manipulation tool.
Important Methods: DataFrame, read_csv
Example:
import pandas as pd
df = pd.DataFrame({'A': [1, 2]})
print(df)