0% found this document useful (0 votes)
24 views

Python Notes for CBSE Class 12 (1)

These notes provide a comprehensive overview of essential Python concepts for CBSE Class 12, including programming modes, data types, operators, functions, data structures, and object-oriented programming. Key topics such as inheritance, memory management, and algorithms are also covered with examples for better understanding. The structured format aims to aid students in grasping Python programming effectively.

Uploaded by

Rahul Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Python Notes for CBSE Class 12 (1)

These notes provide a comprehensive overview of essential Python concepts for CBSE Class 12, including programming modes, data types, operators, functions, data structures, and object-oriented programming. Key topics such as inheritance, memory management, and algorithms are also covered with examples for better understanding. The structured format aims to aid students in grasping Python programming effectively.

Uploaded by

Rahul Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

These notes are well-structured and cover essential Python concepts for CBSE Class 12 in an easy-to-

understand way. Below is a slightly revised and better-formatted version for clarity and readability:

Python Notes for CBSE Class 12

Modes of Python Programming


1. Interactive Mode: Allows interaction with the Operating System. Commands are executed line by
line.
2. Script Mode: Python programs are written in files and executed using the interpreter.

Basic Data Types


1. Number: Stores numerical values (e.g., integers, floats).
2. Sequence: An ordered collection of items indexed by positive integers.

Operators in Python
1. Arithmetic Operators: `+`, `-`, `*`, `/`, `%`, `**`, `//`
2. Relational Operators: `<`, `<=`, `>`, `>=`, `!=`, `==`
3. Logical Operators: `or`, `and`, `not`
4. Assignment Operators: `=`, `+=`, `-=`, `*=`, `/=`, `%=`, `**=`, `//=`

Functions and Modules


1. Functions: A named sequence of statements performing computations.
2. Module: A file containing Python definitions and statements, extending Python’s standard library.

Data Structures
1. String: A sequence of characters accessed using indexes.
2. List: A sequence of values of any type.
3. Dictionary: Stores key-value pairs where keys can be of any data type.
4. Tuple: An immutable sequence of values indexed by integers.

Namespaces and Scope


1. Namespace: Mapping of names to objects (e.g., global, local, built-in).
2. Scope: Region where a variable or name is accessible.
3. LEGB Rule:

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/6
L: Local (current function)
E: Enclosing functions (inner to outer)
G: Global (module level)
B: Built-in (Python-defined names)

Object-Oriented Programming
1. Class: A blueprint for creating objects.
`__init__`: Initializes class members.
`self`: Refers to the instance of the class.
2. Attributes:
Class attributes: Shared by all instances.
Instance attributes: Specific to individual instances.
3. Methods:
`getattr()`: Access an attribute.
`hasattr()`: Check if an attribute exists.
`setattr()`: Set an attribute with a value.
`delattr()`: Delete an attribute.

Special Class Methods


1. `__dict__`: Returns the dictionary of the class namespace.
2. `__doc__`: Returns the class’s docstring.
3. `__name__`: Gives the class name.
4. `__module__`: Specifies the module where the class is defined.
5. `__bases__`: Tuple containing base classes.
6. `__del__`: Invoked when the object is deleted.
7. `__str__`: Returns a string representation of the object.

Inheritance
1. Single Inheritance: Subclass inherits from one base class.
2. Multilevel Inheritance: Subclass acts as a base for another class.
3. Multiple Inheritance: Subclass inherits from multiple base classes.
4. Hierarchical Inheritance: Multiple subclasses inherit from one base class.
5. Hybrid Inheritance: Combination of multiple inheritance types.

Overriding Methods

Enables customization of parent class methods in the subclass.

Abstract Methods

Declared in a parent class but implemented in the subclass.

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 2/6
Memory Management
1. Reference Counting: Tracks the number of references to an object. When the count is zero, the
object is deleted.
2. Garbage Collection: Reclaims unused memory blocks automatically.

This concise and well-organized version is suitable for CBSE Class 12 students preparing for Python-
based concepts. Let me know if you need more topics or detailed explanations!

Here’s the enhanced version with additional information and Python programs for better
understanding:

Data Structures and Algorithms

What is a Data Structure?


A data structure is a group of data that can be processed as a single unit. It helps in organizing and
storing data efficiently.

Types of Data Structures

1. Linear Data Structures: Data is organized sequentially (e.g., arrays, lists).


2. Non-linear Data Structures: Data is organized hierarchically (e.g., trees, graphs).

Array
An array is a set of contiguous memory locations that store data of a similar type.
In Python, lists function as arrays but are more versatile since they can hold elements of different data
types.

Key Features

Sequential Allocation of Memory: Elements are stored sequentially in memory.


Traversal: Moving through each element in a list from the first to the last sequentially.

Operations on Data Structures


Insertion

Inserting an element in a sorted list requires placing it in its appropriate position while maintaining the
order.

Searching

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 3/6
1. Linear Search:
Compares the search element with each element of the list sequentially.
Suitable for unsorted lists.
Python Example:

python

def linear_search(lst, target):


for i in range(len(lst)):
if lst[i] == target:
return i # Element found
return -1 # Element not found

# Example Usage
lst = [10, 20, 30, 40]
print(linear_search(lst, 30)) # Output: 2

2. Binary Search:
Efficient searching algorithm.
Works only on sorted lists by dividing the list into halves.
Python Example:

python

def binary_search(lst, target):


low, high = 0, len(lst) - 1
while low <= high:
mid = (low + high) // 2
if lst[mid] == target:
return mid
elif lst[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1 # Element not found

# Example Usage
lst = [10, 20, 30, 40, 50]
print(binary_search(lst, 30)) # Output: 2

Sorting

Sorting refers to arranging elements in ascending or descending order.

Sorting Techniques

1. Selection Sort:
Repeatedly selects the smallest element in the unsorted portion and swaps it with the first
unsorted element.
Python Example:

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 4/6
python

def selection_sort(lst):
for i in range(len(lst)):
min_index = i
for j in range(i + 1, len(lst)):
if lst[j] < lst[min_index]:
min_index = j
lst[i], lst[min_index] = lst[min_index], lst[i]

# Example Usage
lst = [64, 25, 12, 22, 11]
selection_sort(lst)
print(lst) # Output: [11, 12, 22, 25, 64]

2. Bubble Sort:
Compares adjacent elements and swaps them if they are in the wrong order.
Python Example:

python

def bubble_sort(lst):
for i in range(len(lst)):
for j in range(0, len(lst) - i - 1):
if lst[j] > lst[j + 1]:
lst[j], lst[j + 1] = lst[j + 1], lst[j]

# Example Usage
lst = [64, 34, 25, 12, 22, 11, 90]
bubble_sort(lst)
print(lst) # Output: [11, 12, 22, 25, 34, 64, 90]

3. Insertion Sort:
Selects the next element, compares it with sorted elements, and places it in the correct
position.
Python Example:

python

def insertion_sort(lst):
for i in range(1, len(lst)):
key = lst[i]
j = i - 1
while j >= 0 and key < lst[j]:
lst[j + 1] = lst[j]
j -= 1
lst[j + 1] = key

# Example Usage
lst = [12, 11, 13, 5, 6]
insertion_sort(lst)
print(lst) # Output: [5, 6, 11, 12, 13]

Key Notes
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 5/6
Traversal: Moving through all elements.
Searching: Finding specific elements.
Sorting: Arranging elements in a specific order.

This combination of notes and Python examples makes it easier to understand and implement data
structure operations! Let me know if you want additional examples or advanced algorithms.

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 6/6

You might also like