CLASS 11 PYTHON – ADVANCED DETAILED NOTES WITH EXAMPLES
---------------------------------------------
1. Introduction to Python
---------------------------------------------
• Python is a high■level, interpreted, general■purpose language.
• Used in AI, ML, data science, web development, automation.
• Features: Simple syntax, portable, huge libraries, dynamic typing.
Example:
print("Hello Python")
# Output: Hello Python
---------------------------------------------
2. Python Tokens
---------------------------------------------
Types:
1. Keywords (if, else, import, return)
2. Identifiers (names of variables, functions)
3. Literals (numbers, strings)
4. Operators (+, -, *, ==)
5. Punctuators: ( ), [ ], { }, :, ;, ,
Invalid identifier example:
2name = "Harsh" # invalid (cannot start with digit)
---------------------------------------------
3. Variables & Data Types
---------------------------------------------
• A variable stores values in memory.
• Dynamic typing: type changes automatically.
Example:
x = 10
x = "Hello" # valid in Python
Data Types:
int, float, str, bool, list, tuple, dict, set
Check type:
print(type(10)) #
---------------------------------------------
4. Type Casting
---------------------------------------------
Example:
a = "10"
b = int(a) # "10" → 10
float(3) → 3.0
str(55) → "55"
---------------------------------------------
5. Operators
---------------------------------------------
Arithmetic:
+ - * / % // **
Example:
5 // 2 = 2
5 ** 2 = 25
Comparison:
10 > 5 → True
10 == 20 → False
Logical:
(5 < 10) and (10 > 3) → True
Assignment:
x = 10
x += 5 # x = 15
---------------------------------------------
6. Input & Output
---------------------------------------------
Example:
name = input("Enter name: ")
print("Hello", name)
Formatted output:
age = 17
print(f"I am {age} years old")
---------------------------------------------
7. Strings
---------------------------------------------
• Strings are immutable.
• Indexing: s[0], s[-1]
• Slicing: s[1:4]
String functions:
txt = "Python"
[Link]() # PYTHON
[Link]("th") # index 2
[Link]("Py", "My") # Mython
Split & Join:
s = "a b c".split() # ['a','b','c']
" ".join(s) # "a b c"
---------------------------------------------
8. Conditional Statements
---------------------------------------------
Example:
marks = 82
if marks >= 90:
print("A")
elif marks >= 75:
print("B")
else:
print("C")
---------------------------------------------
9. Loops
---------------------------------------------
While Loop:
i=1
while i <= 5:
print(i)
i += 1
For Loop:
for i in range(1, 6):
print(i)
Break/Continue:
for i in range(1, 10):
if i == 5: break
---------------------------------------------
10. Lists
---------------------------------------------
• Lists are mutable and ordered.
Example:
nums = [10, 20, 30]
[Link](40)
[Link](20)
[Link]()
Slicing:
nums[1:3]
---------------------------------------------
11. Tuples
---------------------------------------------
• Immutable.
Example:
t = (10, 20, 30)
print(t[1]) # 20
print([Link](10)) # 1
---------------------------------------------
12. Dictionary
---------------------------------------------
• Key■value pairs.
Example:
d = {"name": "Harsh", "age": 17}
d["age"] = 18
[Link]({"city": "Delhi"})
Access:
[Link]("name")
---------------------------------------------
13. Sets
---------------------------------------------
• No duplicates, unordered.
Example:
s = {1, 2, 3}
[Link](4)
[Link](2)
Set operations:
{1,2} | {2,3} → union → {1,2,3}
{1,2,3} & {2,3} → intersection → {2,3}
---------------------------------------------
14. Functions
---------------------------------------------
Definition:
def add(a, b):
return a + b
print(add(5, 6)) # 11
Default arguments:
def greet(name="Guest"):
print("Hello", name)
---------------------------------------------
15. Modules (math, random)
---------------------------------------------
import math
[Link](25) → 5.0
[Link](5) → 120
Random module:
import random
[Link](1, 10)
---------------------------------------------
16. File Handling
---------------------------------------------
Modes:
"r" read
"w" write
"a" append
"r+" read/write
Write example:
f = open("[Link]", "w")
[Link]("Hello")
[Link]()
Read example:
f = open("[Link]", "r")
print([Link]())
[Link]()
---------------------------------------------
17. Exception Handling
---------------------------------------------
Example:
try:
a = 10 / 0
except ZeroDivisionError:
print("Cannot divide!")
finally:
print("Done")
---------------------------------------------
18. Important Practice Programs
---------------------------------------------
1. Largest of 3 numbers
2. Prime number checker
3. Sum of digits of a number
4. Fibonacci series
5. Factorial using loop
6. Count vowels in string
7. Reverse a string
8. List sum & max element
9. Dictionary searching
10. File read/write operations
Example: Prime Number
n = 11
flag = True
for i in range(2, n):
if n % i == 0:
flag = False
break
if flag:
print("Prime")
else:
print("Not Prime")
---------------------------------------------
END OF NOTES
---------------------------------------------