17. Automation and Scripting in Python

Automation means using programs to perform repetitive tasks automatically.

Python is widely used for automation because it is:

  • Simple
  • Powerful
  • Cross-platform

Automation can save:

  • Time
  • Effort
  • Manual work

Common Automation Tasks

Python can automate:

  • File handling
  • Web scraping
  • Email sending
  • Data entry
  • System tasks
  • Scheduled jobs

Automating Tasks

Python scripts can perform repetitive operations automatically.


Example: Automatic File Creation

with open("notes.txt", "w") as file:
file.write("Python Automation")

Example: Rename Multiple Files

import os

files = os.listdir()

for index, file in enumerate(files):

if file.endswith(".txt"):

new_name = f"file_{index}.txt"

os.rename(file, new_name)

print("Files renamed")

Example: Folder Creation

import os

for i in range(1, 6):

folder_name = f"Folder_{i}"

os.mkdir(folder_name)

print("Folders created")

Web Scraping

Web scraping extracts data from websites.

Popular libraries:

  • Requests
  • BeautifulSoup

Install Libraries

pip install requests beautifulsoup4

Send HTTP Request

import requests

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

print(response.status_code)

Parse HTML with BeautifulSoup

from bs4 import BeautifulSoup

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

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

print(soup.h1.text)

Output:

Python

Extract All Links

from bs4 import BeautifulSoup

html = """
<a href='https://google.com'>Google</a>
<a href='https://github.com'>GitHub</a>
"""

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

for link in soup.find_all("a"):
print(link.get("href"))

Scrape Website Title

import requests
from bs4 import BeautifulSoup

url = "https://example.com"

response = requests.get(url)

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

print(soup.title.text)

Email Automation

Python can send emails automatically.

Uses:

  • Notifications
  • Reports
  • OTP emails
  • Bulk emails

Using smtplib

import smtplib

Send Simple Email

import smtplib

server = smtplib.SMTP(
"smtp.gmail.com",
587
)

server.starttls()

server.login(
"your_email@gmail.com",
"your_password"
)

message = "Hello from Python"

server.sendmail(
"your_email@gmail.com",
"receiver@gmail.com",
message
)

server.quit()

print("Email sent")

Email with Subject

from email.message import EmailMessage
import smtplib

msg = EmailMessage()

msg["Subject"] = "Python Email"

msg["From"] = "your_email@gmail.com"

msg["To"] = "receiver@gmail.com"

msg.set_content("Hello from Python")

server = smtplib.SMTP(
"smtp.gmail.com",
587
)

server.starttls()

server.login(
"your_email@gmail.com",
"password"
)

server.send_message(msg)

server.quit()

File Automation

Python can automate file operations such as:

  • Copying files
  • Moving files
  • Deleting files
  • Organizing folders

Copy File

import shutil

shutil.copy(
"source.txt",
"destination.txt"
)

Move File

import shutil

shutil.move(
"source.txt",
"new_folder/source.txt"
)

Delete File

import os

os.remove("data.txt")

Organize Files by Extension

import os
import shutil

files = os.listdir()

for file in files:

if file.endswith(".jpg"):

if not os.path.exists("Images"):
os.mkdir("Images")

shutil.move(file, f"Images/{file}")

Scheduling Scripts

Scheduling allows scripts to run automatically at specific times.


Using schedule Library


Install Schedule

pip install schedule

Example Scheduler

import schedule
import time

def task():
print("Task executed")

schedule.every(5).seconds.do(task)

while True:

schedule.run_pending()

time.sleep(1)

Run Daily Task

schedule.every().day.at("10:00").do(task)

System Scheduler

You can also use:

  • Windows Task Scheduler
  • Linux Cron Jobs

Cron Job Example (Linux)

0 10 * * * python3 script.py

Runs script daily at 10:00 AM.


Windows Task Scheduler

Steps:

  1. Open Task Scheduler
  2. Create Basic Task
  3. Select Python script
  4. Set schedule time

Practical Example

Automatic Backup Script

import shutil
import datetime

source = "data.txt"

date = datetime.datetime.now().strftime(
"%Y%m%d_%H%M%S"
)

backup = f"backup_{date}.txt"

shutil.copy(source, backup)

print("Backup created:", backup)

Advantages of Automation

✅ Saves time
✅ Reduces manual work
✅ Improves productivity
✅ Reduces errors
✅ Handles repetitive tasks efficiently


Best Practices

✅ Handle exceptions properly
✅ Avoid hardcoded passwords
✅ Use logging for scripts
✅ Schedule scripts carefully
✅ Respect website scraping policies


Summary

In this chapter you learned:

✅ Automating tasks
✅ Web scraping
✅ Email automation
✅ File automation
✅ Scheduling scripts

16. Machine Learning with Python

Machine Learning (ML) enables computers to learn from data and make predictions without being explicitly programmed.

Python is widely used in ML because of libraries like:

  • Scikit-learn
  • NumPy
  • Pandas
  • Matplotlib

What is Scikit-learn?

Scikit-learn is a popular Python library for machine learning.

Used for:

  • Regression
  • Classification
  • Clustering
  • Model evaluation

Installing Scikit-learn

pip install scikit-learn

Importing Scikit-learn

from sklearn import datasets

Machine Learning Workflow

Typical ML process:

1. Collect Data
2. Prepare Data
3. Train Model
4. Test Model
5. Evaluate Results

Types of Machine Learning

TypeDescription
Supervised LearningUses labeled data
Unsupervised LearningUses unlabeled data
Reinforcement LearningLearns using rewards

Regression

Regression predicts continuous numerical values.

Examples:

  • House prices
  • Temperature
  • Sales prediction

Linear Regression

Linear regression finds relationship between variables.


Example: Linear Regression

from sklearn.linear_model import LinearRegression
import numpy as np

# Input data
X = np.array([[1], [2], [3], [4], [5]])

# Output data
y = np.array([2, 4, 6, 8, 10])

# Create model
model = LinearRegression()

# Train model
model.fit(X, y)

# Predict
prediction = model.predict([[6]])

print(prediction)

Output Example:

[12.]

Regression Terms

TermMeaning
FeaturesInput variables
TargetOutput variable
Training DataData used for learning
PredictionModel output

Train-Test Split

Used to separate training and testing data.

from sklearn.model_selection import train_test_split
import numpy as np

X = np.array([[1], [2], [3], [4], [5]])
y = np.array([2, 4, 6, 8, 10])

X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2
)

print(X_train)
print(X_test)

Classification

Classification predicts categories or labels.

Examples:

  • Spam detection
  • Disease prediction
  • Image recognition

Logistic Regression Example

from sklearn.linear_model import LogisticRegression
from sklearn.datasets import load_iris

# Load dataset
iris = load_iris()

X = iris.data
y = iris.target

# Create model
model = LogisticRegression(max_iter=200)

# Train model
model.fit(X, y)

# Predict
prediction = model.predict([X[0]])

print(prediction)

Decision Tree Classifier

from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import load_iris

iris = load_iris()

X = iris.data
y = iris.target

model = DecisionTreeClassifier()

model.fit(X, y)

print(model.predict([X[1]]))

Clustering

Clustering groups similar data points.

It is an:

Unsupervised Learning

technique.

Examples:

  • Customer segmentation
  • Product grouping

K-Means Clustering

from sklearn.cluster import KMeans
import numpy as np

X = np.array([
[1, 2],
[1, 4],
[5, 8],
[8, 8]
])

model = KMeans(
n_clusters=2,
random_state=0
)

model.fit(X)

print(model.labels_)

Output Example:

[1 1 0 0]

Model Evaluation

Model evaluation checks model performance.


Regression Evaluation


Mean Absolute Error (MAE)

from sklearn.metrics import mean_absolute_error

actual = [10, 20, 30]
predicted = [12, 18, 29]

mae = mean_absolute_error(
actual,
predicted
)

print(mae)

Mean Squared Error (MSE)

from sklearn.metrics import mean_squared_error

mse = mean_squared_error(
actual,
predicted
)

print(mse)

R² Score

from sklearn.metrics import r2_score

score = r2_score(
actual,
predicted
)

print(score)

Classification Evaluation


Accuracy Score

from sklearn.metrics import accuracy_score

actual = [1, 0, 1, 1]
predicted = [1, 0, 1, 0]

accuracy = accuracy_score(
actual,
predicted
)

print(accuracy)

Confusion Matrix

from sklearn.metrics import confusion_matrix

cm = confusion_matrix(
actual,
predicted
)

print(cm)

Classification Report

from sklearn.metrics import classification_report

print(classification_report(
actual,
predicted
))

Cross Validation

Improves model reliability.

from sklearn.model_selection import cross_val_score
from sklearn.linear_model import LinearRegression
import numpy as np

X = np.array([[1], [2], [3], [4]])
y = np.array([2, 4, 6, 8])

model = LinearRegression()

scores = cross_val_score(
model,
X,
y,
cv=2
)

print(scores)

Saving and Loading Models

Use:

joblib

Save Model

import joblib

joblib.dump(model, "model.pkl")

Load Model

model = joblib.load("model.pkl")

Practical Example

House Price Prediction

from sklearn.linear_model import LinearRegression
import numpy as np

# Area of houses
X = np.array([
[500],
[1000],
[1500],
[2000]
])

# Prices
y = np.array([
100000,
200000,
300000,
400000
])

model = LinearRegression()

model.fit(X, y)

price = model.predict([[2500]])

print("Predicted Price:", price[0])

Advantages of Scikit-learn

✅ Simple and easy API
✅ Fast machine learning models
✅ Good documentation
✅ Supports many algorithms
✅ Works well with NumPy and Pandas


Summary

In this chapter you learned:

✅ Scikit-learn basics
✅ Regression
✅ Classification
✅ Clustering
✅ Model evaluation

15. Data Science with Python

Data Science involves:

  • Collecting data
  • Cleaning data
  • Analyzing data
  • Visualizing insights

Python is widely used in Data Science because of its powerful libraries.

Popular libraries:

  • NumPy
  • Pandas
  • Matplotlib
  • Seaborn
  • Scikit-learn

NumPy Basics

NumPy stands for:

Numerical Python

Used for:

  • Arrays
  • Mathematical operations
  • Scientific computing

Installing 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]

Multi-Dimensional Arrays

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

print(matrix)

Useful NumPy Functions

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

Pandas DataFrames

Pandas is used for:

  • Data analysis
  • Data manipulation
  • Working with tables

Installing Pandas

pip install pandas

Import Pandas

import pandas as pd

Creating DataFrame

import pandas as pd

data = {
"Name": ["Aditya", "Rahul"],
"Marks": [90, 85]
}

df = pd.DataFrame(data)

print(df)

Output:

      Name  Marks
0 Aditya 90
1 Rahul 85

Reading CSV File

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

print(df.head())

Viewing Data

print(df.head())
print(df.tail())
print(df.info())

Selecting Columns

print(df["Name"])

Filtering Data

high_marks = df[df["Marks"] > 80]

print(high_marks)

Adding New Column

df["Result"] = "Pass"

print(df)

Data Visualization

Data visualization helps understand patterns and trends.

Popular library:

Matplotlib

Installing Matplotlib

pip install matplotlib

Import Matplotlib

import matplotlib.pyplot as plt

Line Chart

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()

Histogram

data = [10, 20, 20, 30, 40, 40, 40]

plt.hist(data)

plt.show()

Data Cleaning

Data cleaning means fixing:

  • Missing values
  • Duplicate data
  • Incorrect formats

Detect Missing Values

print(df.isnull())
print(df.isnull().sum())

Remove Missing Values

df = df.dropna()

Fill Missing Values

df["Marks"] = df["Marks"].fillna(0)

Remove Duplicate Rows

df = df.drop_duplicates()

Rename Columns

df.rename(columns={
"Marks": "Score"
}, inplace=True)

Change Data Types

df["Marks"] = df["Marks"].astype(int)

Exploratory Data Analysis (EDA)

EDA is the process of analyzing datasets to discover:

  • Patterns
  • Trends
  • Relationships

Basic Statistics

print(df.describe())

Value Counts

print(df["Result"].value_counts())

Correlation

print(df.corr(numeric_only=True))

Grouping Data

grouped = df.groupby("Result")["Marks"].mean()

print(grouped)

Sorting Data

sorted_df = df.sort_values(
by="Marks",
ascending=False
)

print(sorted_df)

Export Data

Save CSV

df.to_csv("output.csv", index=False)

Save Excel File

df.to_excel("output.xlsx", index=False)

Practical Example

Student Data Analysis

import pandas as pd

data = {
"Name": ["Aditya", "Rahul", "Aman"],
"Marks": [90, 75, 85]
}

df = pd.DataFrame(data)

print("Average Marks:")
print(df["Marks"].mean())

print("Highest Marks:")
print(df["Marks"].max())

print("Students with Marks > 80:")
print(df[df["Marks"] > 80])

Advantages of Data Science Libraries

✅ Fast data processing
✅ Powerful analysis tools
✅ Easy visualization
✅ Handles large datasets
✅ Supports machine learning


Summary

In this chapter you learned:

✅ NumPy basics
✅ Pandas DataFrames
✅ Data visualization
✅ Data cleaning
✅ Exploratory Data Analysis (EDA)

14. Web Development with Python

Python is widely used for web development.

Popular Python web frameworks:

  • Flask
  • Django

These frameworks help create:

  • Websites
  • Web applications
  • APIs
  • Admin panels

Flask Framework

Flask is a lightweight Python web framework.

Features:

  • Simple and flexible
  • Easy to learn
  • Good for APIs and small applications

Installing Flask

pip install flask

Creating First Flask App

from flask import Flask

app = Flask(__name__)

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

app.run(debug=True)

Running Flask App

Save file as:

app.py

Run command:

python app.py

Open browser:

http://127.0.0.1:5000

Flask Routing

Routing connects URLs to functions.


Example Routes

from flask import Flask

app = Flask(__name__)

@app.route("/")
def home():
return "Home Page"

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

@app.route("/contact")
def contact():
return "Contact Page"

app.run(debug=True)

Dynamic Routing

@app.route("/user/<name>")
def user(name):
return f"Hello {name}"

URL example:

/user/Aditya

Flask Templates

Templates help separate HTML from Python code.

Flask uses:

Jinja2

template engine.


Project Structure

project/

├── app.py
└── templates/
└── index.html

HTML Template

templates/index.html

<!DOCTYPE html>
<html>
<head>
<title>Home</title>
</head>
<body>

<h1>Welcome {{ name }}</h1>

</body>
</html>

Render Template

from flask import Flask, render_template

app = Flask(__name__)

@app.route("/")
def home():
return render_template(
"index.html",
name="Aditya"
)

app.run(debug=True)

Flask Forms

Used to collect user input.


HTML Form

<form method="POST">

<input type="text" name="username">

<button type="submit">
Submit
</button>

</form>

Handle Form Data

from flask import Flask, request

app = Flask(__name__)

@app.route("/", methods=["GET", "POST"])
def home():

if request.method == "POST":

username = request.form["username"]

return f"Hello {username}"

return """
<form method='POST'>
<input type='text' name='username'>
<button type='submit'>Submit</button>
</form>
"""

app.run(debug=True)

APIs with Flask

API = Application Programming Interface

Flask can return JSON data.


JSON API Example

from flask import Flask, jsonify

app = Flask(__name__)

@app.route("/api")
def api():

data = {
"name": "Aditya",
"course": "Python"
}

return jsonify(data)

app.run(debug=True)

REST API Example

from flask import Flask, request, jsonify

app = Flask(__name__)

students = []

@app.route("/students", methods=["POST"])
def add_student():

data = request.json

students.append(data)

return jsonify({
"message": "Student added"
})

@app.route("/students", methods=["GET"])
def get_students():
return jsonify(students)

app.run(debug=True)

Django Framework

Django is a powerful full-stack web framework.

Features:

  • Built-in admin panel
  • Authentication system
  • ORM support
  • Security features

Installing Django

pip install django

Create Django Project

django-admin startproject myproject

Run Django Server

python manage.py runserver

Open browser:

http://127.0.0.1:8000

Django Project Structure

myproject/

├── manage.py
├── myproject/
└── myapp/

Create Django App

python manage.py startapp myapp

Django Models

Models define database structure.


Example Model

from django.db import models

class Student(models.Model):

name = models.CharField(max_length=100)

age = models.IntegerField()

def __str__(self):
return self.name

Run Migrations

python manage.py makemigrations
python manage.py migrate

Django Views

Views handle application logic.


Example View

from django.http import HttpResponse

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

Django URLs

urls.py

from django.urls import path
from . import views

urlpatterns = [
path("", views.home)
]

Django Templates


Template File

templates/index.html

<h1>Welcome {{ name }}</h1>

Render Template

from django.shortcuts import render

def home(request):

return render(
request,
"index.html",
{"name": "Aditya"}
)

Django Admin Panel

Django provides built-in admin dashboard.


Create Superuser

python manage.py createsuperuser

Access Admin Panel

http://127.0.0.1:8000/admin

Register Model in Admin

admin.py

from django.contrib import admin
from .models import Student

admin.site.register(Student)

Django Authentication

Django provides built-in authentication system.

Supports:

  • Login
  • Logout
  • Registration
  • Password management

Create User

from django.contrib.auth.models import User

user = User.objects.create_user(
username="aditya",
password="12345"
)

user.save()

Login User

from django.contrib.auth import authenticate

user = authenticate(
username="aditya",
password="12345"
)

if user:
print("Login successful")
else:
print("Invalid credentials")

Logout User

from django.contrib.auth import logout

def user_logout(request):
logout(request)

Flask vs Django

FeatureFlaskDjango
TypeLightweightFull-stack
Learning CurveEasyModerate
FlexibilityHighMedium
Built-in FeaturesFewerMany
Best ForAPIs, small appsLarge projects

Practical Example

Simple Flask API

from flask import Flask, jsonify

app = Flask(__name__)

@app.route("/")

def home():
return "API Running"

@app.route("/users")

def users():

data = [
{"name": "Aditya"},
{"name": "Rahul"}
]

return jsonify(data)

app.run(debug=True)

Advantages of Python Web Frameworks

✅ Fast development
✅ Secure applications
✅ Easy database integration
✅ Large community support
✅ Scalable applications


Summary

In this chapter you learned:

✅ Flask basics
✅ Routing in Flask
✅ Templates in Flask
✅ Forms in Flask
✅ APIs with Flask
✅ Django basics
✅ Models in Django
✅ Views in Django
✅ Templates in Django
✅ Django admin panel
✅ Authentication in Django

13. Database Programming in Python

Database programming allows Python applications to:

  • Store data permanently
  • Retrieve records
  • Update information
  • Delete records

Python supports many databases such as:

  • SQLite
  • MySQL
  • PostgreSQL
  • MongoDB

SQLite with Python

SQLite is a lightweight built-in database.

Features:

  • No separate server required
  • Easy to use
  • Stored in a single file

Python provides:

sqlite3

module.


Import SQLite

import sqlite3

Create Database Connection

import sqlite3

conn = sqlite3.connect("students.db")

print("Database connected")

This creates:

students.db

file.


Create Cursor Object

Cursor executes SQL queries.

cursor = conn.cursor()

Create Table

cursor.execute("""
CREATE TABLE IF NOT EXISTS students(
id INTEGER PRIMARY KEY,
name TEXT,
age INTEGER
)
""")

conn.commit()

Insert Data

cursor.execute("""
INSERT INTO students(name, age)
VALUES (?, ?)
""", ("Aditya", 25))

conn.commit()

Read Data

cursor.execute("SELECT * FROM students")

rows = cursor.fetchall()

for row in rows:
print(row)

Output Example:

(1, 'Aditya', 25)

Update Data

cursor.execute("""
UPDATE students
SET age = 26
WHERE name = 'Aditya'
""")

conn.commit()

Delete Data

cursor.execute("""
DELETE FROM students
WHERE name = 'Aditya'
""")

conn.commit()

Close Connection

conn.close()

Complete SQLite Example

import sqlite3

conn = sqlite3.connect("school.db")

cursor = conn.cursor()

cursor.execute("""
CREATE TABLE IF NOT EXISTS students(
id INTEGER PRIMARY KEY,
name TEXT,
marks INTEGER
)
""")

cursor.execute("""
INSERT INTO students(name, marks)
VALUES (?, ?)
""", ("Rahul", 90))

conn.commit()

cursor.execute("SELECT * FROM students")

for row in cursor.fetchall():
print(row)

conn.close()

MySQL Connectivity

MySQL is a popular relational database management system.

Python connects to MySQL using:

mysql-connector-python

Install MySQL Connector

pip install mysql-connector-python

Import MySQL Connector

import mysql.connector

Connect to MySQL

import mysql.connector

conn = mysql.connector.connect(
host="localhost",
user="root",
password="password",
database="school"
)

print("Connected")

Create Cursor

cursor = conn.cursor()

Create Table in MySQL

cursor.execute("""
CREATE TABLE IF NOT EXISTS students(
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
age INT
)
""")

Insert Data in MySQL

sql = """
INSERT INTO students(name, age)
VALUES (%s, %s)
"""

values = ("Aditya", 25)

cursor.execute(sql, values)

conn.commit()

Read Data

cursor.execute("SELECT * FROM students")

for row in cursor.fetchall():
print(row)

Update Data

sql = """
UPDATE students
SET age = %s
WHERE name = %s
"""

values = (26, "Aditya")

cursor.execute(sql, values)

conn.commit()

Delete Data

sql = """
DELETE FROM students
WHERE name = %s
"""

cursor.execute(sql, ("Aditya",))

conn.commit()

Close MySQL Connection

conn.close()

CRUD Operations

CRUD stands for:

OperationMeaning
CreateInsert data
ReadRetrieve data
UpdateModify data
DeleteRemove data

CRUD Example with SQLite

import sqlite3

conn = sqlite3.connect("employee.db")

cursor = conn.cursor()

# Create
cursor.execute("""
CREATE TABLE IF NOT EXISTS employees(
id INTEGER PRIMARY KEY,
name TEXT,
salary INTEGER
)
""")

# Insert
cursor.execute("""
INSERT INTO employees(name, salary)
VALUES (?, ?)
""", ("Rahul", 50000))

# Read
cursor.execute("SELECT * FROM employees")

print(cursor.fetchall())

# Update
cursor.execute("""
UPDATE employees
SET salary = 60000
WHERE name = 'Rahul'
""")

# Delete
cursor.execute("""
DELETE FROM employees
WHERE name = 'Rahul'
""")

conn.commit()

conn.close()

ORM Basics

ORM stands for:

Object Relational Mapping

ORM allows working with databases using Python objects instead of SQL queries.

Popular Python ORM:

  • SQLAlchemy
  • Django ORM

Advantages of ORM

✅ Less SQL code
✅ Easier database handling
✅ Better readability
✅ Database portability


SQLAlchemy Installation

pip install sqlalchemy

SQLAlchemy Example

from sqlalchemy import create_engine

engine = create_engine("sqlite:///students.db")

print(engine)

Define ORM Model

from sqlalchemy.orm import declarative_base
from sqlalchemy import Column, Integer, String

Base = declarative_base()

class Student(Base):

__tablename__ = "students"

id = Column(Integer, primary_key=True)
name = Column(String)
age = Column(Integer)

Create Table

Base.metadata.create_all(engine)

Insert Data Using ORM

from sqlalchemy.orm import sessionmaker

Session = sessionmaker(bind=engine)

session = Session()

student = Student(
name="Aditya",
age=25
)

session.add(student)

session.commit()

Fetch Data Using ORM

students = session.query(Student).all()

for student in students:
print(student.name, student.age)

Django ORM Example

from django.db import models

class Student(models.Model):

name = models.CharField(max_length=100)
age = models.IntegerField()

Run Migrations

python manage.py makemigrations
python manage.py migrate

Practical Example

Student Record System

import sqlite3

conn = sqlite3.connect("students.db")

cursor = conn.cursor()

cursor.execute("""
CREATE TABLE IF NOT EXISTS students(
id INTEGER PRIMARY KEY,
name TEXT,
marks INTEGER
)
""")

name = input("Enter name: ")
marks = int(input("Enter marks: "))

cursor.execute("""
INSERT INTO students(name, marks)
VALUES (?, ?)
""", (name, marks))

conn.commit()

cursor.execute("SELECT * FROM students")

for row in cursor.fetchall():
print(row)

conn.close()

Best Practices

✅ Use parameterized queries
✅ Close database connections
✅ Use ORM for large projects
✅ Handle exceptions properly
✅ Avoid hardcoded credentials


Summary

In this chapter you learned:

✅ SQLite with Python
✅ MySQL connectivity
✅ CRUD operations
✅ ORM basics
✅ SQLAlchemy basics
✅ Django ORM basics

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

12. Python Libraries

Python libraries provide ready-made functions and modules that help perform tasks easily.

Python includes many built-in libraries called:

Standard Libraries

These libraries come pre-installed with Python.


Standard Libraries in Python

Some commonly used standard libraries are:

  • math
  • random
  • datetime
  • os
  • sys
  • collections

1. math Module

The math module provides mathematical functions.


Importing math

import math

Common Math Functions

FunctionDescription
sqrt()Square root
pow()Power
ceil()Round up
floor()Round down
factorial()Factorial
piValue of π

Example: Square Root

import math

print(math.sqrt(25))

Output:

5.0

Example: Power

print(math.pow(2, 3))

Output:

8.0

Example: Ceiling and Floor

print(math.ceil(4.2))
print(math.floor(4.8))

Output:

5
4

Example: Factorial

print(math.factorial(5))

Output:

120

Example: Value of Pi

print(math.pi)

2. random Module

The random module generates random values.


Importing random

import random

Random Integer

print(random.randint(1, 10))

Returns random number between 1 and 10.


Random Float

print(random.random())

Returns float between 0 and 1.


Random Choice

fruits = ["Apple", "Banana", "Mango"]

print(random.choice(fruits))

Shuffle List

numbers = [1, 2, 3, 4, 5]

random.shuffle(numbers)

print(numbers)

Generate OTP

otp = random.randint(1000, 9999)

print("OTP:", otp)

3. datetime Module

Used for date and time operations.


Importing datetime

from datetime import datetime

Current Date and Time

now = datetime.now()

print(now)

Current Date Only

print(now.date())

Current Time Only

print(now.time())

Formatting Date

print(now.strftime("%d-%m-%Y"))

Output Example:

25-05-2026

Common Date Formats

FormatMeaning
%dDay
%mMonth
%YYear
%HHour
%MMinute
%SSecond

Create Custom Date

from datetime import datetime

date = datetime(2026, 5, 25)

print(date)

Date Difference

from datetime import datetime

d1 = datetime(2026, 1, 1)
d2 = datetime(2026, 5, 25)

difference = d2 - d1

print(difference.days)

4. os Module

Used for interacting with operating system.


Importing os

import os

Current Working Directory

print(os.getcwd())

List Files and Folders

print(os.listdir())

Create Directory

os.mkdir("new_folder")

Remove Directory

os.rmdir("new_folder")

Rename File

os.rename("old.txt", "new.txt")

Check File Exists

print(os.path.exists("data.txt"))

Join File Paths

path = os.path.join("folder", "file.txt")

print(path)

Environment Variables

print(os.environ)

5. sys Module

Provides system-specific functions.


Importing sys

import sys

Python Version

print(sys.version)

Command Line Arguments

print(sys.argv)

Exit Program

sys.exit()

System Path

print(sys.path)

Memory Size

x = [1, 2, 3]

print(sys.getsizeof(x))

6. collections Module

Provides advanced container datatypes.


Importing collections

from collections import Counter

Counter

Counts occurrences.

from collections import Counter

data = ["apple", "banana", "apple"]

count = Counter(data)

print(count)

Output:

Counter({'apple': 2, 'banana': 1})

defaultdict

Provides default values.

from collections import defaultdict

data = defaultdict(int)

data["a"] += 1

print(data["a"])

deque

Fast insertion/removal from both ends.

from collections import deque

dq = deque([1, 2, 3])

dq.appendleft(0)

print(dq)

namedtuple

Tuple with named fields.

from collections import namedtuple

Student = namedtuple("Student", ["name", "age"])

s = Student("Aditya", 25)

print(s.name)
print(s.age)

Practical Example

Dice Simulator

import random

while True:

input("Press Enter to roll dice")

print("Dice:", random.randint(1, 6))

choice = input("Roll again? (y/n): ")

if choice.lower() != "y":
break

Advantages of Standard Libraries

✅ Save development time
✅ Ready-made functions
✅ Reliable and optimized
✅ Cross-platform support
✅ Easy to use

Summary

In this chapter you learned:

✅ Standard Libraries
math module
random module
datetime module
os module
sys module
collections module

11. Advanced Python Concepts

Advanced Python concepts help write:

  • Efficient code
  • Cleaner programs
  • Reusable and optimized solutions

Iterators in Python

An iterator is an object used to iterate through data one element at a time.

Examples of iterable objects:

  • Lists
  • Tuples
  • Strings
  • Dictionaries

Using Iterator

numbers = [10, 20, 30]

iterator = iter(numbers)

print(next(iterator))
print(next(iterator))
print(next(iterator))

Output:

10
20
30

StopIteration Exception

When elements finish:

print(next(iterator))

Output:

StopIteration

Custom Iterator

class Counter:

def __init__(self, max_value):
self.max_value = max_value
self.current = 1

def __iter__(self):
return self

def __next__(self):

if self.current <= self.max_value:
value = self.current
self.current += 1
return value

raise StopIteration

counter = Counter(5)

for num in counter:
print(num)

Generators in Python

Generators create iterators using yield.

Advantages:

  • Memory efficient
  • Faster for large data

Generator Function

def numbers():

yield 1
yield 2
yield 3

gen = numbers()

print(next(gen))
print(next(gen))

Generator with Loop

def countdown(n):

while n > 0:
yield n
n -= 1

for value in countdown(5):
print(value)

Generator Expression

squares = (x * x for x in range(5))

for num in squares:
print(num)

Decorators in Python

Decorators modify function behavior without changing original code.


Basic Decorator

def decorator_function(func):

def wrapper():
print("Before function call")

func()

print("After function call")

return wrapper

Applying Decorator

@decorator_function
def say_hello():
print("Hello")

say_hello()

Output:

Before function call
Hello
After function call

Decorator with Arguments

def smart_divide(func):

def wrapper(a, b):

if b == 0:
print("Cannot divide by zero")
return

return func(a, b)

return wrapper

@smart_divide
def divide(a, b):
print(a / b)

divide(10, 2)
divide(10, 0)

Context Managers

Context managers manage resources automatically.

Usually used with:

with

statement.


File Context Manager

with open("data.txt", "r") as file:
content = file.read()

print(content)

File closes automatically.


Custom Context Manager

class FileManager:

def __enter__(self):
print("File opened")
return self

def __exit__(self, exc_type, exc_value, traceback):
print("File closed")

with FileManager():
print("Working with file")

Closures in Python

A closure remembers variables from outer function even after outer function finishes.


Example

def outer(message):

def inner():
print(message)

return inner

greet = outer("Hello Python")

greet()

Output:

Hello Python

Comprehensions in Python

Comprehensions provide shorter syntax for creating collections.


List Comprehension

Syntax

[expression for item in iterable]

Example

squares = [x * x for x in range(5)]

print(squares)

Output:

[0, 1, 4, 9, 16]

List Comprehension with Condition

even_numbers = [x for x in range(10) if x % 2 == 0]

print(even_numbers)

Dictionary Comprehension

Syntax

{key:value for item in iterable}

Example

squares = {x: x * x for x in range(5)}

print(squares)

Output:

{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}

Set Comprehension

numbers = {x for x in range(5)}

print(numbers)

Functional Programming Tools

Python provides built-in functional programming functions.


1. map()

Applies function to every item.


Syntax

map(function, iterable)

Example

numbers = [1, 2, 3, 4]

result = list(map(lambda x: x * 2, numbers))

print(result)

Output:

[2, 4, 6, 8]

2. filter()

Filters elements based on condition.


Syntax

filter(function, iterable)

Example

numbers = [1, 2, 3, 4, 5, 6]

even = list(filter(lambda x: x % 2 == 0, numbers))

print(even)

Output:

[2, 4, 6]

3. reduce()

Reduces iterable to single value.

Available in:

functools

module.


Example

from functools import reduce

numbers = [1, 2, 3, 4]

result = reduce(lambda a, b: a + b, numbers)

print(result)

Output:

10

Comparison of map, filter, and reduce

FunctionPurpose
map()Transform data
filter()Select data
reduce()Combine data

Practical Example

Student Marks Processing

marks = [45, 78, 90, 32, 67]

# Passed students
passed = list(filter(lambda x: x >= 40, marks))

# Add grace marks
updated = list(map(lambda x: x + 5, passed))

print(updated)

Advantages of Advanced Concepts

✅ Cleaner code
✅ Better performance
✅ Reusable components
✅ Memory optimization
✅ Functional programming support

Summary

In this chapter you learned:

✅ Iterators
✅ Generators
✅ Decorators
✅ Context managers
✅ Closures
✅ List comprehensions
✅ Dictionary comprehensions
map()
filter()
reduce()

10. Object-Oriented Programming (OOP)

Object-Oriented Programming (OOP) is a programming paradigm based on objects and classes.

OOP helps:

  • Organize code better
  • Improve reusability
  • Make large applications easier to manage

Classes and Objects


What is a Class?

A class is a blueprint for creating objects.

It defines:

  • Variables (attributes)
  • Functions (methods)

What is an Object?

An object is an instance of a class.


Creating a Class

Syntax

class ClassName:
# attributes and methods

Example

class Student:
pass

Creating Objects

class Student:
pass

s1 = Student()
s2 = Student()

print(s1)
print(s2)

Constructors

A constructor is a special method automatically called when object is created.

Python uses:

__init__()

Example

class Student:

def __init__(self, name, age):
self.name = name
self.age = age

student1 = Student("Aditya", 25)

print(student1.name)
print(student1.age)

Output:

Aditya
25

The self Keyword

self refers to the current object.

Used to access instance variables and methods.


Instance Variables

Variables unique to each object.

class Car:

def __init__(self, brand):
self.brand = brand

c1 = Car("BMW")
c2 = Car("Audi")

print(c1.brand)
print(c2.brand)

Class Variables

Shared by all objects.

class Employee:

company = "SOA Technology"

def __init__(self, name):
self.name = name

e1 = Employee("Aditya")
e2 = Employee("Rahul")

print(e1.company)
print(e2.company)

Methods in Python

Methods are functions inside classes.


Instance Methods

class Person:

def __init__(self, name):
self.name = name

def greet(self):
print("Hello", self.name)

p1 = Person("Aditya")
p1.greet()

Class Methods

Use @classmethod.

class Demo:

count = 0

@classmethod
def show_count(cls):
print(cls.count)

Demo.show_count()

Static Methods

Use @staticmethod.

class Math:

@staticmethod
def add(a, b):
return a + b

print(Math.add(5, 3))

Inheritance

Inheritance allows one class to acquire properties of another class.


Parent and Child Class

class Animal:

def sound(self):
print("Animal makes sound")

class Dog(Animal):
pass

d = Dog()
d.sound()

Method Overriding

Child class changes parent method behavior.

class Animal:

def sound(self):
print("Animal sound")

class Dog(Animal):

def sound(self):
print("Dog barks")

d = Dog()
d.sound()

Output:

Dog barks

super() Function

Used to call parent class methods.

class Person:

def __init__(self, name):
self.name = name

class Student(Person):

def __init__(self, name, course):
super().__init__(name)
self.course = course

s = Student("Aditya", "Python")

print(s.name)
print(s.course)

Polymorphism

Polymorphism means:

One interface, many forms

Example

class Bird:

def sound(self):
print("Bird sound")

class Sparrow(Bird):

def sound(self):
print("Sparrow chirps")

class Crow(Bird):

def sound(self):
print("Crow caws")

for bird in [Sparrow(), Crow()]:
bird.sound()

Encapsulation

Encapsulation means restricting direct access to data.

Use:

  • Private variables
  • Getter and setter methods

Private Variable

Use double underscore __.

class Bank:

def __init__(self):
self.__balance = 1000

b = Bank()

# print(b.__balance) # Error

Getter and Setter

class Bank:

def __init__(self):
self.__balance = 0

def deposit(self, amount):
self.__balance += amount

def get_balance(self):
return self.__balance

b = Bank()

b.deposit(500)

print(b.get_balance())

Abstraction

Abstraction hides implementation details and shows only essential features.

Python uses abstract classes from abc module.


Example

from abc import ABC, abstractmethod

class Vehicle(ABC):

@abstractmethod
def start(self):
pass

class Car(Vehicle):

def start(self):
print("Car started")

c = Car()
c.start()

Magic Methods

Magic methods are special methods with double underscores.

Also called:

Dunder methods

Common Magic Methods

MethodPurpose
__init__Constructor
__str__String representation
__len__Length
__add__Addition
__del__Destructor

__str__() Method

Defines object string representation.

class Student:

def __init__(self, name):
self.name = name

def __str__(self):
return f"Student name is {self.name}"

s = Student("Aditya")

print(s)

Output:

Student name is Aditya

__len__() Method

class Team:

def __len__(self):
return 5

t = Team()

print(len(t))

Operator Overloading

Using magic methods to customize operators.

class Number:

def __init__(self, value):
self.value = value

def __add__(self, other):
return self.value + other.value

n1 = Number(10)
n2 = Number(20)

print(n1 + n2)

Output:

30

Destructor __del__()

Called when object is destroyed.

class Demo:

def __del__(self):
print("Object destroyed")

d = Demo()

Real-Life Example

Student Management System

class Student:

school = "SOA School"

def __init__(self, name, marks):
self.name = name
self.marks = marks

def display(self):
print("Name:", self.name)
print("Marks:", self.marks)

s1 = Student("Aditya", 90)

s1.display()

Advantages of OOP

✅ Code reusability
✅ Better organization
✅ Easier maintenance
✅ Data security
✅ Real-world modeling

Summary

In this chapter you learned:

✅ Classes and objects
✅ Constructors
✅ Instance and class variables
✅ Methods
✅ Inheritance
✅ Polymorphism
✅ Encapsulation
✅ Abstraction
✅ Magic methods (__init__, __str__, etc.)

9. Exception Handling in Python

Exception handling is used to handle runtime errors gracefully without stopping the program.

It helps:

  • Prevent program crashes
  • Handle unexpected situations
  • Improve program reliability

Types of Errors in Python

Python errors are mainly of two types:

  1. Syntax Errors
  2. Exceptions (Runtime Errors)

1. Syntax Errors

Occurs when Python rules are violated.

Example

if 5 > 2
print("Hello")

Output:

SyntaxError

Missing colon (:).


2. Exceptions (Runtime Errors)

Occurs during program execution.


Common Exception Types

ExceptionDescription
ZeroDivisionErrorDivision by zero
NameErrorVariable not defined
TypeErrorWrong data type
ValueErrorInvalid value
IndexErrorInvalid list index
KeyErrorInvalid dictionary key
FileNotFoundErrorFile does not exist

Example: ZeroDivisionError

x = 10 / 0

try and except

Used to handle exceptions.


Syntax

try:
# risky code
except:
# handling code

Example

try:
num = 10 / 0
except:
print("Error occurred")

Output:

Error occurred

Handling Specific Exceptions

try:
number = int(input("Enter number: "))
result = 10 / number

except ZeroDivisionError:
print("Cannot divide by zero")

except ValueError:
print("Invalid input")

Multiple Exceptions Together

try:
x = int("abc")

except (ValueError, TypeError):
print("Error handled")

else Block

The else block executes if no exception occurs.


Syntax

try:
# code
except:
# error handling
else:
# runs if no error

Example

try:
num = 10 / 2

except ZeroDivisionError:
print("Division error")

else:
print("Result:", num)

Output:

Result: 5.0

finally Block

The finally block always executes whether exception occurs or not.

Used for:

  • Closing files
  • Releasing resources
  • Cleanup operations

Syntax

try:
# code
except:
# handling
finally:
# always executes

Example

try:
file = open("data.txt", "r")

except FileNotFoundError:
print("File not found")

finally:
print("Execution completed")

Complete Example

try:
num = int(input("Enter number: "))
result = 100 / num

except ZeroDivisionError:
print("Cannot divide by zero")

except ValueError:
print("Invalid input")

else:
print("Result:", result)

finally:
print("Program finished")

Raising Exceptions

Use raise keyword to create exceptions manually.


Syntax

raise ExceptionType("message")

Example

age = -5

if age < 0:
raise ValueError("Age cannot be negative")

Example: Custom Validation

password = "123"

if len(password) < 6:
raise Exception("Password too short")

Custom Exceptions

You can create your own exception classes.


Creating Custom Exception

class InvalidAgeError(Exception):
pass

Using Custom Exception

class InvalidAgeError(Exception):
pass

age = -1

try:
if age < 0:
raise InvalidAgeError("Invalid age entered")

except InvalidAgeError as e:
print(e)

Output:

Invalid age entered

Exception Object

Store exception in variable using as.

try:
x = 10 / 0

except ZeroDivisionError as e:
print("Error:", e)

Nested try Blocks

try:
try:
x = 10 / 0
except ZeroDivisionError:
print("Inner exception handled")

except:
print("Outer exception")

User-Defined Function with Exception Handling

def divide(a, b):

try:
return a / b

except ZeroDivisionError:
return "Cannot divide by zero"

print(divide(10, 0))

Best Practices

✅ Handle specific exceptions
✅ Use finally for cleanup
✅ Avoid empty except blocks
✅ Use meaningful error messages
✅ Use custom exceptions for large projects


Practical Example

ATM Withdrawal System

balance = 5000

try:
amount = int(input("Enter withdrawal amount: "))

if amount > balance:
raise Exception("Insufficient balance")

balance -= amount

print("Remaining Balance:", balance)

except ValueError:
print("Please enter valid amount")

except Exception as e:
print(e)

finally:
print("Transaction completed")

Summary

In this chapter you learned:

✅ Types of errors
try and except
else block
finally block
✅ Raising exceptions
✅ Custom exceptions
✅ Exception handling best practices