Common Python Libraries and Practical Projects: NumPy, Pandas, Matplotlib, Scikit‑learn, Requests, Beautiful Soup, Selenium, Pygame, Flask, PyTorch
This article introduces ten widely used Python libraries—NumPy, Pandas, Matplotlib, Scikit‑learn, Requests, Beautiful Soup, Selenium, Pygame, Flask, and PyTorch—each accompanied by a concise real‑world project and complete code examples to help readers understand and apply them effectively.
Python is a powerful language with many libraries and tools that simplify numerous everyday tasks. This article introduces common Python libraries and provides a practical project for each to help you understand and master them.
1. NumPy – NumPy is a numerical‑computing library for efficiently handling large arrays and matrices, offering many mathematical functions for linear algebra, Fourier transforms, and more.
Practical project: Use NumPy to compute all vectors on a plane formed by two orthogonal vectors, employing arange(), reshape(), dot, and cross functions.
import numpy as np
# generate two vectors
a = np.arange(0, 10, 2)
b = np.arange(5)
# compute dot product array
dot_product = np.dot(np.array(np.meshgrid(a, b)).T.reshape(-1, 2), [1, -1])
# compute cross product array
cross_product = np.cross(np.array(np.meshgrid(a, b)).T.reshape(-1, 2), [1, -1])
print('All Vectors:')
print(np.array(np.meshgrid(a, b)).T.reshape(-1, 2))
print('Dot Product Vectors:')
print(dot_product)
print('Cross Product Vectors:')
print(cross_product)2. Pandas – Pandas is a data‑analysis library that provides powerful data structures such as the DataFrame for reading, filtering, slicing, aggregating, and visualising data.
Practical project: Analyse a "pirate treasure" CSV file to list pirate names, total gold, average gold per pirate, and average treasure value.
import pandas as pd
# read CSV file into DataFrame
df = pd.read_csv('pirate_treasure.csv')
# print all pirate names
pirate_names = df['pirate']
print('
All Pirate Names:')
print(pirate_names)
# total gold
total_gold = df['gold'].sum()
print('
Total Gold Across All Pirates:')
print(total_gold)
# average gold per pirate
average_gold = df['gold'].mean()
print('
Average Gold Per Pirate:')
print(average_gold)
# average treasure value (gold + gems) per pirate
average_treasure_value = df[['gold', 'gems']].sum().sum() / df.shape[0]
print('
Average Treasure Value:')
print(average_treasure_value)3. Matplotlib – Matplotlib is a data‑visualisation library for creating a wide variety of charts and plots with extensive customisation options.
Practical project: Create a bar chart showing temperatures of several cities.
import matplotlib.pyplot as plt
# data
cities = ['New York', 'London', 'Paris', 'Tokyo', 'Shanghai']
temperatures = [18, 15, 19, 21, 23]
# bar chart
plt.bar(cities, temperatures)
plt.title('Temperature by City')
plt.xlabel('City')
plt.ylabel('Temperature (Celsius)')
plt.show()4. Scikit‑learn – Scikit‑learn is a machine‑learning library offering many algorithms and tools for classification, clustering, regression, feature extraction, model selection, and hyper‑parameter tuning.
Practical project: Build a Naïve Bayes classifier to identify spam emails.
from sklearn.naive_bayes import MultinomialNB
from sklearn.feature_extraction.text import CountVectorizer
# training data (1 = spam, 0 = ham)
train_data = ['you won the lottery', 'to the moon and back', 'make a million dollars fast', 'hey john how are you', 'how about lunch today', 'earn money from home', 'get rich quick', 'john how about dinner tonight']
train_labels = [1, 0, 1, 0, 0, 1, 1, 0]
# vectoriser
count_vectorizer = CountVectorizer()
train_array = count_vectorizer.fit_transform(train_data)
# train classifier
naive_bayes_classifier = MultinomialNB()
naive_bayes_classifier.fit(train_array, train_labels)
# test
test_email = ['how about earning money from home']
test_array = count_vectorizer.transform(test_email)
predicted_label = naive_bayes_classifier.predict(test_array)
print(predicted_label)5. Requests – Requests is an HTTP library that simplifies sending GET, POST, PUT, DELETE and other HTTP requests, supporting SSL and proxies.
Practical project: Fetch and display the HTML content of the Python official website.
import requests
# get web page
response = requests.get('https://www.python.org/')
print('HTTP Status code: ' + str(response.status_code))
print(response.text)6. Beautiful Soup – Beautiful Soup parses HTML and XML documents, enabling easy extraction, searching, and navigation of elements.
Practical project: Retrieve a web page with Requests and parse its title and first paragraph using Beautiful Soup.
import requests
from bs4 import BeautifulSoup
response = requests.get('https://www.python.org/')
soup = BeautifulSoup(response.text, 'html.parser')
print('Page Title: ' + soup.title.string)
print('First Paragraph: ' + soup.p.string)7. Selenium – Selenium automates browsers for testing and web‑scraping, allowing simulation of user interactions.
Practical project: Automate logging into Twitter and posting a tweet.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
# specify Chrome driver path
driver = webdriver.Chrome('/usr/local/bin/chromedriver')
# open Twitter and log in
driver.get('https://twitter.com/')
sleep(3)
login = driver.find_elements_by_xpath('//*[@id="doc"]/div[1]/div/div[1]/div[2]/a[3]')
login[0].click()
user = driver.find_elements_by_xpath('//*[@id="login-dialog-dialog"]/div[2]/div[2]/div[2]/form/div[1]/input')
user[0].send_keys('YourUsername')
user = driver.find_element_by_xpath('//*[@id="login-dialog-dialog"]/div[2]/div[2]/div[2]/form/div[2]/input')
user.send_keys('YourPassword')
LOG = driver.find_elements_by_xpath('//*[@id="login-dialog-dialog"]/div[2]/div[2]/div[2]/form/div[3]/button')
LOG[0].click()
sleep(3)
# post tweet
tweet = driver.find_elements_by_xpath('//*[@id="tweet-box-home-timeline"]')
tweet[0].send_keys('This is a tweet from Python!')
button = driver.find_elements_by_xpath('//*[@id="timeline"]/div[2]/div/form/div[3]/div[2]/button')
button[0].click()
print('Done')8. Pygame – Pygame is a multimedia library for creating games and interactive applications.
Practical project: Build a keyboard‑reaction survival game that tracks score and moves the player with arrow keys.
import pygame
import random
# initialise pygame
pygame.init()
# screen size
width = 800
height = 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Keyboard Reaction Game")
# game timer
game_time = 30
timer = pygame.time.Clock()
# player and food size
player_size = 50
food_size = 25
# colours
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
# player and food positions
player_x = width * 0.5 - player_size * 0.5
player_y = height * 0.5 - player_size * 0.5
food_x = random.randint(food_size, width - food_size)
food_y = random.randint(food_size, height - food_size)
score = 0
game_over = False
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player_x -= 10
elif event.key == pygame.K_RIGHT:
player_x += 10
elif event.key == pygame.K_UP:
player_y -= 10
elif event.key == pygame.K_DOWN:
player_y += 10
screen.fill(white)
pygame.draw.rect(screen, red, [food_x, food_y, food_size, food_size])
pygame.draw.rect(screen, black, [player_x, player_y, player_size, player_size])
if abs(player_x - food_x) < food_size and abs(player_y - food_y) < food_size:
score += 1
food_x = random.randint(food_size, width - food_size)
food_y = random.randint(food_size, height - food_size)
font = pygame.font.Font(None, 36)
text = font.render("Score: " + str(score), True, black)
screen.blit(text, (10, 10))
pygame.display.update()
game_time -= 1
if game_time == 0:
game_over = True
else:
timer.tick(60)
# final score display
font = pygame.font.Font(None, 72)
text = font.render("Final Score: " + str(score), True, black)
screen.blit(text, (width * 0.5 - text.get_width() * 0.5, height * 0.5 - text.get_height() * 0.5))
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()9. Flask – Flask is a lightweight web framework for building web applications and APIs.
Practical project: Create a simple web app with a form; submitted text is displayed on a result page.
from flask import Flask, render_template, request
# create app
app = Flask(__name__)
# form submission route
@app.route('/submit', methods=['POST'])
def submit():
text = request.form['text']
return render_template('submit.html', text=text)
# main route
@app.route('/')
def index():
return render_template('index.html')
# run app
if __name__ == '__main__':
app.run(debug=True)10. PyTorch – PyTorch is a scientific‑computing package for building deep‑learning neural networks with flexibility and speed.
Practical project: Build and train a neural network to recognise handwritten digits from the MNIST dataset.
import torch
import torch.nn as nn
import torchvision.datasets as dsets
import torchvision.transforms as transforms
# define network
class NeuralNet(nn.Module):
def __init__(self, input_size, hidden_size, num_classes):
super(NeuralNet, self).__init__()
self.fc1 = nn.Linear(input_size, hidden_size)
self.relu = nn.ReLU()
self.fc2 = nn.Linear(hidden_size, num_classes)
def forward(self, x):
out = self.fc1(x)
out = self.relu(out)
out = self.fc2(out)
return out
# hyper‑parameters
input_size = 784
hidden_size = 500
num_classes = 10
num_epochs = 5
batch_size = 100
learning_rate = 0.001
# data loaders
train_dataset = dsets.MNIST(root='data', train=True, transform=transforms.ToTensor(), download=True)
test_dataset = dsets.MNIST(root='data', train=False, transform=transforms.ToTensor())
train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=False)
# model, loss, optimizer
neural_net = NeuralNet(input_size, hidden_size, num_classes)
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(neural_net.parameters(), lr=learning_rate)
# training loop
total_step = len(train_loader)
for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader):
images = images.reshape(-1, 28*28)
outputs = neural_net(images)
loss = criterion(outputs, labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (i+1) % 100 == 0:
print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, i+1, total_step, loss.item()))
# testing
with torch.no_grad():
correct = 0
total = 0
for images, labels in test_loader:
images = images.reshape(-1, 28*28)
outputs = neural_net(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print('Accuracy of the network on the 10000 test images: {} %'.format(100 * correct / total))These examples demonstrate how a variety of Python libraries can be applied to real‑world tasks ranging from data analysis and visualisation to machine learning, web automation, game development, and deep learning.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
