UNIT – 1: COMPUTATIONAL THINKING AND PROGRAMMING – 2
1. FUNCTIONS IN PYTHON
A function is a block of code that performs a specific task.
Types of Functions:
• Built-in Functions – print(), len(), type()
• Functions defined in module – [Link]()
• User defined functions
Syntax:
def function_name(parameters):
statements
return value
Types of Parameters:
• Positional Parameters
• Default Parameters
Scope of Variables:
• Local Variable
• Global Variable
2. EXCEPTION HANDLING
Exception is an error that occurs during execution.
Syntax:
try:
statement
except:
statement
finally:
statement
3. FILE HANDLING
Types of Files:
• Text File
• Binary File
• CSV File
Text File Operations:
• Open file
• Read / Write
• Append
• Close file
File Modes:
r, w, a, r+, w+, a+
Reading Methods:
read(), readline(), readlines()
seek() – moves cursor
tell() – returns cursor position
4. BINARY FILE
Binary files store data in binary format.
Uses pickle module:
dump() – write
load() – read
File Modes:
rb, wb, ab, rb+, wb+, ab+
5. CSV FILE
CSV – Comma Separated Values
Write:
[Link]()
writerow()
writerows()
Read:
[Link]()
6. STACK (DATA STRUCTURE)
Stack follows LIFO principle.
Operations:
• Push
• Pop
• Peek
• isEmpty()
Implementation using list:
[Link]()
[Link]()
----------------------------------
Prepared for Class 12 – Python