0% found this document useful (0 votes)
14 views

1974 - 987 - DOC - Python Modules and Libraries

Modules and libraries

Uploaded by

sumanthshetty080
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

1974 - 987 - DOC - Python Modules and Libraries

Modules and libraries

Uploaded by

sumanthshetty080
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Python Modules and Libraries: Student Handout

Introduction to Modules and Libraries


Modules and libraries in Python allow you to use pre-written code to solve problems efficiently.
Instead of writing everything from scratch, you can leverage existing solutions.

1. What is a Python Module?


A module is a file containing Python code, including functions, classes, and variables. You can
import a module into your program to use its code.

How to Use a Module?


To use a module, you need to import it using the import statement.

Example 1: Using the math Module

import math

# Calculate the square root of 16


result = math.sqrt(16)
print(result) # Output: 4.0

Example 2: Using the random Module

import random

# Generate a random number between 1 and 10


random_number = random.randint(1, 10)
print(random_number)

Example 3: Using the statistics Module


import statistics

# Calculate the mean of a list of numbers


data = [1, 2, 3, 4, 5]
mean_value = statistics.mean(data)
print(mean_value) # Output: 3

2. What is a Python Library?


A library is a collection of modules that are related to each other. Libraries help you perform
specific tasks without writing all the code yourself.

Popular Python Libraries


Example 1: NumPy

NumPy is used for numerical computations and provides support for arrays and matrices.

import numpy as np

# Create a NumPy array


array = np.array([1, 2, 3, 4, 5])
print(array)

Example 2: Pandas
Pandas is used for data manipulation and analysis, providing data structures like DataFrames.

import pandas as pd

# Create a DataFrame
data = {'Name': ['Alice', 'Bob'], 'Age': [25, 30]}
df = pd.DataFrame(data)
print(df)

Example 3: Matplotlib
Matplotlib is used for creating visualizations like graphs and charts.

import matplotlib.pyplot as plt

# Plot a simple line graph


plt.plot([1, 2, 3], [4, 5, 6])
plt.show()

3. Importing and Using Built-in Modules


Python has several built-in modules that you can use without installing anything.

Built-in Modules
Example 1: The datetime Module

import datetime

# Get the current date and time


current_time = datetime.datetime.now()
print(current_time)

Example 2: The os Module

import os

# Get the current working directory


current_directory = os.getcwd()
print(current_directory)

Example 3: The sys Module

import sys

# Get the list of command-line arguments


arguments = sys.argv
print(arguments)

4. Installing External Libraries


For external libraries, you need to install them using a package manager like pip .

Installing a Library
Example: Installing NumPy

pip install numpy

Conclusion
A module is a file containing Python code.
A library is a collection of related modules.
Use built-in modules like math , datetime , os , and sys without installation.
Install external libraries using pip .

By using modules and libraries, you can save time and effort in your programming tasks. Happy
coding!

You might also like