Open Category List

Python Standard Libraries

1 minuteread

Python comes with a rich set of built-in modules known as the standard library, which provides functionality for various tasks.

Some Important Standard Libraries:

  • os: Interacting with the operating system.
  • sys: Access to some variables used or maintained by the interpreter.
  • math: Mathematical functions.
  • datetime: Manipulating dates and times.
  • random: Generating pseudo-random numbers.
  • json: Encoding and decoding JSON data.
  • re: Regular expressions.
  • urllib: Working with URLs.
  • sqlite3: SQLite database operations.
  • threading: Thread-based parallelism.

Example Using datetime:

import datetime

current_time = datetime.datetime.now()
print("Current date and time:", current_time)

formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S")
print("Formatted date and time:", formatted_time)

Example Using json:

import json

# Convert Python object to JSON string
data = {'name': 'Alice', 'age': 30, 'city': 'New York'}
json_str = json.dumps(data)
print(json_str)

# Convert JSON string to Python object
parsed_data = json.loads(json_str)
print(parsed_data['name'])  # Output: Alice

Related Knowledge Base Posts