1. Introduction
“Hello everyone! Welcome back to our Python learning series. Today, we are going to talk about a very interesting topic: Modules and Libraries in Python. Whether you’re a student or working in an office, this concept will save you time and effort in coding. Let’s dive in!”
2.What Are Modules and Libraries?
- Definition of Modules:
“Modules are like tools. Instead of writing everything from scratch, you can use these pre-built tools to do tasks quickly. For example, Python has a module calledmathfor calculations.” - Definition of Libraries:
“A library is a collection of many modules. Think of it like a toolbox full of different tools for different tasks. Libraries likepandasandopenpyxlare used for tasks like managing Excel files.”
3. How to Import Modules –
- Basic Syntax:
import module_name
“For example, to use the math module, just type: import math.”
- Example 1: Calculate Square Root with
mathModule
import math
result = math.sqrt(16)
print("The square root of 16 is:", result)
4. Import Specific Function:
from math import sqrt
result = sqrt(25)
print("Square root of 25 is:", result)
- “This way, we import only the part we need, making the code shorter.”
5. Examples for Modules
randomModule for Selecting Random Items
import random students = ["Amit", "Priya", "Rahul", "Sneha"]
chosen = random.choice(students)
print("The chosen student is:", chosen)
2. datetime for Date and Time
from datetime import datetime
now = datetime.now()
print("Current date and time:", now)
6. Examples Useful Libraries
openpyxlfor Excel Files- “Imagine you have an Excel file and want to automate tasks like reading or writing data.”
from openpyxl import Workbook
workbook = Workbook()
sheet = workbook.active
sheet["A1"] = "Hello, Excel!"
workbook.save("example.xlsx")
print("Excel file created!")
2. os for Managing Files
- “This library helps you work with files and folders directly in Python.”
import os os.makedirs("NewFolder")
print("Folder created!")
7. How to Install External Libraries
- Using
pipCommand:
“To install a library not built into Python, use thepipcommand in command prompt. For example:
pip install pandas
“This installs the pandas library, which is great for handling large datasets.
Pre-installed modules and libraries:
Python comes with a standard library that includes many pre-installed modules and libraries, making it easy to perform a wide range of tasks without installing additional packages. Below are some of the commonly used predefined modules and libraries included in Python:
1. General Purpose Modules
sys: Provides access to system-specific parameters and functions.- Example:
sys.argvfor command-line arguments.
- Example:
os: For interacting with the operating system.- Example:
os.listdir()to list files in a directory.
- Example:
time: Handles time-related tasks.- Example:
time.sleep()to pause execution.
- Example:
datetime: For working with dates and times.- Example:
datetime.date.today()to get the current date.
- Example:
platform: Provides information about the platform (OS, Python version, etc.).- Example:
platform.system()to get the OS name.
- Example:
2. File and Directory Handling
shutil: High-level file and directory operations.- Example:
shutil.copy()to copy files.
- Example:
pathlib: Object-oriented approach to working with file paths.- Example:
Path().exists()to check if a file exists.
- Example:
glob: To find file paths using patterns.- Example:
glob.glob('*.txt')to find all text files.
- Example:
3. Data Handling and Manipulation
json: For working with JSON data.- Example:
json.dumps()to convert Python objects to JSON.
- Example:
csv: For reading and writing CSV files.- Example:
csv.reader()to read CSV files.
- Example:
sqlite3: For working with SQLite databases.- Example:
sqlite3.connect()to connect to a database.
- Example:
pickle: For serializing and deserializing Python objects.- Example:
pickle.dump()to save objects to a file.
- Example:
4. Math and Statistics
math: Provides mathematical functions.- Example:
math.sqrt()to find the square root.
- Example:
statistics: For statistical calculations.- Example:
statistics.mean()to calculate the average.
- Example:
random: For generating random numbers.- Example:
random.randint()for random integers.
- Example:
5. Internet and Web
urllib: For working with URLs.- Example:
urllib.request.urlopen()to fetch web pages.
- Example:
http: For handling HTTP requests.- Example:
http.clientfor HTTP communication.
- Example:
email: For email processing.- Example:
email.messageto create email messages.
- Example:
6. Text Processing
re: For regular expressions.- Example:
re.search()to search patterns in text.
- Example:
string: Common string operations.- Example:
string.ascii_lettersto get all alphabets.
- Example:
textwrap: For wrapping and formatting text.- Example:
textwrap.wrap()to wrap text to a specified width.
- Example:
7. Debugging and Testing
logging: For logging messages.- Example:
logging.info()to log informational messages.
- Example:
unittest: For writing test cases.- Example:
unittest.TestCaseto define test cases.
- Example:
pdb: Python debugger for debugging code.- Example:
pdb.set_trace()to set a breakpoint.
- Example:
8. Networking
socket: For network communication.- Example:
socket.socket()to create a socket.
- Example:
ipaddress: For working with IP addresses.- Example:
ipaddress.ip_network()to define a network.
- Example:
9. GUI Development
tkinter: For creating graphical user interfaces.- Example:
tkinter.Tk()to create a window.
- Example:
10. Cryptography and Security
hashlib: For generating secure hashes.- Example:
hashlib.md5()to generate MD5 hashes.
- Example:
hmac: For keyed-hashing for message authentication.- Example:
hmac.new()to create a hash object.
- Example:
11. Advanced Topics
itertools: For efficient looping.- Example:
itertools.permutations()to generate permutations.
- Example:
functools: For higher-order functions.- Example:
functools.reduce()to reduce a list.
- Example:
collections: High-performance data structures.- Example:
collections.Counter()to count elements in a list.
- Example:
n Python, the terms module and library are often used interchangeably, but they do have slight distinctions:
Key Differences
- Module: A single Python file containing definitions (functions, classes, variables) and code.
- Library: A collection of modules that provide related functionality. For example, Python’s standard library is a collection of modules and packages included with Python.
Now, let’s clarify which items in the above list are modules and which are libraries:
General Purpose
sys: Moduleos: Moduletime: Moduledatetime: Moduleplatform: Module
File and Directory Handling
shutil: Modulepathlib: Moduleglob: Module
Data Handling and Manipulation
json: Modulecsv: Modulesqlite3: Modulepickle: Module
Math and Statistics
math: Modulestatistics: Modulerandom: Module
Internet and Web
urllib: Library (contains submodules likeurllib.requestandurllib.parse)http: Library (contains submodules likehttp.clientandhttp.server)email: Library (contains submodules likeemail.messageandemail.mime)
Text Processing
re: Modulestring: Moduletextwrap: Module
Debugging and Testing
logging: Moduleunittest: Library (contains submodules likeunittest.mock)pdb: Module
Networking
socket: Moduleipaddress: Module
GUI Development
tkinter: Library (contains modules liketkinter.ttkandtkinter.messagebox)
Cryptography and Security
hashlib: Modulehmac: Module
Advanced Topics
itertools: Modulefunctools: Modulecollections: Module
Most famous external libraries in Python
1. Data Science and Machine Learning
- NumPy: For numerical computing and handling multi-dimensional arrays.
- Pandas: For data manipulation and analysis.
- Matplotlib: For creating static, animated, and interactive visualizations.
- Seaborn: For statistical data visualization built on top of Matplotlib.
- Scikit-learn: For machine learning, including classification, regression, and clustering.
- TensorFlow: For deep learning and AI.
- PyTorch: Another powerful deep learning library.
- Keras: A high-level API for TensorFlow, focusing on ease of use.
- Statsmodels: For statistical modeling and hypothesis testing.
2. Data Visualization
- Plotly: For interactive visualizations, including charts, graphs, and dashboards.
- Bokeh: For creating interactive visualizations in a web browser.
- Altair: Declarative statistical visualization library for Python.
3. Web Development
- Django: A high-level web framework for rapid development and clean, pragmatic design.
- Flask: A lightweight and flexible web framework.
- FastAPI: A modern web framework for building APIs with Python 3.6+.
- Bottle: A micro web framework that is simple to use.
4. Automation and Scripting
- Selenium: For automating web browsers.
- BeautifulSoup: For web scraping and parsing HTML/XML.
- Requests: For making HTTP requests easily.
- PyAutoGUI: For GUI automation tasks like controlling the mouse and keyboard.
5. Game Development
- Pygame: For developing 2D games.
- Godot: Python bindings for the Godot game engine.
- Arcade: Another library for developing 2D games.
6. Networking
- SocketIO: For WebSocket communication.
- Paramiko: For SSH and SFTP.
- Twisted: For event-driven networking.
7. Database Handling
- SQLAlchemy: For database access and object-relational mapping (ORM).
- PyMongo: For MongoDB interaction.
- Psycopg2: For working with PostgreSQL databases.
8. Cryptography and Security
- Cryptography: For secure encryption and decryption.
- PyJWT: For JSON Web Tokens (JWT) authentication.
- Passlib: For password hashing.
9. GUI Development
- PyQt: For building cross-platform graphical applications.
- Kivy: For developing multi-touch applications.
- Tkinter: The standard GUI toolkit for Python.
10. Testing
- pytest: A powerful framework for testing.
- unittest: Built-in testing framework (but pytest is more flexible).
- Mock: For mocking objects in tests.
11. File Handling
- PyPDF2: For working with PDF files.
- OpenPyXL: For reading and writing Excel files.
- Pillow: For image manipulation and processing.
12. Other Popular Libraries
- pytz: For timezone handling.
- Arrow: For working with dates and times in an easy and human-friendly way.
- Shapely: For geometric operations.
- Geopy: For geocoding and working with geographic data.
- MoviePy: For video editing.
13. AI and Natural Language Processing (NLP)
- NLTK: For natural language processing.
- spaCy: Another NLP library for processing large text datasets.
- OpenCV: For computer vision and image processing.
- transformers (by Hugging Face): For working with state-of-the-art NLP models.

Leave a Reply