Session 4: Introduction to Libraries and Modules

Instructor : Seyed Sadjad Abedi-Shahri

Python provides a modular structure that allows you to organize your code into reusable components called modules and packages. These modules and packages can contain variables, functions, classes, and even other modules and packages.

This modular approach promotes code reuse, maintainability, and collaboration within the Python community.

Modules

A module in Python is a single file containing Python code, such as functions, classes, or variables. Modules help organize code into logical units and promote code reuse across different parts of your program or across different projects.

Packages

A package is a collection of modules organized into a directory structure. Packages allow you to hierarchically organize related modules, making it easier to manage and distribute larger code bases.

A package is essentially a directory that contains an __init__.py file, which marks the directory as a Python package. The __init__.py file can contain initialization code or be left empty.

Importing and Using Modules

To use a module or package in your Python code, you need to import it. The import statement allows you to access and use the code defined in the imported module or package.

Importing Modules

There are several ways to import modules in Python:

  1. Import the entire module: import module_name
  2. Import specific objects from a module: from module_name import object1, object2
  3. Import a module with an alias: import module_name as alias
  4. Import all objects from a module: from module_name import * (not recommended, as it can lead to naming conflicts)

Example:

1import math  # Import the entire math module
2print(math.pi)  # Access the pi constant from the math module
3
4from math import sqrt  # Import the sqrt function from the math module
5print(sqrt(16))  # Use the imported sqrt function
6
7import random as rd  # Import the random module with an alias
8print(rd.randint(1, 10))  # Use the randint function from the random module

Importing Packages

To import modules from a package, you need to use the package hierarchy in your import statement.

1import package_name.module_name
2from package_name.module_name import object1, object2

Installing External Libraries

Python has a vast ecosystem of external libraries and packages that provide additional functionality beyond the standard library. These external libraries can be installed using package managers like pip (Python Package Installer) or conda (Anaconda’s package manager).

To install an external library (e.g., NumPy or Matplotlib), you can open the Anaconda Prompt (on Windows) or the terminal (on macOS/Linux) and run the following command:

1conda install library_name

Replace library_name with the name of the library you want to install (e.g., numpy or matplotlib).

After installing a library, you can import and use it in your Python code like any other module or package.

Built-in Libraries

Python comes with a comprehensive standard library that provides a wide range of modules for various tasks. Here are a few examples of commonly used built-in modules:

  • math: This module provides access to mathematical functions and constants, such as pi, sin(), cos(), sqrt(), and more.
  • random: This module provides functions for generating random numbers and making random choices.
  • os: The os module allows you to interact with the operating system, including functions for file and directory operations, environment variables, and more.
  • re: The re module provides support for regular expressions, which are powerful patterns for text processing and manipulation.
  • datetime: This module supplies classes for working with dates, times, and time intervals, making it easier to perform date and time operations.
  • json: The json module allows you to encode and decode JSON (JavaScript Object Notation) data, which is a lightweight data interchange format.

Examples of math Module

1import math
2
3print(math.pi)        # Output: 3.141592653589793 (pi constant)
4print(math.sqrt(16))  # Output: 4.0 (square root)
5print(math.sin(0))    # Output: 0.0 (sine function)
6print(math.cos(math.pi / 2))  # Output: 6.123233995736766e-17 (cosine function)
7print(math.floor(3.7))  # Output: 3 (floor function)
8print(math.ceil(3.2))   # Output: 4 (ceiling function)

Examples of random Module

 1import random
 2
 3print(random.random())  # Output: A random float between 0 and 1
 4print(random.uniform(1, 10))  # Output: A random float between 1 and 10
 5print(random.randint(1, 6))  # Output: A random integer between 1 and 6 (inclusive)
 6
 7my_list = [1, 2, 3, 4, 5]
 8print(random.choice(my_list))  # Output: A random element from the list
 9random.shuffle(my_list)  # Shuffle the elements of the list in-place
10print(my_list)  # Output: The shuffled list

The Python ecosystem is enriched by a vast collection of external libraries and packages contributed by the community. These libraries offer specialized functionality for various domains. Here’s a description of some popular external libraries:

  • NumPy: A powerful library for numerical computing, providing support for large, multi-dimensional arrays and matrices, along with a vast collection of mathematical functions to operate on these arrays.
  • Matplotlib: A comprehensive data visualization library for creating static, animated, and interactive visualizations in Python, including plots, histograms, bar charts, scatter plots, and more.
  • Pandas: A high-performance, easy-to-use data manipulation and analysis library for working with structured (tabular, multidimensional, heterogeneous) and time-series data.
  • SciPy: A collection of mathematical algorithms and convenience functions for scientific and technical computing, including modules for optimization, linear algebra, integration, interpolation, and more.
  • Scikit-learn: A machine learning library that provides simple and efficient tools for data mining and data analysis, including classification, regression, clustering, dimensionality reduction, and more.
  • TensorFlow: A powerful library for machine learning and deep learning, used for building and deploying neural networks and other machine learning models.
  • Requests: A simple and elegant library for making HTTP requests, handling cookies, file uploads, and more.
  • Django: A high-level Python web framework that encourages rapid development and clean, pragmatic design for web applications.
  • Flask: A lightweight, flexible, and minimalist Python web framework for building web applications.

Introducing NumPy (for Numerical Computing)

NumPy (Numerical Python) is a fundamental library for scientific computing in Python. It provides support for large, multi-dimensional arrays and matrices, as well as a vast collection of mathematical functions to operate on these arrays efficiently.

NumPy is widely used in various fields, including data analysis, machine learning, scientific computing, and more. It serves as the foundation for many other scientific libraries in the Python ecosystem, such as Pandas, SciPy, and Matplotlib.

NumPy Arrays

The core data structure in NumPy is the ndarray (N-dimensional array). NumPy arrays are efficient, homogeneous (containing elements of the same data type), and provide a wide range of operations and functions to work with them.

1import numpy as np
2
3# Creating NumPy arrays
4a = np.array([1, 2, 3, 4, 5])  # 1D array
5b = np.array([[1, 2], [3, 4]])  # 2D array
6c = np.zeros((3, 4))  # 3x4 array filled with zeros
7d = np.ones((2, 3, 4))  # 2x3x4 array filled with ones
8e = np.arange(10)  # 1D array with values from 0 to 9
9f = np.linspace(0, 1, 5)  # 1D array with 5 evenly spaced values between 0 and 1
 1# Array operations
 2print(a + 2)  # Output: [3 4 5 6 7] (Element-wise addition)
 3print(a * b)  # Output: [[ 1  4]
 4             #          [ 9 16]] (Element-wise multiplication)
 5print(np.sqrt(a))  # Output: [1.         1.41421356 1.73205081 2.         2.23606798] (Square root)
 6print(np.sum(b))  # Output: 10 (Sum of all elements)
 7print(np.mean(a))  # Output: 3.0 (Mean of the elements)
 8
 9# Array indexing and slicing
10print(b[0, :])  # Output: [1 2] (First row of b)
11print(b[:, 1])  # Output: [2 4] (Second column of b)
12print(b[1, 1])  # Output: 4 (Element at row 1, column 1 of b)
1# Array reshaping
2g = a.reshape(1, 5)  # Reshaping 1D array a into a 2D array with shape (1, 5)
3print(g)  # Output: [[1 2 3 4 5]]
4
5# Array concatenation
6h = np.concatenate((a, e), axis=0)  # Concatenate a and e along the first axis
7print(h)  # Output: [1 2 3 4 5 0 1 2 3 4 5 6 7 8 9]

Introducing Matplotlib (for Data Visualization)

Matplotlib is a comprehensive data visualization library in Python. It provides a wide range of tools and functions for creating static, animated, and interactive visualizations in various formats, including plots, histograms, bar charts, scatter plots, and more.

Basic Plotting

Matplotlib provides a MATLAB-like interface for creating plots. Here’s an example of creating a simple line plot:

 1import matplotlib.pyplot as plt
 2import numpy as np
 3
 4# Generate some data
 5x = np.linspace(0, 10, 100)
 6y = np.sin(x)
 7
 8# Create a figure and axes
 9fig, ax = plt.subplots()
10
11# Plot the data
12ax.plot(x, y)
13
14# Add labels and title
15ax.set_xlabel('X')
16ax.set_ylabel('Y')
17ax.set_title('Sine Wave')
18
19# Display the plot
20plt.show()

Plot Customization

Matplotlib offers extensive customization options for plots, allowing you to adjust colors, line styles, markers, legends, and more.

 1import matplotlib.pyplot as plt
 2import numpy as np
 3
 4# Generate some data
 5x = np.linspace(-10, 10, 100)
 6y1 = x**2
 7y2 = x**3
 8
 9# Create a figure and axes
10fig, ax = plt.subplots()
11
12# Plot the data
13ax.plot(x, y1, color='r', label='Square')
14ax.plot(x, y2, color='g', linestyle='--', marker='o', label='Cube')
15
16# Add labels, title, and legend
17ax.set_xlabel('X')
18ax.set_ylabel('Y')
19ax.set_title('Polynomial Plots')
20ax.legend()
21
22# Display the plot
23plt.show()

Other Visualization Types

Matplotlib supports various types of visualizations beyond line plots, including:

  • Scatter plots
  • Bar charts
  • Histograms
  • Pie charts
  • 3D plots
  • Contour plots
  • Image plots
 1import matplotlib.pyplot as plt
 2import numpy as np
 3
 4# Generate some data
 5data = np.random.normal(0, 1, 1000)
 6
 7# Create a figure and axes
 8fig, ax = plt.subplots()
 9
10# Plot a histogram
11ax.hist(data, bins=20, edgecolor='black')
12
13# Add labels and title
14ax.set_xlabel('Value')
15ax.set_ylabel('Frequency')
16ax.set_title('Histogram')
17
18# Display the plot
19plt.show()

Reading and Writing Files

Working with files is a fundamental aspect of many programming tasks. Python provides built-in functions and modules for reading and writing files, allowing you to interact with various file formats and perform file operations efficiently.

Reading Files

Python provides the open() function to open files for reading or writing. Here’s an example of reading the contents of a text file:

 1# Open a file in read mode
 2file = open('example.txt', 'r')
 3
 4# Read the entire file
 5content = file.read()
 6print(content)
 7
 8# Read a single line
 9line = file.readline()
10print(line)
11
12# Read all lines into a list
13lines = file.readlines()
14print(lines)
15
16# Close the file
17file.close()

It’s recommended to use the with statement when working with files, as it ensures the file is properly closed after the operations are completed, even in the case of exceptions or errors.

1# Open a file using the with statement
2with open('example.txt', 'r') as file:
3    # Read the file contents
4    content = file.read()
5    print(content)

Writing Files

Writing to files is similar to reading, but you need to open the file in write mode (w) or append mode (a).

1
2# Open a file in write mode (will create the file if it doesn't exist)
3with open('output.txt', 'w') as file:
4    file.write('This is a new line\n')
5    file.write('This is another line\n')
6
7# Open a file in append mode
8with open('output.txt', 'a') as file:
9    file.write('This line is appended\n')

Working with Other File Formats

Python’s standard library and third-party libraries provide support for working with various file formats, such as CSV, JSON, Excel, and more.

 1# Working with CSV files
 2import csv
 3
 4# Open a CSV file and read its contents
 5with open('data.csv', 'r') as file:
 6    reader = csv.reader(file)
 7    for row in reader:
 8        print(row)
 9
10# Open a CSV file and write data to it
11with open('output.csv', 'w', newline='') as file:
12    writer = csv.writer(file)
13    writer.writerow(['Name', 'Age', 'City'])
14    writer.writerow(['John', 25, 'New York'])
15    writer.writerow(['Jane', 30, 'London'])

Questions?

Email

Telegram