ADVANCED PYTHON PROGRAMMING – EXAM ANSWERS (FULL VERSION)
-----------------------------------------
CHAPTER 3.1 – FILE HANDLING
-----------------------------------------
1. DIFFERENT MODES OF FILE HANDLING IN PYTHON
Python provides several modes to open a file depending on how you want to interact with it.
• "r" – Read mode. Opens file for reading. Error if file doesn't exist.
• "w" – Write mode. Creates a new file or overwrites existing file.
• "a" – Append mode. Adds data at the end of the file.
• "r+" – Read + Write mode without deleting content.
• "w+" – Write + Read mode (overwrites file).
• "a+" – Append + Read mode.
• "rb", "wb", "ab" – Binary versions of read, write, append.
The 'open()' function is used to open files and 'close()' is used to close them.
The 'with' statement is recommended because it automatically closes files.
-----------------------------------------------------
2. PYTHON PROGRAM TO READ AND WRITE DATA
Writing:
with open("[Link]", "w") as f:
[Link]("Hello Python File Handling!")
Reading:
with open("[Link]", "r") as f:
print([Link]())
-----------------------------------------------------
3. CREATING YOUR OWN DATA STREAMS IN PYTHON
Custom data streams can be created using classes implementing read/write methods or with the 'io'
module.
Using [Link]:
from io import StringIO
stream = StringIO()
[Link]("Sample text")
print([Link]())
-----------------------------------------------------
4. COMMON FILE HANDLING FUNCTIONS
• read(), readline(), readlines()
• write(), writelines()
• seek(), tell()
• close()
• open()
-----------------------------------------
CHAPTER 3.2 – RELATIONAL DATABASES
-----------------------------------------
1. SQL STATEMENTS FOR DATA MANIPULATION (DML)
INSERT:
INSERT INTO students(name, age) VALUES ('Satyajit', 23);
SELECT:
SELECT * FROM students;
UPDATE:
UPDATE students SET age = 24 WHERE name = 'Satyajit';
DELETE:
DELETE FROM students WHERE name='Satyajit';
-----------------------------------------------------
2. WORKING WITH DATABASES IN PYTHON (SQLite)
Steps:
1. Import sqlite3
2. Connect to database
3. Create cursor
4. Execute SQL
5. Commit changes
6. Close connection
-----------------------------------------------------
3. PYTHON PROGRAM FOR SQLITE OPERATIONS
import sqlite3
conn = [Link]("[Link]")
cur = [Link]()
[Link]("CREATE TABLE IF NOT EXISTS users(id INTEGER PRIMARY KEY, name TEXT)")
[Link]("INSERT INTO users(name) VALUES ('Satyajit')")
[Link]()
[Link]("SELECT * FROM users")
print([Link]())
[Link]()
-----------------------------------------
CHAPTER 3.3 – MACHINE LEARNING
-----------------------------------------
1. WHAT IS MACHINE LEARNING?
Machine Learning is a method where systems learn patterns from data and make predictions without
explicit programming.
Types:
• Supervised Learning
• Unsupervised Learning
• Reinforcement Learning
-----------------------------------------------------
2. NUMPY FOR NUMERICAL DATA
import numpy as np
arr = [Link]([1,2,3])
matrix = [Link]([[1,2],[3,4]])
print([Link](matrix))
print([Link](matrix, matrix))
-----------------------------------------------------
3. PANDAS FOR DATA ANALYSIS
Pandas provides Series (1D) and DataFrame (2D) structures.
df = [Link]({"name":["A","B"], "age":[20,30]})
print([Link]())
-----------------------------------------------------
4. BASICS OF MATPLOTLIB
Used to create plots.
import [Link] as plt
[Link]([1,2,3],[2,4,6])
[Link]()
-----------------------------------------------------
5. SCATTER PLOTS
[Link]([1,2,3],[3,6,9])
[Link]()
-----------------------------------------------------
6. INTERACTIVE VISUALIZATION
Using libraries like Plotly, Bokeh, and Matplotlib widgets.
-----------------------------------------------------
7. SCIKIT-LEARN
A popular library for ML tasks.
from sklearn.linear_model import LinearRegression
[Link](X, y)
[Link]()
-----------------------------------------
OTHER LONG ANSWERS
-----------------------------------------
1. FILE HANDLING PROCESS (DETAILED)
Includes opening file, reading/writing, pointer management, exception handling, using with-block, and
differentiating between text/binary streams.
-----------------------------------------------------
2. DATABASES (SQLite, MySQL, PostgreSQL)
SQLite – lightweight, file-based
MySQL – stable, scalable, widely used
PostgreSQL – advanced, supports complex queries
Python libraries:
sqlite3, [Link], psycopg2
-----------------------------------------------------
3. MACHINE LEARNING IN PYTHON
• NumPy for numerical operations
• Pandas for data manipulation
• Matplotlib for visualization
• Scikit-learn for ML algorithms
Machine Learning workflow:
Data loading → Preprocessing → Model training → Testing → Prediction
-----------------------------------------------------
END OF DOCUMENT