Python Mid–2 Exam Notes
Organized by: Hemanth
6 Marks Questions & Answers
1. Modes of File with Example
File modes: 'r' (read), 'w' (write), 'a' (append), 'r+' (read/write). Example:
f = open('[Link]', 'w'); [Link]('Hello'); [Link]()
2. File Object Methods
read() – entire file, readline() – one line, readlines() – list of lines, tell() – position, seek() – move
cursor.
3. Namespace & Types
Namespace: area where names map to objects.
Types: Local, Global, Built-in, Enclosing.
4. Built-in Modules: Math & Statistics
math: sqrt(), pow(), ceil().
statistics: mean(), median(), mode().
5. Difference between Module & Package
Module – single .py file.
Package – collection of modules with __init__.py file.
6. Principles of Object Orientation
Encapsulation, Abstraction, Inheritance, Polymorphism.
7. Regex Functions
findall(), search(), split(), sub(), match() – used to search/manipulate strings.
8. Class & Object
class Student:
def __init__(self,name): [Link]=name
Example: s=Student('Hemanth')
9. Built-in Module Functions
Example: [Link](16)=4, [Link](1,10)=random number.
10. Encapsulation, Overloading, Overriding
Encapsulation: data hiding.
Overloading: same name diff args.
Overriding: child replaces parent method.
12 Marks Questions & Answers
1. Python File Handling
File: collection of data. Modes – r, w, a, r+. Operations: open(), read(), write(), close().
Example:
import os
f=open('[Link]','w'); [Link]('Hello'); [Link]()
2. Modules & Arithmetic Example
Module: Python file with functions.
Example: [Link] -> add(), sub(), mul(), div().
Import using import arith.
3. Package and Subpackage Creation
Steps:
1. Create folder mypack
2. Add __init__.py
3. Add module [Link]
4. For subpackage, add folder inside with own __init__.py.
4. Database Connectivity (sqlite3)
import sqlite3
con=[Link]('[Link]')
cur=[Link]()
[Link]('CREATE TABLE student(id INT, name TEXT)')
[Link]()
5. Inheritance
Inheritance: properties of one class to another.
Types: single, multiple, multilevel, hierarchical, hybrid.
Example:
class A: pass
class B(A): pass
6. Password Validation Program
import re
p=input('Enter password: ')
if [Link]('[A-Z]',p) and [Link]('[a-z]',p) and [Link]('[0-9]',p) and [Link]('[@$!%*#?&]',p)
and len(p)>=8:
print('Valid')
else:
print('Invalid')
7. Regular Expressions & Meta Characters
Regex: pattern for matching strings.
Meta characters: . ^ $ * + ? { } [ ] \ | ( )