• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 2 out of 6 pages
Other

Libraries and Modules in Python: A Comprehensive Guide with Examples

Document preview thumbnail
Preview 2 out of 6 pages

This document introduces libraries and modules in Python, explaining how to use built-in and external libraries. Learn about Python’s standard library, importing modules, and popular libraries like NumPy, pandas, and Matplotlib with practical examples. Perfect for first-year Computer Science students.

Content preview

Libraries and Modules in Python
Libraries and modules are essential to Python, enabling developers to reuse code,
reduce redundancy, and leverage powerful prebuilt functionalities. Python comes
with a rich standard library, and you can also install third-party libraries to extend
its capabilities.



1. What are Modules and Libraries?
 A module is a file containing Python code (functions, classes, variables) that
can be imported and reused.
 A library is a collection of modules packaged together for specific tasks.

Example:

A single .py file is a module, whereas libraries like NumPy or Pandas contain
multiple modules.



2. Importing Modules
Python provides the import statement to use modules in your program.

Importing a Module:

import math
print(math.sqrt(16)) # Output: 4.0

Importing Specific Functions or Variables:

from math import sqrt, pi
print(sqrt(25)) # Output: 5.0
print(pi) # Output: 3.141592653589793

Using Aliases:

, import math as m
print(m.pow(2, 3)) # Output: 8.0

Importing All Functions (Not Recommended):

from math import *
print(sin(pi / 2)) # Output: 1.0


3. Built-in Libraries
Python's standard library includes modules for common tasks. Here are some
popular ones:

1. os: Operating System Operations

import os
print(os.getcwd()) # Get the current working directory
os.mkdir("test_folder") # Create a new folder
os.rmdir("test_folder") # Remove the folder

2. sys: System-Specific Parameters and Functions

import sys
print(sys.version) # Python version
print(sys.argv) # Command-line arguments

3. datetime: Date and Time Operations

from datetime import datetime
now = datetime.now()
print(now.strftime("%Y-%m-%d %H:%M:%S")) # Output: e.g., 2024-12-24
10:30:45




4. random: Generating Random Numbers

Document information

Uploaded on
January 24, 2025
Number of pages
6
Written in
2024/2025
Type
Other
Person
Unknown
$7.39

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Sold
0
Followers
0
Items
252
Last sold
-



Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions