Class12 CS Combined Revision Notes
Class12 CS Combined Revision Notes
1. Introduction to Python
- Python is an interpreted, high-level programming language.
- It is dynamically typed and easy to read due to its simple syntax.
4. Operators
- Arithmetic: +, -, *, /, %, //, **
- Relational: ==, !=, <, >, <=, >=
- Logical: and, or, not
- Assignment: =, +=, -=, etc.
6. Conditional Statements
if condition:
# statements
elif condition:
# statements
else:
# statements
7. Loops
- for loop: Used for iterating over a sequence (list, string, etc.)
Example: for i in range(5): print(i)
- while loop: Repeats while condition is true.
- Loop Control: break (exit loop), continue (skip to next iteration), pass (do nothing)
8. Type Casting
- str(), int(), float(), list(), tuple() - convert from one type to another
1. Strings
- Sequence of characters; immutable.
- Common Methods: lower(), upper(), find(), replace(), split(), join(), strip()
- Slicing: str[start:stop:step]
2. Lists
- Ordered, mutable collection.
- Methods: append(), extend(), insert(), remove(), pop(), sort(), reverse()
- Indexing and slicing similar to strings
3. Tuples
- Ordered, immutable collection.
- Useful for fixed data (like coordinates, RGB colors)
4. Dictionaries
- Unordered, mutable collection of key-value pairs.
- Syntax: dict = {'name': 'Alice', 'age': 17}
- Methods: keys(), values(), items(), get(), update(), pop()
5. Sets
- Unordered, unindexed, no duplicate items.
- Useful for membership tests and set operations (union, intersection).
6. List Comprehension
- Compact way to create lists:
[x for x in range(10) if x % 2 == 0]
1. What is a Function?
- A function is a reusable block of code that performs a specific task.
- Helps reduce redundancy and makes code more readable.
2. Types of Functions
- Built-in Functions: Predefined (e.g., print(), len(), input(), type())
- User-defined Functions: Created by the programmer using def
3. Defining a Function
Syntax:
def function_name(parameters):
statements
return value
Example:
def greet(name):
print("Hello", name)
4. Calling a Function
- Just use the function name with arguments: greet("Haru")
6. Return Statement
- Used to return a value from a function
- Ends the execution of the function
Example:
def add(a, b):
return a + b
7. Default Parameters
- Parameters with default values
Example:
def greet(name="User"):
print("Hello", name)
Example:
def student(name, age): pass
student(age=17, name="Haru")
9. Variable Scope
- Local Scope: Defined inside function
- Global Scope: Defined outside function
global x
x=5
def show():
global x
x=x+1
10. Recursion
- Function that calls itself
- Must have a base condition to end recursion
Example:
def factorial(n):
if n == 1:
return 1
return n * factorial(n - 1)
Example:
square = lambda x: x*x
print(square(5)) # Output: 25
Tips:
- Always trace recursive functions.
- Use return wisely.
- Know the difference between arguments and parameters.
- Use local/global properly.