Popular External Libraries in Python

External libraries are additional packages created by the Python community.

They extend Python capabilities for:

  • Data Science
  • Web Development
  • Automation
  • APIs
  • Machine Learning
  • Web Scraping

Install libraries using:

pip install package_name

1. NumPy

NumPy stands for:

Numerical Python

Used for:

  • Numerical computing
  • Arrays
  • Mathematical operations
  • Scientific computing

Install NumPy

pip install numpy

Import NumPy

import numpy as np

Creating Arrays

import numpy as np

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

print(arr)

Output:

[1 2 3 4]

Array Operations

arr = np.array([1, 2, 3])

print(arr + 2)
print(arr * 2)

Output:

[3 4 5]
[2 4 6]

Useful NumPy Functions

print(np.zeros((2, 2)))
print(np.ones((3, 3)))
print(np.arange(1, 10))

2. Pandas

Pandas is used for:

  • Data analysis
  • Data cleaning
  • Working with tables and CSV files

Install Pandas

pip install pandas

Import Pandas

import pandas as pd

Creating DataFrame

import pandas as pd

data = {
"Name": ["Aditya", "Rahul"],
"Age": [25, 22]
}

df = pd.DataFrame(data)

print(df)

Output:

      Name  Age
0 Aditya 25
1 Rahul 22

Read CSV File

df = pd.read_csv("students.csv")

print(df.head())

Data Information

print(df.info())
print(df.describe())

Select Column

print(df["Name"])

3. Matplotlib

Matplotlib is used for:

  • Data visualization
  • Charts and graphs

Install Matplotlib

pip install matplotlib

Import Matplotlib

import matplotlib.pyplot as plt

Line Chart

import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [10, 20, 30, 40]

plt.plot(x, y)

plt.xlabel("X-axis")
plt.ylabel("Y-axis")

plt.title("Line Chart")

plt.show()

Bar Chart

subjects = ["Python", "Java", "C++"]
marks = [90, 80, 70]

plt.bar(subjects, marks)

plt.show()

Pie Chart

labels = ["Python", "Java", "C++"]
sizes = [50, 30, 20]

plt.pie(sizes, labels=labels)

plt.show()

4. Requests

Requests library is used to:

  • Send HTTP requests
  • Work with APIs
  • Fetch web data

Install Requests

pip install requests

Import Requests

import requests

GET Request

import requests

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

print(response.status_code)
print(response.text)

JSON Response

data = response.json()

print(data)

POST Request

data = {
"name": "Aditya"
}

response = requests.post(
"https://httpbin.org/post",
data=data
)

print(response.text)

5. BeautifulSoup

BeautifulSoup is used for:

  • Web scraping
  • Extracting HTML data

Install BeautifulSoup

pip install beautifulsoup4

Import BeautifulSoup

from bs4 import BeautifulSoup

Example HTML Parsing

from bs4 import BeautifulSoup

html = """
<html>
<h1>Python</h1>
</html>
"""

soup = BeautifulSoup(html, "html.parser")

print(soup.h1.text)

Output:

Python

Extract Links

html = """
<a href="https://example.com">Visit</a>
"""

soup = BeautifulSoup(html, "html.parser")

link = soup.a["href"]

print(link)

6. Flask

Flask is a lightweight Python web framework.

Used for:

  • Web applications
  • APIs
  • Backend development

Install Flask

pip install flask

Simple Flask App

from flask import Flask

app = Flask(__name__)

@app.route("/")
def home():
return "Welcome to Flask"

app.run(debug=True)

Flask Route Example

@app.route("/about")
def about():
return "About Page"

Running Flask App

python app.py

Open browser:

http://127.0.0.1:5000

7. Django

Django is a powerful full-stack web framework.

Used for:

  • Large web applications
  • Secure websites
  • Database-driven projects

Install Django

pip install django

Create Django Project

django-admin startproject myproject

Run Django Server

python manage.py runserver

Create Django App

python manage.py startapp myapp

Simple Django View

from django.http import HttpResponse

def home(request):
return HttpResponse("Welcome to Django")

Flask vs Django

FeatureFlaskDjango
TypeLightweightFull-stack
FlexibilityHighMedium
LearningEasierMore complex
Best ForSmall apps/APIsLarge applications

Practical Example

Fetch API Data Using Requests

import requests

url = "https://api.github.com"

response = requests.get(url)

if response.status_code == 200:
data = response.json()
print(data)
else:
print("Request failed")

Advantages of External Libraries

✅ Faster development
✅ Powerful features
✅ Large community support
✅ Reusable code
✅ Professional project support


Summary

In this chapter you learned:

✅ NumPy
✅ Pandas
✅ Matplotlib
✅ Requests
✅ BeautifulSoup
✅ Flask
✅ Django