NumPy is the fundamental package for scientific computing in Python. It provides the ndarray object for efficient array operations.
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr.shape) # (5,)
print(arr.reshape(5, 1))
zeros = np.zeros((3, 4))
ones = np.ones((2, 3))
range_arr = np.arange(0, 10, 2) # [0, 2, 4, 6, 8]
matrix = np.array([[1, 2], [3, 4]])
print(matrix + 10) # Broadcasting
print(matrix @ matrix.T) # Matrix multiplication
Pandas provides Series and DataFrame for data manipulation and analysis.
import pandas as pd
data = {
"Name": ["Alice", "Bob", "Charlie"],
"Age": [25, 30, 35],
"Salary": [50000, 60000, 70000]
}
df = pd.DataFrame(data)
print(df.head())
print(df.describe())
grouped = df.groupby("Age").mean()
merged = pd.merge(df, other_df, on="id")
df.to_csv("output.csv", index=False)
read_df = pd.read_csv("data.csv")
SciPy builds on NumPy and provides modules for optimization, statistics, integration, and signal processing.
from scipy.optimize import minimize
from scipy import stats
from scipy.integrate import quad
# Optimization
result = minimize(lambda x: x**2 + 10, x0=2)
print(result.x)
# Statistics
data = [1, 2, 3, 4, 5]
mean = stats.tmean(data)
z_scores = stats.zscore(data)
# Integration
area, error = quad(lambda x: x**2, 0, 1)
print(f"Area under x^2 from 0 to 1: {area}")
Matplotlib is the standard plotting library in Python.
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y, marker="o", linestyle="--", color="b")
plt.title("Simple Line Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.grid(True)
plt.show()
# Bar chart and histogram
categories = ["A", "B", "C"]
values = [3, 7, 5]
plt.bar(categories, values)
data = [1, 2, 2, 3, 3, 3, 4, 4, 5]
plt.hist(data, bins=5)
# Subplots
fig, axes = plt.subplots(2, 2)
axes[0, 0].plot(x, y)
axes[0, 1].bar(categories, values)
plt.tight_layout()
Django is a high-level web framework following the MTV (Model-Template-View) pattern. It includes an ORM, admin interface, URL routing, and templating.
# models.py
from django.db import models
class BlogPost(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
pub_date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title
# views.py
from django.shortcuts import render
from .models import BlogPost
def home(request):
posts = BlogPost.objects.all()
return render(request, "home.html", {"posts": posts})
# urls.py
from django.urls import path
from . import views
urlpatterns = [
path("", views.home, name="home"),
]
import cv2
img = cv2.imread("image.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 100, 200)
cv2.imshow("Original", img)
cv2.imshow("Edges", edges)
cv2.waitKey(0)
cv2.destroyAllWindows()
pip install numpy pandas scipy matplotlib django opencv-python.