100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.6 TrustPilot
logo-home
Other

Libraries and Modules in Python: A Comprehensive Guide with Examples

Rating
-
Sold
-
Pages
6
Uploaded on
24-01-2025
Written in
2024/2025

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.

Show more Read less
Institution
Course

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

Written for

Institution
Course

Document information

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

Subjects

$7.39
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
rileyclover179

Get to know the seller

Seller avatar
rileyclover179 US
Follow You need to be logged in order to follow users or courses
Sold
0
Member since
1 year
Number of followers
0
Documents
252
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

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

Frequently asked questions