External Modules in Python (original) (raw)

External modules are collections of pre-written code created by other programmers. They add extra features for tasks like web development, working with data, machine learning or web scraping. Using these modules saves time and makes coding easier because you don’t have to write everything from scratch.

External modules can be easily installed and managed using Python’s package manager, pip. **For example:

pip install requests

Here are some commonly used external modules in Python:

Examples of External Modules

Example 1: Using requests to Fetch Data from an API

The "requests" module simplifies the process of making HTTP requests in Python. We can install this module by executing the below command in terminal.

pip install requests

This example shows how to connect to an API and check the response status.

Python `

import requests

response = requests.get("https://api.github.com")

if response.status_code == 200: print("Successfully connected to GitHub API!") else: print("Failed to connect.")

`

**Output

Successfully connected to GitHub API!

**Explanation:

Example 2: Using pandas to Read a CSV File

Pandas is a tool used for data manipulation and analysis in Python. Using pandas, we can handle vast datasets, manipulate data structures, and perform complex data operations with ease.

To install "pandas" module execute the below command in terminal.

pip install pandas

Below is a simple example showcasing reading a CSV file and displaying its first five rows:

Python `

import pandas as pd df = pd.read_csv("datafile.csv") print(df.head())

`

**Ouput
Screenshot-2023-10-17-124842

**Explanation:

Example 3: Using numpy for Numerical Calculations

Numpy library is used for numerical computing in Python. It provides support for large multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these arrays.

We can install this library by executing below command in our terminal.

pip install numpy

In the below code, we create a one dimensional array after that we print the mean of all the elements in an array using mean() function.

Python `

import numpy as np arr = np.array([1, 2, 3, 4, 5]) print("Mean:", np.mean(arr))

`

**Explanation:

Example 4: Using matplotlib to Plot Data

Matplotlib is a plotting library that is used to visualize large datasets. To install "matplotlib" execute the below command in the terminal.

pip install matplotlib

In the below example, we draw a simple plot by using the sample data set.

Python `

import matplotlib.pyplot as plt

x = [0, 1, 2, 3, 4] y = [0, 1, 4, 9, 16] plt.plot(x, y) plt.show()

`

**Output

Screenshot-2023-10-17-132411

**Explanation:

Example 5: Using Flask to Create a Simple Web App

Flask is a framework for web development in Python. It allows for the rapid development of web applications. We can install flask by executing the below command in terminal.

pip install flask

In the below example, we have written the script to display the message "Hello, Flask!" on the webpage.

Python `

from flask import Flask app = Flask(name)

@app.route("/home") def home(): return "Hello, Flask!"

if name == "main": app.run()

`