Docsity

Python for Data Science Complete Notes | SQLite, NumPy, Pandas, Matplotlib & Scikit-learn, Summaries of Computer science

Toggle sidebar
bg1
Python for Data: A Student's Complete
Guide
This guide covers five essential Python libraries every MCA student must master: SQLite for databases, NumPy for
numerical computing, Pandas for data analysis, Matplotlib for visualization, and Scikit-learn for machine learning. Each
section breaks down concepts in plain English with real code examples you can run and experiment with. Whether you're
preparing for exams or building real projects, this guide gives you a clear, practical foundation in Python's data ecosystem.
Renu kundan
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Python for Data Science Complete Notes | SQLite, NumPy, Pandas, Matplotlib & Scikit-learn and more Summaries Computer science in PDF only on Docsity!

Python for Data: A Student's Complete

Guide

This guide covers five essential Python libraries every MCA student must master: SQLite for databases, NumPy for numerical computing, Pandas for data analysis, Matplotlib for visualization, and Scikit-learn for machine learning. Each section breaks down concepts in plain English with real code examples you can run and experiment with. Whether you're preparing for exams or building real projects, this guide gives you a clear, practical foundation in Python's data ecosystem.

PART A โ€” DATABASES

What Is a Database? Connecting Python to

SQLite

Database Definition

A database is an organized collection of related data that can be easily stored, retrieved, updated, and managed. Think of it as a digital filing cabinet where information is structured so you can find exactly what you need, fast. Common examples include a Student Database , Employee Database , and Library Database.

Python + SQLite

Python can interact with many databases โ€” SQLite , MySQL, PostgreSQL, and Oracle. For MCA exams, SQLite is the most commonly used because it is built directly into Python โ€” no installation needed.

To get started, import the sqlite3 module and connect to a database file:

import sqlite

conn = sqlite3.connect("college.db") print("Database Connected")

The sqlite3.connect() function creates a new database file if it doesn't exist, or opens it if it does. The connection object conn is your gateway to the database.

The Cursor Object

Once connected, you need a cursor to execute SQL queries. Think of the cursor as a messenger โ€” it carries your commands to the database and brings back results. You create a cursor using conn.cursor().

cursor = conn.cursor()

With the cursor ready, you can run any SQL command โ€” creating tables, inserting data, reading records, updating values, or deleting rows. After executing write operations (INSERT, UPDATE, DELETE), always call conn.commit() to save changes, and conn.close() when done to release the connection.

PART A โ€” DATABASES

CRUD Operations: Insert, Read, Update,

Delete

CRUD stands for Create, Read, Update, Delete โ€” the four fundamental operations you perform on data in any database. In SQL, these map to INSERT, SELECT, UPDATE, and DELETE. Together with DDL, these form the complete toolkit for working with databases in Python.

INSERT โ€” Add a Record

cursor.execute( "INSERT INTO Student VALUES( 1 ,'Rahul', 85 )" ) conn.commit()

Inserts a new row into the Student table.

SELECT โ€” Read Records

cursor.execute("SELECT * FROM Student") rows = cursor.fetchall() for row in rows: print(row)

Output: (1, 'Rahul', 85)

UPDATE โ€” Modify a Record

cursor.execute( "UPDATE Student SET marks= 95 WHERE id= 1 " ) conn.commit()

Changes Rahul's marks from 85 to 95.

DELETE โ€” Remove a Record

cursor.execute( "DELETE FROM Student WHERE id= 1 " ) conn.commit()

Removes the student with id=1.

DDL vs DML โ€” Key Difference

DDL (Data Definition Language) defines the database structure โ€” commands like CREATE, ALTER, DROP, and TRUNCATE change the schema. DML (Data Manipulation Language) works with the data inside tables โ€” commands like INSERT, UPDATE, DELETE, and SELECT change records. Remember: DDL changes the container, DML changes the contents.

DDL DML

Defines database structure Manipulates data inside tables

CREATE, ALTER, DROP, TRUNCATE INSERT, UPDATE, DELETE, SELECT

Changes schema Changes records

PART B โ€” NUMPY

NumPy: Numerical Python for Fast

Computing

NumPy (Numerical Python) is one of the most important libraries in the Python data ecosystem. It is designed for numerical computing , scientific calculations, matrix operations, and fast array processing. NumPy arrays are significantly faster than regular Python lists , use less memory , and support vectorized operations โ€” meaning you can apply math to entire arrays without writing loops.

Faster Than Lists

NumPy arrays are optimized for speed and can process millions of numbers much faster than standard Python lists.

Less Memory Usage

NumPy stores data more efficiently, using significantly less memory compared to equivalent Python list structures.

Vectorized Operations

Apply mathematical operations to entire arrays at once without writing explicit for-loops โ€” cleaner and faster code.

Creating Arrays

Import NumPy with the alias np (a universal convention). Use np.array() to create arrays. You can create 1D arrays (a single row of values) or 2D arrays (rows and columns, like a table).

import numpy as np

1 D Array

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

Output: [ 1 2 3 4 ]

2 D Array

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

Output: [[1 2]

[3 4]]

PART C โ€” PANDAS

Pandas: Series, DataFrame & Data

Structures

Pandas is the go-to Python library for data analysis, data cleaning, and data manipulation. It is built on top of NumPy and provides easy-to-use data structures that resemble spreadsheets or SQL tables. Import it with the alias pd โ€” a universal convention in the Python community.

1

Series โ€” 1D Labeled

Array

A Series is a one-dimensional labeled array, like a single column in a spreadsheet. Each value has an index label (0, 1, 2โ€ฆ by default).

import pandas as pd s = pd.Series([ 10 , 20 , 30 ]) print(s)

Output:

0 10

1 20

2 30

2

DataFrame โ€” 2D Table

A DataFrame is a two- dimensional table โ€” rows and columns, just like an Excel sheet. You can create one from a Python dictionary.

data = { "Name": ["A", "B"], "Marks": [90, 85] } df = pd.DataFrame(data) print(df)

Output:

Name Marks

0 A 90

1 B 85

3

Panel โ€” 3D Structure

(Deprecated)

A Panel was a three-dimensional data structure in older Pandas versions. It has been removed from modern Pandas , but some MCA syllabi still include it. Conceptually: Series โ†’ 1D, DataFrame โ†’ 2D, Panel โ†’ 3D.

PART C โ€” PANDAS

Pandas: Indexing, Sorting & Iteration

Beyond creating DataFrames, Pandas gives you powerful tools to explore, reorganize, and navigate your data. You can change row labels with reindexing , sort data by any column, loop through rows, and inspect the index structure. These operations are essential for real-world data analysis tasks.

Index Objects & Reindexing

The index is the label system that identifies each row in a DataFrame. You can view it with df.index and change it with reindex().

View index

df.index

Reindex โ€” change row order

df = df.reindex([1, 0])

Reindexing is useful when you need to reorder rows or align two DataFrames by their labels.

Sorting & Iteration

Sort a DataFrame by any column using sort_values(). To loop through every row, use iterrows().

Sort by Marks column

df.sort_values("Marks")

Iterate through rows

for i, row in df.iterrows(): print(row)

iterrows() returns both the index (i) and the row data, making it easy to process each row one at a time.

1D

Series

Single labeled column of data

2D

DataFrame

Full table with rows and columns

3D

Panel

Deprecated 3D structure (legacy)

PART E โ€” SCIKIT-LEARN

Scikit-learn: Machine Learning Made

Simple

Scikit-learn (imported as sklearn) is Python's most widely used library for machine learning. It provides simple, efficient tools for classification, regression, clustering, and prediction. The workflow follows a clear pattern: load data โ†’ train a model โ†’ make predictions โ†’ save the model.

Make Save Model

Prediction

Load Train Model

Dataset

This four-step pattern is the foundation of almost every machine learning project in Scikit-learn. Once you understand it, you can swap in different datasets and models easily.

Training a Model

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

data = load_iris() model = DecisionTreeClassifier() model.fit(data.data, data.target)

load_iris() provides a built-in dataset. fit() trains the model on the data. The decision tree learns patterns to classify iris flowers.

Prediction & Model Persistence

Make a prediction

prediction = model.predict( [[5.1, 3.5, 1.4, 0.2]] ) print(prediction)

Save the model

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

Load it later

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

Model persistence means saving a trained model so you don't have to retrain it every time. joblib.dump() saves it; joblib.load() restores it โ€” ready to predict immediately.

Quick Recap: You've now covered all five pillars โ€” SQLite (databases & CRUD), NumPy (arrays & math), Pandas (Series & DataFrames), Matplotlib (charts & graphs), and Scikit-learn (ML training & prediction). Master these and you have a complete Python data toolkit for exams and beyond.