UNIT – 1 : COMPUTATIONAL THINKING AND PROGRAMMING – 2
(CLASS 12 – COMPUTER SCIENCE / PYTHON)
--------------------------------------------------
1. INTRODUCTION
--------------------------------------------------
Computational Thinking is a way of solving problems logically using computer programs.
Python is a high-level, easy-to-learn programming language used widely in education,
data science, web development, and automation.
--------------------------------------------------
2. FUNCTIONS IN PYTHON
--------------------------------------------------
A function is a block of reusable code that performs a specific task.
Advantages of Functions:
• Reduces repetition of code
• Improves readability
• Easy debugging
• Code reusability
--------------------------------------------------
TYPES OF FUNCTIONS
--------------------------------------------------
1. Built-in Functions
These are predefined functions available in Python.
Examples:
print(), len(), type(), input(), range()
2. Functions Defined in Modules
Functions stored inside modules.
Example:
import math
[Link](16)
3. User Defined Functions
Functions created by the programmer.
Syntax:
def function_name(parameters):
statements
return value
Example:
def add(a, b):
return a + b
--------------------------------------------------
ARGUMENTS AND PARAMETERS
--------------------------------------------------
Parameter → Variable in function definition
Argument → Value passed during function call
Types:
1. Positional Parameters
2. Default Parameters
Example:
def greet(name="User"):
print("Hello", name)
--------------------------------------------------
RETURN STATEMENT
--------------------------------------------------
Used to return value from function.
Example:
def square(x):
return x * x
--------------------------------------------------
SCOPE OF VARIABLES
--------------------------------------------------
Local Variable – declared inside function
Global Variable – declared outside function
Example:
x = 10
def show():
y=5
--------------------------------------------------
3. EXCEPTION HANDLING
--------------------------------------------------
Exception is an error that occurs during execution.
Purpose:
• Prevents program crash
• Handles runtime errors
Syntax:
try:
risky code
except:
error handling
finally:
always executed
Example:
try:
a = int(input())
print(10/a)
except:
print("Error")
finally:
print("Done")
--------------------------------------------------
4. FILE HANDLING
--------------------------------------------------
File is used to store data permanently.
Types of Files:
1. Text File
2. Binary File
3. CSV File
--------------------------------------------------
TEXT FILE HANDLING
--------------------------------------------------
Opening a file:
f = open("[Link]", "r")
File Modes:
r → read
w → write
a → append
r+ → read & write
w+ → write & read
a+ → append & read
Reading Methods:
read()
readline()
readlines()
Writing Methods:
write()
writelines()
Closing file:
[Link]()
Using with statement:
with open("[Link]") as f:
print([Link]())
seek() → moves file pointer
tell() → returns pointer position
--------------------------------------------------
5. BINARY FILE
--------------------------------------------------
Binary files store data in binary form.
pickle module is used.
import pickle
Writing:
[Link](object, file)
Reading:
[Link](file)
File modes:
rb, wb, ab, rb+, wb+, ab+
--------------------------------------------------
6. CSV FILE
--------------------------------------------------
CSV = Comma Separated Values
Writing:
import csv
writer = [Link](file)
[Link]()
[Link]()
Reading:
reader = [Link](file)
--------------------------------------------------
7. STACK (DATA STRUCTURE)
--------------------------------------------------
Stack follows LIFO principle
(Last In First Out)
Operations:
• Push
• Pop
• Peek
• isEmpty
Implementation using List:
stack = []
[Link](10)
[Link]()
--------------------------------------------------
IMPORTANT EXAM POINTS
--------------------------------------------------
✔ Learn syntax of file handling
✔ Practice programs
✔ Understand stack operations
✔ Prepare binary & CSV questions
✔ Exception handling is compulsory
--------------------------------------------------
END OF NOTES
Prepared for Class 12 Students
--------------------------------------------------