Advanced Python & Applications
Working with Libraries, Databases, Web Development & Machine Learning
Comprehensive Study Notes with Examples & Outputs
1. NumPy — Numerical Python
NumPy is the core library for numerical computing in Python. It provides the ndarray (n-dimensional
array) object and highly optimised mathematical operations on arrays, forming the backbone of the
entire scientific Python ecosystem.
1.1 Installation & Import
Shell / Python
pip install numpy
import numpy as np
1.2 Creating Arrays
Python
import numpy as np
# 1D array from a list
a = [Link]([1, 2, 3, 4, 5])
# 2D array (matrix)
b = [Link]([[1, 2, 3],
[4, 5, 6]])
# Special arrays
zeros = [Link]((3, 3)) # 3x3 matrix of zeros
ones = [Link]((2, 4)) # 2x4 matrix of ones
eye = [Link](3) # 3x3 identity matrix
rng = [Link](0, 10, 2) # [0 2 4 6 8]
linsp = [Link](0, 1, 5) # 5 evenly spaced values
print("1D array:", a)
print("2D array:\n", b)
print("Zeros:\n", zeros)
print("Arange:", rng)
print("Linspace:", linsp)
1
1.3 Array Properties
Python
b = [Link]([[1, 2, 3], [4, 5, 6]])
print("Shape :", [Link]) # (rows, cols)
print("Size :", [Link]) # total elements
print("Dtype :", [Link]) # data type
print("Ndim :", [Link]) # dimensions
Output
Shape : (2, 3)
Size : 6
Dtype : int64
Ndim : 2
1.4 Array Operations
Python
a = [Link]([10, 20, 30, 40])
b = [Link]([1, 2, 3, 4])
print(a + b) # element-wise addition
print(a * b) # element-wise multiplication
print(a ** 2) # squaring
print([Link](a)) # square root
print([Link](a)) # mean value
print([Link](a)) # sum
print([Link](a)) # max value
print([Link](a)) # standard deviation
Output
[11 22 33 44]
[ 10 40 90 160]
[ 100 400 900 1600]
[3.162 4.472 5.477 6.324]
25.0
100
40
11.180
2
1.5 Indexing, Slicing & Reshaping
Python
m = [Link]([[1,2,3],[4,5,6],[7,8,9]])
print(m[0, 1]) # row 0, col 1 → 2
print(m[1:, :2]) # rows 1+, first 2 cols
print(m[m > 5]) # boolean masking
# Reshape
flat = [Link](1, 13)
matrix = [Link](3, 4)
print(matrix)
Output
2
[[4 5]
[7 8]]
[6 7 8 9]
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
1.6 Matrix Operations
Python
A = [Link]([[1, 2], [3, 4]])
B = [Link]([[5, 6], [7, 8]])
print("Dot product:\n", [Link](A, B))
print("Transpose:\n", A.T)
print("Determinant:", [Link](A))
print("Inverse:\n", [Link](A))
Output
Dot product:
[[19 22]
[43 50]]
Transpose:
[[1 3]
[2 4]]
Determinant: -2.0
Inverse:
[[-2. 1. ]
[ 1.5 -0.5]]
3
2. Pandas — Data Analysis Library
Pandas provides two key data structures: Series (1D labeled array) and DataFrame (2D table with rows
and columns). It is the standard tool for data cleaning, manipulation, and exploratory analysis in Python.
2.1 Series & DataFrame Creation
Python
import pandas as pd
# Series
s = [Link]([10, 20, 30], index=['a', 'b', 'c'])
print(s)
# DataFrame from dictionary
data = {
'Name': ['Asha', 'Rohan', 'Priya'],
'Age': [25, 30, 22],
'Salary': [50000, 70000, 45000]
}
df = [Link](data)
print(df)
Output
a 10
b 20
c 30
dtype: int64
Name Age Salary
0 Asha 25 50000
1 Rohan 30 70000
2 Priya 22 45000
2.2 Reading Data & Inspection
Python
# Read from CSV
df = pd.read_csv('[Link]')
[Link](5) # first 5 rows
[Link](3) # last 3 rows
[Link] # (rows, columns) tuple
[Link]() # column types + null counts
[Link]() # statistical summary
[Link] # column names
4
[Link] # data types per column
Output ([Link] example)
RangeIndex: 3 entries, 0 to 2
Data columns (total 3 columns):
# Column Non-Null Count Dtype
0 Name 3 non-null object
1 Age 3 non-null int64
2 Salary 3 non-null int64
dtypes: int64(2), object(1)
2.3 Selection, Filtering & Sorting
Python
# Column selection
print(df['Name'])
print(df[['Name', 'Salary']])
# Row selection by label / position
print([Link][0]) # by label index
print([Link][1]) # by integer position
# Filtering rows
print(df[df['Age'] > 23])
# Sorting
print(df.sort_values('Salary', ascending=False))
Output
# Filter Age > 23:
Name Age Salary
0 Asha 25 50000
1 Rohan 30 70000
# Sorted by Salary (desc):
Name Age Salary
1 Rohan 30 70000
0 Asha 25 50000
2 Priya 22 45000
2.4 Data Manipulation
Python
# Add a new column
df['Bonus'] = df['Salary'] * 0.10
# Drop a column
5
[Link]('Bonus', axis=1, inplace=True)
# Handle missing values
df2 = [Link]({'A': [1, None, 3], 'B': [4, 5, None]})
print([Link]().sum()) # count nulls per column
[Link](0, inplace=True) # fill nulls with 0
[Link](inplace=True) # drop rows that have any null
# Apply a custom function
df['Tax'] = df['Salary'].apply(lambda x: x * 0.30)
# GroupBy aggregation
print([Link]('Name')['Salary'].mean())
Output
A 1
B 1
dtype: int64
Name Age Salary Tax
0 Asha 25 50000 15000.0
1 Rohan 30 70000 21000.0
2 Priya 22 45000 13500.0
2.5 Merge, Join & Concatenate
Python
df1 = [Link]({'ID': [1, 2], 'Name': ['Asha', 'Rohan']})
df2 = [Link]({'ID': [1, 2], 'Dept': ['HR', 'IT']})
# Merge (SQL-style JOIN)
merged = [Link](df1, df2, on='ID')
print(merged)
# Concatenate rows
df3 = [Link]({'ID': [3], 'Name': ['Priya'], 'Dept': ['Finance']})
combined = [Link]([merged, df3], ignore_index=True)
print(combined)
Output
ID Name Dept
0 1 Asha HR
1 2 Rohan IT
ID Name Dept
0 1 Asha HR
1 2 Rohan IT
2 3 Priya Finance
6
3. Matplotlib — Data Visualisation
Matplotlib is Python's standard plotting library. The pyplot module provides a MATLAB-style interface for
creating a wide variety of charts and figures. It integrates seamlessly with NumPy and Pandas.
3.1 Installation & Import
Shell / Python
pip install matplotlib
import [Link] as plt
import numpy as np
3.2 Line Plot
Python
x = [1, 2, 3, 4, 5]
y = [2, 4, 1, 6, 3]
[Link](figsize=(6, 4))
[Link](x, y, color='blue', marker='o',
linestyle='--', linewidth=2, label='Sales')
[Link]('Monthly Sales')
[Link]('Month')
[Link]('Sales (units)')
[Link]()
[Link](True)
[Link]()
Output
[Displays a dashed blue line chart with circular markers,
title "Monthly Sales", labeled axes, legend, and grid lines]
3.3 Bar Chart
Python
categories = ['Python', 'Java', 'C++', 'JavaScript']
scores = [90, 75, 65, 80]
[Link](figsize=(6, 4))
[Link](categories, scores,
color=['#1D9E75', '#378ADD', '#D85A30', '#BA7517'])
[Link]('Programming Language Scores')
[Link]('Language')
[Link]('Score')
[Link]()
Output
[Displays a coloured bar chart with 4 bars,
each representing a programming language's score]
7
3.4 Histogram, Scatter Plot & Pie Chart
Python
# Histogram
data = [Link](1000)
[Link](data, bins=30, color='steelblue', edgecolor='white')
[Link]('Normal Distribution Histogram')
[Link]()
# Scatter Plot
x = [Link](50)
y = [Link](50)
[Link](x, y, color='coral', alpha=0.7)
[Link]('Scatter Plot')
[Link]()
# Pie Chart
sizes = [35, 25, 20, 20]
labels = ['Python', 'Java', 'C++', 'Other']
[Link](sizes, labels=labels, autopct='%1.1f%%',
startangle=90)
[Link]('Language Usage Share')
[Link]()
Output
[Histogram : bell-shaped distribution with 30 bins]
[Scatter : 50 random coral dots on x-y plane]
[Pie chart : 4 slices with percentage labels]
3.5 Subplots — Multiple Charts
Python
fig, axes = [Link](1, 2, figsize=(10, 4))
axes[0].plot([1, 2, 3], [4, 5, 6], 'g-o')
axes[0].set_title('Line Plot')
axes[1].bar(['A', 'B', 'C'], [7, 3, 9], color='orange')
axes[1].set_title('Bar Chart')
plt.tight_layout()
[Link]()
Output
[Side-by-side figures: green line plot on the left,
orange bar chart on the right, with tight layout spacing]
8
4. Database Connectivity — SQLite & MySQL
Python connects to relational databases using the built-in sqlite3 module (no installation required) or
mysql-connector-python / MySQLdb for MySQL databases. Both follow the DB-API 2.0 standard interface.
4.1 SQLite3 — Connect & Create Table
Python
import sqlite3
# Connect to database file (creates it if not present)
conn = [Link]('[Link]')
cursor = [Link]()
# Create a table
[Link]('''
CREATE TABLE IF NOT EXISTS students (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
marks REAL
)
''')
[Link]()
print("Table created successfully")
Output
Table created successfully
4.2 CRUD Operations — Insert, Read, Update, Delete
Python
# INSERT rows (parameterised queries prevent SQL injection)
[Link]("INSERT INTO students (name, marks) VALUES (?,
?)", ("Asha", 88.5))
[Link]("INSERT INTO students (name, marks) VALUES (?,
?)", ("Rohan", 92.0))
[Link]()
# READ all rows
[Link]("SELECT * FROM students")
rows = [Link]()
for row in rows:
print(row)
# UPDATE — change Asha's marks
9
[Link]("UPDATE students SET marks = ? WHERE name = ?",
(95.0, "Asha"))
[Link]()
# DELETE a row
[Link]("DELETE FROM students WHERE name = ?",
("Rohan",))
[Link]()
# Verify after update + delete
[Link]("SELECT * FROM students")
print([Link]())
[Link]()
Output
(1, 'Asha', 88.5)
(2, 'Rohan', 92.0)
# After UPDATE + DELETE:
[(1, 'Asha', 95.0)]
Note: Always use parameterised queries (? for SQLite, %s for MySQL). Never
concatenate user input directly into SQL strings — this prevents SQL injection
attacks.
4.3 MySQL Connection (mysql-connector-python)
Shell
pip install mysql-connector-python
Python
import [Link]
conn = [Link](
host = "localhost",
user = "root",
password = "yourpassword",
database = "school_db"
)
cursor = [Link]()
[Link]("SELECT * FROM students WHERE marks > 80")
results = [Link]()
for row in results:
print(row)
10
[Link]()
Output
(1, 'Asha', 95.0)
(3, 'Priya', 89.5)
4.4 Using Pandas with SQLite
Python
import pandas as pd
import sqlite3
conn = [Link]('[Link]')
# Read SQL query directly into a DataFrame
df = pd.read_sql_query("SELECT * FROM students", conn)
print(df)
# Write a DataFrame back to a SQL table
df.to_sql('students_backup', conn, if_exists='replace',
index=False)
[Link]()
Output
id name marks
0 1 Asha 95.0
11
5. Web Development — Flask & Django
Flask is a lightweight micro web framework ideal for APIs and small applications. Django is a full-stack
framework with built-in ORM, admin panel, and authentication — suited for larger applications.
5.1 Flask — Minimal Application
Shell
pip install flask
Python — [Link]
from flask import Flask, request, jsonify
app = Flask(__name__)
# Route: Home page
@[Link]('/')
def home():
return "Welcome to Flask!"
# Route with URL parameter
@[Link]('/greet/<name>')
def greet(name):
return f"Hello, {name}!"
# GET endpoint returning JSON
@[Link]('/api/data', methods=['GET'])
def get_data():
data = {"status": "ok", "items": [1, 2, 3]}
return jsonify(data)
# POST endpoint — receives JSON body
@[Link]('/api/submit', methods=['POST'])
def submit():
body = request.get_json()
return jsonify({"received": body}), 201
if __name__ == '__main__':
[Link](debug=True)
Output (terminal + browser)
* Running on [Link]
GET / -> "Welcome to Flask!"
GET /greet/Asha -> "Hello, Asha!"
12
GET /api/data -> {"items":[1,2,3],"status":"ok"}
POST /api/submit -> {"received": {...}}
5.2 Flask Templates (Jinja2)
templates/[Link]
<!DOCTYPE html>
<html>
<body>
<h1>Hello, {{ name }}!</h1>
{% for item in items %}
<p>{{ item }}</p>
{% endfor %}
</body>
</html>
[Link] — render template
from flask import render_template
@[Link]('/page')
def page():
return render_template('[Link]',
name='Manju', items=['Python', 'Flask', 'Jinja2'])
5.3 Django — Quick Start
Setup Commands
pip install django
django-admin startproject mysite
cd mysite
python [Link] startapp blog
python [Link] runserver
[Link]
from [Link] import HttpResponse
from [Link] import render
def home(request):
return HttpResponse("Welcome to Django!")
def dashboard(request):
context = {'user': 'Manju', 'score': 95}
return render(request, '[Link]', context)
13
[Link]
from [Link] import path
from . import views
urlpatterns = [
path('', [Link], name='home'),
path('dashboard/', [Link], name='dashboard'),
]
5.4 Flask vs Django — Comparison
Feature Flask Django
Type Micro-framework Full-stack framework
ORM No (use SQLAlchemy) Yes — built-in Django ORM
Admin Panel No Yes — auto-generated
Best for APIs, microservices Large web applications
Routing @[Link]() [Link] urlpatterns
Learning curve Easier / gentler Steeper initially
14
6. Introduction to Machine Learning
Machine Learning (ML) is a branch of Artificial Intelligence where algorithms learn patterns from data to
make predictions or decisions without being explicitly programmed for each task.
6.1 Types of Machine Learning
Supervised Learning
The algorithm learns from labelled training data (input → known output). Examples: Classification and
Regression.
• Algorithms: Linear Regression, Logistic Regression, Decision Tree, SVM, KNN, Random Forest
• Use cases: Email spam detection, house price prediction, disease diagnosis
Unsupervised Learning
No labels are provided; the algorithm finds hidden patterns or groupings in data.
• Algorithms: K-Means, DBSCAN, PCA, Autoencoders
• Use cases: Customer segmentation, anomaly detection, topic modelling
Reinforcement Learning
An agent learns by interacting with an environment, receiving rewards for correct actions and penalties for
wrong ones.
• Algorithms: Q-Learning, Deep Q-Network (DQN), PPO
• Use cases: Game AI, robotics, autonomous vehicles
6.2 Standard ML Workflow (Pipeline)
Steps
Step 1 : Collect & Load Data
Step 2 : Explore Data (EDA) — shape, nulls, distributions,
correlations
Step 3 : Preprocess — handle nulls, encode categories, scale
features
Step 4 : Split — train_test_split (80/20 or 70/30)
Step 5 : Choose Model — e.g. LogisticRegression, RandomForest
Step 6 : Train — [Link](X_train, y_train)
Step 7 : Predict — [Link](X_test)
Step 8 : Evaluate — accuracy, precision, recall, F1-score,
RMSE
Step 9 : Tune — GridSearchCV, cross-validation
15
Step 10 : Deploy — save with joblib/pickle, serve via Flask
API
6.3 Key Terminology
Glossary
Feature (X) : Input variable (column) used for prediction
Label / Target : Output variable to predict (y)
Training set : Data used to fit the model
Test set : Unseen data to evaluate performance
Overfitting : Model too complex; memorises training data
but fails on new data
Underfitting : Model too simple; misses patterns in the
data
Bias : Error from overly simplistic assumptions in
the model
Variance : Sensitivity of model to small fluctuations
in training data
Cross-validation : Evaluate model performance on multiple data
splits
Hyperparameter : Configuration setting of the model (e.g.
n_neighbors in KNN)
7. Scikit-learn — Machine Learning Library
Scikit-learn is Python's primary ML library. It provides a consistent API for classification, regression,
clustering, preprocessing, and model evaluation, making it easy to experiment with many algorithms.
7.1 Linear Regression
Python
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from [Link] import mean_squared_error, r2_score
import numpy as np
# Sample data: study hours → exam score
X = [Link]([[1],[2],[3],[4],[5],[6],[7],[8]])
y = [Link]([40, 50, 55, 65, 70, 80, 85, 90])
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=42)
model = LinearRegression()
[Link](X_train, y_train)
y_pred = [Link](X_test)
print("Predictions:", y_pred)
16
print("R2 Score :", round(r2_score(y_test, y_pred), 3))
print("RMSE :", round(mean_squared_error(y_test,
y_pred)**0.5, 3))
print("Intercept :", model.intercept_)
print("Coefficient:", model.coef_)
Output
Predictions: [54.2 72.6]
R2 Score : 0.978
RMSE : 2.341
Intercept : 31.25
Coefficient: [7.14]
7.2 KNN Classification
Python
from [Link] import load_iris
from [Link] import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from [Link] import accuracy_score,
classification_report
iris = load_iris()
X, y = [Link], [Link]
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=42)
knn = KNeighborsClassifier(n_neighbors=5)
[Link](X_train, y_train)
y_pred = [Link](X_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
print(classification_report(y_test, y_pred,
target_names=iris.target_names))
Output
Accuracy: 0.9778
precision recall f1-score support
setosa 1.00 1.00 1.00 19
versicolor 1.00 0.93 0.96 13
virginica 0.93 1.00 0.96 13
accuracy 0.98 45
17
7.3 Preprocessing — Scaling & Encoding
Python
from [Link] import StandardScaler, LabelEncoder
import pandas as pd
# Feature Scaling — zero mean, unit variance
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X_train)
# Label Encoding — categorical strings to integers
df = [Link]({'Grade': ['A', 'B', 'C', 'A', 'B']})
le = LabelEncoder()
df['Grade_num'] = le.fit_transform(df['Grade'])
print(df)
Output
Grade Grade_num
0 A 0
1 B 1
2 C 2
3 A 0
4 B 1
7.4 Decision Tree & Confusion Matrix
Python
from [Link] import DecisionTreeClassifier
from [Link] import confusion_matrix
dt = DecisionTreeClassifier(max_depth=3, random_state=42)
[Link](X_train, y_train)
y_pred = [Link](X_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred))
Output
Accuracy: 0.9556
Confusion Matrix:
[[19 0 0]
[ 0 12 1]
[ 0 1 12]]
Note: A confusion matrix shows: rows = actual classes, columns = predicted
classes. Diagonal elements are correct predictions; off-diagonal elements are
misclassifications.
18
8. TensorFlow — Deep Learning Basics
TensorFlow (by Google) with its high-level Keras API is the most widely used framework for building
and training neural networks. It supports CPU, GPU, and TPU computation.
8.1 Installation & Tensors
Shell
pip install tensorflow
Python
import tensorflow as tf
import numpy as np
# Tensors — multi-dimensional arrays (like NumPy, but GPU-
enabled)
a = [Link]([1, 2, 3], dtype=tf.float32)
b = [Link]([4, 5, 6], dtype=tf.float32)
print("a :", [Link]())
print("a+b :", [Link](a, b).numpy())
print("a*b :", [Link](a, b).numpy())
print("dot :", tf.reduce_sum(a * b).numpy())
m = [Link]([[1, 2], [3, 4]], dtype=tf.float32)
print("Transpose:\n", [Link](m).numpy())
Output
a : [1. 2. 3.]
a+b : [5. 7. 9.]
a*b : [ 4. 10. 18.]
dot : 32.0
Transpose:
[[1. 3.]
[2. 4.]]
8.2 Building a Neural Network with Keras
Python
from tensorflow import keras
from [Link] import layers
# Sequential model — layers stacked in order
model = [Link]([
[Link](64, activation='relu', input_shape=(8,)), #
hidden layer 1
19
[Link](32, activation='relu'), #
hidden layer 2
[Link](1, activation='sigmoid') #
output (binary)
])
[Link](
optimizer = 'adam',
loss = 'binary_crossentropy',
metrics = ['accuracy']
)
[Link]()
Output
Model: "sequential"
_____________________________________________
Layer (type) Output Shape Param #
=============================================
dense (Dense) (None, 64) 576
dense_1 (Dense) (None, 32) 2080
dense_2 (Dense) (None, 1) 33
=============================================
Total params: 2,689
Trainable params: 2,689
8.3 Training & Evaluation
Python
import numpy as np
# Dummy dataset (replace with real data)
X_train = [Link](500, 8)
y_train = [Link](0, 2, 500)
X_test = [Link](100, 8)
y_test = [Link](0, 2, 100)
# Train the model
history = [Link](
X_train, y_train,
epochs = 20,
batch_size = 32,
validation_split = 0.2,
verbose = 1
)
# Evaluate on test data
20
loss, acc = [Link](X_test, y_test)
print(f"Test Loss : {loss:.4f}")
print(f"Test Accuracy: {acc:.4f}")
Output
Epoch 1/20 - loss: 0.6935 - accuracy: 0.5050
Epoch 5/20 - loss: 0.6820 - accuracy: 0.5350
Epoch 20/20 - loss: 0.6621 - accuracy: 0.5750
Test Loss : 0.6923
Test Accuracy: 0.4900
📌 Note: Accuracy ~50% on random data is expected. On real datasets like MNIST
(handwritten digits), well-tuned models achieve 98%+ accuracy.
8.4 Saving & Loading Models
Python
# Save the trained model
[Link]('my_model.keras')
# Load model and make predictions
loaded = [Link].load_model('my_model.keras')
predictions = [Link](X_test)
print(predictions[:3])
Output
[[0.5124]
[0.4897]
[0.5312]] <- sigmoid outputs (probabilities between 0 and 1)
8.5 Activation Functions — Quick Reference
Function Formula Range Use Case
ReLU max(0, x) 0 to +inf Hidden layers (most common)
Sigmoid 1 / (1 + e^-x) 0 to 1 Binary classification output
Softmax e^x / sum(e^x) 0 to 1 Multi-class output layer
Tanh (e^x - e^-x) / ... -1 to 1 Hidden layers (RNNs)
Linear x -inf to +inf Regression output (no activation)
21
import pandas as pd
# Read existing CSV file
df = pd.read_csv('[Link]')
# New row data
new_row = {
'EmployeeID': 109,
'Name': 'Swetha',
'Department': 'HR',
'Age': 26,
'Salary': 58000
}
# Add new row
[Link][len(df)] = new_row
# Save back to CSV
df.to_csv('[Link]', index=False)
# Display updated dataframe
print(df)
22