1. The Power of Functions: Modular Programming
In the real world, software is millions of lines long. We cannot write it in one file. We use Decomposition to break it into small, manageable chunks called Functions.
1.1 Anatomy of a Function
A function has a Header and a Body.
Python
def calculate_area(radius): # Header (Signature)
"""Returns area of circle""" # Docstring (Documentation)
area = 3.14 * radius * radius
return area # Return Statement
def: Keyword to define a function.radius: The Parameter (Placeholder variable).- Indent: Everything indented is the “Body.”
return: Stops the function and sends value back.
1.2 Void vs. Non-Void Functions
- Non-Void (Fruitful) Function: Returns a value (like the example above).
- Void Function: Does not return a value. It performs an action (like printing).
- Exam Note: A void function technically returns
Nonein Python.
- Exam Note: A void function technically returns
1.3 Flow of Execution
Python executes code line-by-line. When a function is called, the flow jumps to the function body, executes it, and jumps back to where it left off.
- Exam Trick: If you define a function but never call it, the code inside never runs!
2. Arguments & Parameters: The Handshake
2.1 Parameters vs. Arguments
- Parameter: Variable in the function definition (
def func(x):). - Argument: Actual value passed during the call (
func(10)).
2.2 The 3 Types of Arguments
- Positional Arguments:
- The most common. Order matters.
def power(a, b):$\rightarrow$power(2, 3)means \(a=2, b=3 \).
- Keyword Arguments (Named Args):
- Order doesn’t matter if you use names.
power(b=3, a=2)works perfectly.
- Default Arguments:
- You can provide a backup value.
def greet(name, msg="Good Morning"):- Crucial Rule: Non-default arguments must come before default arguments.
def f(a=1, b)is a SyntaxError.
2.3 Pass by Object Reference (The “Mutable” Trap)
Does Python pass by Value or Reference? It uses “Pass by Object Reference”.
- Immutable Args (Int, String): Changes inside function do not affect the outside variable.
- Mutable Args (List, Dict): Changes inside function do affect the outside variable.
Example:
Python
def modify(L): L.append(100) # Modifies the original list!
my_list = [1, 2]
modify(my_list)
print(my_list) # Output: [1, 2, 100]
3. Variable Scope: The LEGB Rule
Scope tells us where a variable is “visible.” Python resolves names using the LEGB Rule (checked in this order):
- L – Local: Inside the current function.
- E – Enclosing: Inside enclosing functions (nested functions).
- G – Global: At the top level of the script.
- B – Built-in: Python’s built-in names (like
len,print).
The global Keyword
To write to a global variable inside a function, you must declare it.
Python
x = 10
def change():
global x # Permission to modify global x
x = 20
change()
print(x) # Output: 20
4. File Handling: Persistence
Files allow data to survive after the program ends.
4.1 Absolute vs. Relative Paths
- Absolute Path: The full address from root.
C:\Users\Admin\Documents\data.txt - Relative Path: Address relative to the current script.
data.txtorfolder/data.txt.
4.2 The open() Function and Modes
Syntax: file_object = open(file_name, access_mode)
| Mode | Name | Description | Pointer |
'r' | Read | Default. Error if file missing. | Start |
'w' | Write | Creates/Overwrites file. | Start |
'a' | Append | Writes at end. Preserves old data. | End |
'r+' | Read+Write | File must exist. | Start |
'w+' | Write+Read | Wipes file clean first. | Start |
'a+' | Append+Read | Creates if missing. | End |
Pro-Tip: Always use the with statement. It automatically closes the file even if an error occurs.
with open(“data.txt”, “r”) as f:
5. Text File Operations
Text files store data as characters (ASCII/Unicode). Python handles the translation of \n (newline) automatically.
Reading Methods
read(n): Reads \(n \) bytes. If \(n \) is empty, reads the whole file.readline(): Reads a single line (up to\n). Returns empty string''at EOF.readlines(): Returns a List containing all lines.['Line1\n', 'Line2\n'].
Writing Methods
write(str): Writes a string. (You must add\nmanually).writelines(list): Writes a list of strings. (Does not add\nautomatically).
6. Binary File Operations
(Syllabus Topic: Pickle module, dump, load, EOFError)
Binary files (.dat, .jpg, .mp3) store data as raw bytes. They are faster and secure (not human readable).
Pickling (Serialization)
The process of converting a Python object (List/Dict) into a byte stream.
- Module:
import pickle - Function:
pickle.dump(data, file_object)
Unpickling (Deserialization)
Converting a byte stream back to a Python object.
- Function:
pickle.load(file_object)
Handling the End of File (EOF):
When reading binary files, you don’t get an empty string at the end; you get an error. You must handle it with try-except.
Python
try: while True: data = pickle.load(f) print(data) except EOFError: f.close()
7. CSV File Operations
CSV (Comma Separated Values) is the universal format for spreadsheets.
Writing CSV
We use csv.writer or csv.DictWriter.
- Important: Open file with
newline=''to prevent blank rows in Windows.
Python
import csv
with open('student.csv', 'w', newline='') as f:
w = csv.writer(f)
w.writerow(['Name', 'Roll']) # Header
w.writerow(['Ravi', 1]) # Data
Reading CSV
We use csv.reader. It returns an iterator.
Python
with open('student.csv', 'r') as f:
data = csv.reader(f)
for row in data:
print(row) # row is a list ['Ravi', '1']
8. Seek and Tell: Navigating the File
(Syllabus Topic: File pointers, random access)
Imagine a cassette tape. You can rewind or fast-forward.
f.tell(): Tells you the current byte position (Integer).f.seek(offset, whence): Moves the pointer.seek(10, 0): Move 10 bytes from Beginning (0).seek(10, 1): Move 10 bytes from Current (1). (Binary mode only)seek(-10, 2): Move 10 bytes back from End (2). (Binary mode only)
Exam Corner: 5 Tricky MCQs
- Q: What is the return type of
f.readlines()?- A) String
- B) List
- C) Tuple
- Ans: B (List).
- Q: Which mode opens a file for reading in binary format?
- A)
rb - B)
r - C)
bin - Ans: A (
rb).
- A)
- Q: In
seek(offset, whence), what is the default value ofwhence?- A) 1 (Current)
- B) 2 (End)
- C) 0 (Beginning)
- Ans: C (0).
- Q: Which exception is raised when
pickle.load()hits the end of the file?- A)
StopIteration - B)
EOFError - C)
IOError - Ans: B.
- A)
- Q: Can a global variable be modified inside a function without the
globalkeyword?- A) Yes
- B) No
- Ans: B. (You can read it, but you cannot modify it without the keyword).
Next Lesson: [Module 4: Data Handling with Pandas]
