Exploring standard library modules in python
Exploring the standard library modules in Python programming is an easy way to use the built-in Python library modules for Python programming tasks without the need to install additional custom Python packages based on your Python programming needs. Remember that the standard library for Python programming is very vast, and it already contains all the necessary Python development library modules that a Python programmer needs such as data types, data structures, file operations, client server networking communication, and many more.
So let’s take a closer look at the standard programming library modules that are already present in Python programming.
os standard library module.
Provides operating system features for Python programmers to directly interact with the installed operating system. Such as navigating/exploring the operating system file system (os.path), executing operating system commands (os.system), and managing and manipulating operating system environment variables (os.environ).
import os
# findout below file is exist or not exit at path location
if os.path.exists(‘/path/to/info.txt’):
print(“\n let check file is exist on path or not”)
sys standard library module.
The Python sys system library module provides user access control to system-specific parameters and functions, such as configuring and managing command-line arguments (sys.argv) and Python interpreter settings (sys.version) in the Python programming environment.
import sys #import python sys standard module library
# let display current installed python version info
print(sys.version)
datetime standard library module.
Python provides built-in classes for programmers to manipulate date and time (datetime.datetime, datetime.date, datetime.time) in an existing Python program and format them in string format.
from datetime import datetime # import datetime python standard module library
# let display current data and time in python program
now = datetime.now()
print(now.strftime(“\n date is – %Y-%m-%d \n time is – %H:%M:%S”))
random standard library module.
Python provides functions to display random numbers in Python programming and to select random elements from that list. Random standard library helps you to print random numbers in a particular range. For example, (random.random, random.randint, random.choice).
# let display random integer between 2 to 17
print(random.randint(2, 17))
json standard library module.
In Python programming, the json standard library module enables Python programmers to encode and decode JSON data (json.dumps, json.loads) with features.
import json # import json standard library module
# let display and convert python element to json string
info = {‘course’: ‘python’, ‘fee’: 99, ‘duration’: 3}
json_str_module = json.dumps(info)
print(“\n”, json_str_module)
csv standard library module.
In Python programming, the standard library module provides classes for reading and writing CSV files, called (csv.reader, csv.writer) classes. A csv file is a simple text file of a spreadsheet file where information is stored in row and column order.
import csv # import csv python standard module
# let try to read csv file in python program
with open(‘file.csv’, newline=”) as csvfile:
reader = csv.reader(csvfile, delimiter=’,’)
for row in reader:
print(‘, ‘.join(row))
math standard library module.
Provides mathematical functions and constants in Python programming. Standard model library features. For example, (math.sqrt, math.pi, math.sin).
# let calculate mathematics square roop in python
import math
print(math.sqrt(120)) # display given number square root
collections standard library module.
Provides special container datatype functions to Python programmers beyond the built-in types (collections.Counter, collections.defaultdict, collections.namedtuple).
from collections import Counter # import collection as counter standard library module
# let display info from given list data type
programming = [‘python’, ‘java’, ‘c++’, ‘c’, ‘sql’, ‘ruby’, ‘css’, ‘html’]
display = Counter(programming)
print(“\n”,display)
socket standard library module.
Python programming provides low-level networking interface features for online communication between computers over a network. (socket.socket, socket.bind, socket.connect).
import socket # import socket standard library module
# let create a tcp socket for client
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((‘domain.com’, 80))
About Python Standard Module Library.
The standard library of Python programming is very large or vast. It covers a vast series of different domains of Python programming. Python programmers can find out more about the modules and features and functions in the official Python library documentation. Each Python module document provides a complete detail clarification, existing module examples and usage guide so that you can use them effectively in your Python programming project based on your needs. Understanding and gaining expertise in these modules based on their programming needs will greatly increase your productivity and functionality as a Python programmer.