Computational Thinking and Programming – 2
Computational Thinking and Programming – 2
Here are well-structured Class 12 CBSE Board Exam Notes for Computational Thinking and Returns value
Programming – 2 based on your syllabus:
Scope of a Variable
Computational Thinking and Programming – 2 Local Scope: Variables declared inside a function.
Global Scope: Variables declared outside a function (use `global` keyword to modify inside
1. Revision of Python Topics Covered in Class XI function).
Python Basics: Variables, Data Types, Operators, Conditional Statements, Loops python
Strings, Lists, Tuples, Dictionaries, Sets
Functions, File Handling, Exception Handling `x = 10 # Global variable
def myfunc():
Libraries: NumPy, Pandas (Basic Concepts) global x
x = x + 5
print(x)
myfunc() # Output: 15
2. Functions in Python `
Types of Functions
3. Exception Handling
1. Built-in Functions: Predefined functions like `len()`, `max()`, `print()`, `input()`, `type()`.
2. Functions Defined in Modules: Functions in Python libraries (`math.sqrt()`, `random.randint()`).
Introduction to Exception Handling
3. User-Defined Functions: Functions created by programmers using `def` keyword.
Exception: Runtime error that disrupts program flow (e.g., `ZeroDivisionError`,
Creating User-Defined Functions `FileNotFoundError`).
Exception Handling: Using `try-except-finally` to prevent program crash.
python
`def greet(name):
Handling Exceptions Using `try-except-finally`
return "Hello, " + name + "!"
print(greet("Rahul")) # Output: Hello, Rahul! python
`
`try:
x = int(input("Enter a number: "))
Arguments and Parameters y = 10 / x
print(y)
except ZeroDivisionError:
Positional Parameters: Arguments passed in order. print("Cannot divide by zero!")
Default Parameters: Parameters with default values. except ValueError:
print("Invalid input! Enter a number.")
Keyword Arguments: Arguments passed with parameter names. finally:
Variable-Length Arguments: `*args` (multiple positional arguments), `**kwargs` (multiple print("Execution Completed.")
`
keyword arguments).
python
`f = open("example.txt", "w")
f.write("Hello, Python!")
6. CSV File Handling
f.close()
`
Opening and Closing a CSV File
python
`seek()` and `tell()` Methods
`import csv
with open("data.csv", "w", newline="") as f:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 3/10 Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 4/10
writer = csv.writer(f) 🔹 Exception → A runtime error that disrupts normal program flow.
writer.writerow(["Name", "Age"])
🔹 Common Exceptions → , , , etc.
🔹 Exception Handling → Using
writer.writerows([["Rahul", 18], ["Amit", 20]]) `ZeroDivisionError` `ValueError` `FileNotFoundError`
`
to handle errors and prevent program crashes.
`try-except-finally`
python
`stack = []
# Push operation 📌 File Handling
1️⃣ Introduction to Files
stack.append(10)
stack.append(20)
stack.append(30)
print(stack) # Output: [10, 20, 30]
Here are concise, easy-to-understand, and pointwise notes for Class 12 CBSE Board Exam on `f = open("file.txt", "r") # Open file in read mode
print(f.read()) # Read entire file content
Exception Handling, File Handling, and Stack in Python, including code snippets for better f.close() # Close the file
understanding. `
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 5/10 Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 6/10
→ Write (Overwrites existing content) 📌 Binary File Operations:
✔ Opening & Closing a Binary File
`w`
`w+` → Write & Read
`a` → Append ✔ Reading & Writing using Module
✔ Updating, Searching, and Appending Data
`pickle`
`a+` → Append & Read
`import pickle
`f = open("file.txt", "r") data = {"name": "Rahul", "age": 18}
print(f.readline()) # Reads one line
print(f.readlines()) # Reads all lines as a list # Writing data to binary file
f.close() with open("data.bin", "wb") as f:
` pickle.dump(data, f)
`f = open("file.txt", "w")
f.write("Hello, World!\n") # Writes a string to the file
📌 Binary File Open Modes:
f.writelines(["Line 1\n", "Line 2\n"]) # Writes multiple lines
f.close() `rb` → Read Binary
` `rb+` → Read & Write Binary
`wb` → Write Binary (Overwrites)
`wb+` → Write & Read Binary
Using `with` Clause (Automatic Closing)
`ab` → Append Binary
`
print(content) # File closes automatically
📌 CSV (Comma-Separated Values) File → Used to store tabular data.
📌 Requires Module to handle CSV files.
`csv`
`import csv
with open("data.csv", "r") as f:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 7/10 Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 8/10
reader = csv.reader(f) ✅ Exception Handling: Use to handle errors.
✅ Text File Handling: Open, Read, Write, Append,
`try-except-finally`
for row in reader:
`seek()` `tell()`.
,
✅ Binary File Handling: Use
print(row) # Output: ['Name', 'Age'], ['Rahul', '18'], ['Amit', '20']
`
module ( , ).
✅ CSV File Handling: Use module (
`pickle` `dump()` `load()`
, ).
✅ Stack Implementation: Use list (
`csv` `writer()` `reader()`
1️⃣ Implementing Stack using List ✔ Write & execute Python programs to check outputs.
✔ Solve CBSE sample papers & previous year questions.
python
These notes are simple, pointwise, and exam-focused with easy-to-understand code snippets. Let
`stack = [] me know if you need sample questions or additional explanations! 😊🔥
# Push Operation
stack.append(10)
stack.append(20)
stack.append(30)
print(stack) # Output: [10, 20, 30]
# Pop Operation
stack.pop()
print(stack) # Output: [10, 20]
`
`stack = []
def push(element):
stack.append(element)
print(f"Pushed {element} into stack")
def pop():
if len(stack) == 0:
print("Stack is empty!")
else:
print(f"Popped {stack.pop()} from stack")
push(10)
push(20)
pop() # Output: Popped 20 from stack
print(stack) # Output: [10]
`