Python and SQL for Data Science - Complete Notes
1. What is Python? Why is it used in Data Science?
Python is a high-level, interpreted, object-oriented programming language created in 1991.
It is widely used in Data Science because of its simplicity, powerful libraries, strong
community support, and ability to handle large datasets efficiently.
Key Reasons:
- Easy to learn and readable syntax
- Powerful libraries like NumPy and Pandas
- Supports Machine Learning and AI
- Strong integration with databases
------------------------------------------------------------
2. What is a Variable in Python?
A variable is a name used to store data in memory.
Example:
age = 20
name = "Piku"
marks = 85.5
------------------------------------------------------------
3. Two Python Libraries Used in Data Science
1. NumPy – Used for numerical computations and array operations.
2. Pandas – Used for data analysis and data manipulation.
------------------------------------------------------------
4. Difference Between DDL and DML
DDL (Data Definition Language):
- Used to define database structure
- Commands: CREATE, ALTER, DROP
- Auto-commit
DML (Data Manipulation Language):
- Used to manipulate data
- Commands: INSERT, UPDATE, DELETE
- Requires commit
------------------------------------------------------------
5. What is View and CTE in SQL?
View:
A virtual table created using a SQL query. It stores query logic permanently.
Example:
CREATE VIEW HighSalary AS
SELECT name, salary FROM Employee WHERE salary > 50000;
CTE (Common Table Expression):
A temporary result set used within a query.
Example:
WITH HighSalary AS (
SELECT name, salary FROM Employee WHERE salary > 50000
)
SELECT * FROM HighSalary;
------------------------------------------------------------
6. Python Data Types with Examples
Numeric:
x = 10
y = 10.5
z = 3+4j
String:
name = "Piku"
List:
marks = [90, 85, 88]
Tuple:
coordinates = (10, 20)
Set:
numbers = {1, 2, 3}
Dictionary:
student = {"name": "Piku", "age": 20}
------------------------------------------------------------
7. Role of NumPy and Pandas
NumPy:
- Works with arrays
- Performs fast mathematical operations
- Used in Machine Learning calculations
Pandas:
- Works with structured data (DataFrame)
- Reads CSV, Excel, SQL files
- Used for data cleaning and analysis
------------------------------------------------------------
8. How Python Connects with Database
Steps:
1. Import database library (sqlite3, [Link], etc.)
2. Create connection
3. Create cursor
4. Execute query
5. Fetch results
6. Close connection
Example:
import sqlite3
conn = [Link]("[Link]")
cursor = [Link]()
[Link]("SELECT * FROM students")
rows = [Link]()
[Link]()
------------------------------------------------------------
9. DDL Commands
CREATE:
CREATE TABLE Student (id INT, name VARCHAR(50));
ALTER:
ALTER TABLE Student ADD age INT;
DROP:
DROP TABLE Student;
------------------------------------------------------------
10. DML Commands
INSERT:
INSERT INTO Student (id, name) VALUES (1, 'Piku');
UPDATE:
UPDATE Student SET name = 'Rahul' WHERE id = 1;
DELETE:
DELETE FROM Student WHERE id = 1;
------------------------------------------------------------
11. Python for Data Science (Detailed)
Python is used for:
- Data collection
- Data cleaning
- Data analysis
- Visualization
- Machine Learning
Important Concepts:
Variables – Store data
Data Structures – List, Tuple, Dictionary, Set
Libraries – NumPy, Pandas
Example:
import pandas as pd
df = pd.read_csv("[Link]")
[Link](inplace=True)
------------------------------------------------------------
12. How SQL and Python Work Together
SQL:
- Stores structured data in relational databases
- Uses DDL to define tables
- Uses DML to manipulate records
Python:
- Connects to database
- Fetches data
- Performs analysis and visualization
Example:
import pandas as pd
import sqlite3
conn = [Link]("[Link]")
df = pd.read_sql("SELECT * FROM Sales", conn)
print([Link]())
[Link]()
------------------------------------------------------------
Exam Answering Pattern:
1. Start with definition
2. Explain key points
3. Provide example
4. Conclude with practical use