0% found this document useful (0 votes)
6 views2 pages

Master Python Guide for CBSE 11-12

This document is a comprehensive Python program for Class 11 and 12 CBSE students, covering various topics such as custom exceptions, functions, exception handling, comprehensions, file handling, and modules. It includes practical examples and demonstrations of each concept, such as using eval and exec, handling exceptions, and working with text, CSV, and binary files. Additionally, it showcases the use of the OS and String modules, providing a well-rounded overview of Python programming.

Uploaded by

abhineet.8471
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Master Python Guide for CBSE 11-12

This document is a comprehensive Python program for Class 11 and 12 CBSE students, covering various topics such as custom exceptions, functions, exception handling, comprehensions, file handling, and modules. It includes practical examples and demonstrations of each concept, such as using eval and exec, handling exceptions, and working with text, CSV, and binary files. Additionally, it showcases the use of the OS and String modules, providing a well-rounded overview of Python programming.

Uploaded by

abhineet.8471
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Python

# ===================================================== # Custom Exception


# MASTER PYTHON PROGRAM - CLASS 11 + 12 (CBSE 083) class MyError(Exception):
# ===================================================== pass

import random try:


import math x = int(input("50 se bada number daal: "))
import datetime if x <= 50:
import string raise MyError("Bhai 50 se bada daal na!")
import os except MyError as m:
import csv print(f"Custom Error: {m}")
import pickle
from collections import Counter, defaultdict, namedtuple # 3. Functions - *args, **kwargs, nested, nonlocal
print("\n3. FUNCTIONS - Advanced")
print("="*70) def full_func(a, b, *args, **kwargs):
print(" ULTIMATE CLASS 12 PYTHON - SAB KUCH EK HI PROGRAM MEIN") print(f"a={a}, b={b}")
print("="*70) print("args:", args)
print("kwargs:", kwargs)
# 1. Input + eval() + exec()
print("\n1. INPUT + eval() + exec()") full_func(1, 2, 3, 4, 5, name="Aman", marks=95)
name = input("Naam daalo: ").strip()
age = eval(input("Age daalo (number): ")) def outer():
code = input("Ek chhota Python code likho (jaise 5+3): ") x = "outer"
exec(f"result = {code}") def inner():
print(f"Exec result: {result}") nonlocal x
x = "inner"
# 2. Exception Handling (Full) print("Inner:", x)
print("\n2. EXCEPTION HANDLING - Full Demo") inner()
try: print("Outer:", x)
num = int(input("Ek number daalo: ")) outer()
print(100 / num)
except ZeroDivisionError: # 4. Lambda + map + filter + zip
print("Zero se divide nahi kar sakte!") print("\n4. LAMBDA + MAP + FILTER + ZIP")
except ValueError: nums = [1,2,3,4,5,6,7,8,9,10]
print("Sirf number daal bhai!") squares = list(map(lambda x: x*x, nums))
except Exception as e: evens = list(filter(lambda x: x%2==0, nums))
print(f"Kuch aur error: {e}") z = list(zip(nums, squares))
else: print("Squares:", squares[:5])
print("Sab badhiya chala!") print("Evens:", evens)
finally: print("Zip sample:", z[:3])
print("Finally toh hamesha chalega!\n")
# 5. List, Dict, Set Comprehension
print("\n5. COMPREHENSIONS")
lst_comp = [x**2 for x in range(1,11) if x%2!=0]
dict_comp = {i: chr(65+i) for i in range(5)} # 9. namedtuple
set_comp = {x for x in "abracadabra" if x in "abc"} print("\n9. NAMEDTUPLE")
print("Odd squares:", lst_comp) Point = namedtuple('Point', ['x', 'y'])
print("Dict comp:", dict_comp) p = Point(10, 20)
print("Set comp:", set_comp) print(f"Point: x={p.x}, y={p.y}")

# 6. File Handling - Text + CSV + Binary


print("\n6. FILE HANDLING - All Types")

# Text file
with open("[Link]", "w") as f:
[Link]("Hardcoder jeetega!\nPython is best\n")
with open("[Link]") as f:
print("Text file:", [Link]())

# CSV file
data = [["Name","Marks"], ["Aman",95], ["Rahul",98]]
with open("[Link]", "w", newline="") as f:
writer = [Link](f)
[Link](data)
with open("[Link]") as f:
print("CSV content:")
print([Link]())

# Binary file with pickle


student = {"name":"Vikash", "class":12, "percentage":96.8}
with open("[Link]", "wb") as f:
[Link](student, f)
with open("[Link]", "rb") as f:
loaded = [Link](f)
print("Binary file loaded:", loaded)

# 7. OS Module
print("\n7. OS MODULE")
print("Current folder:", [Link]())
print("Files in folder:", [Link](".")[:5])

# 8. String module
print("\n8. STRING MODULE")
print("All letters:", string.ascii_letters[:20] + "...")
print("Digits:", [Link])
print("Punctuation:", [Link][:15])

You might also like