Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
By default, How Many Modules Does Python Come With?
Python is an open-source programming language widely used for various purposes including web development, data analysis, artificial intelligence, machine learning, and more. One of Python's main strengths is its modular architecture, which allows developers to easily extend its functionality by importing pre-written code modules.
What is a Python Module?
A module is a file containing Python definitions and statements. Modules can be imported into other modules and can contain classes, functions, and variables that can be used by other components of the program.
How Many Built-in Modules Does Python Come With?
Python includes a vast number of built-in modules in its standard library. These modules cover a wide range of functionality, from basic operations such as file I/O and string manipulation, to more advanced topics like networking and web development.
As of Python 3.11, the standard library includes over 200 built-in modules. The exact number can vary slightly depending on the Python version and installation, but this gives you an idea of the extensive functionality available out of the box.
Example: Counting Available Modules
You can check the available modules in your Python installation ?
import sys
import pkgutil
# Get all built-in modules
builtin_modules = sys.builtin_module_names
print(f"Built-in modules: {len(builtin_modules)}")
# Get all available modules (including standard library)
available_modules = [name for _, name, _ in pkgutil.iter_modules()]
print(f"Total available modules: {len(available_modules)}")
# Show first 10 built-in modules
print("\nFirst 10 built-in modules:")
for module in sorted(builtin_modules)[:10]:
print(f" - {module}")
Built-in modules: 62 Total available modules: 267 First 10 built-in modules: - _abc - _ast - _bisect - _blake2 - _codecs - _collections - _csv - _datetime - _elementtree - _functools
Commonly Used Standard Library Modules
Here are some of the most commonly used modules in the Python standard library ?
os Provides a way to interact with the operating system, including creating, deleting, and renaming files and directories.
sys Provides information about the Python interpreter and the environment in which it is running.
re Provides regular expression matching operations, useful for searching and manipulating text data.
math Provides a set of mathematical functions, including trigonometric functions, logarithms, and more.
random Provides functions for generating random numbers, commonly used in simulations and games.
datetime Provides classes for working with dates and times, including formatting and parsing date strings.
urllib Provides a way to interact with web pages and URLs, including downloading files and web scraping.
json Provides functions for encoding and decoding JSON data, a popular data format used in web applications.
sqlite3 Provides a way to interact with SQLite databases, which are lightweight and easy to use.
threading Provides a way to write concurrent programs by running multiple threads of execution within a single program.
Example: Using Standard Library Modules
import math
import random
import datetime
# Using math module
radius = 5
area = math.pi * radius ** 2
print(f"Circle area: {area:.2f}")
# Using random module
random_number = random.randint(1, 10)
print(f"Random number: {random_number}")
# Using datetime module
current_time = datetime.datetime.now()
print(f"Current time: {current_time.strftime('%Y-%m-%d %H:%M:%S')}")
Circle area: 78.54 Random number: 7 Current time: 2024-01-15 10:30:45
Third-Party Modules
In addition to the standard library, there are numerous third-party Python modules available for download. These can be installed using Python's package manager, pip. The Python Package Index (PyPI) is a repository of over 400,000 packages, making it easy for developers to find and install the modules they need.
Conclusion
Python comes with over 200 built-in modules in its standard library, covering everything from basic file operations to advanced networking. This extensive collection, combined with thousands of third-party packages, makes Python incredibly versatile for any programming task.
