🌟 1.
Introduction to Python
Python is a high-level, interpreted, and easy-to-learn programming language.
It was developed by Guido van Rossum in 1991.
It is used for web development, data science, machine learning, automation, and many more
areas.
✨ Features of Python
1. Simple syntax – looks like English.
2. Interpreted – runs line by line, no need to compile.
3. Portable – runs on Windows, Mac, and Linux.
4. Dynamically typed – no need to mention variable type.
5. Object-oriented – supports classes and objects.
6. Free and open-source.
7. Large library support.
✅ 2. Advantages of Python
1. Easy to learn and understand.
2. Portable (run anywhere).
3. Free and open-source.
4. Huge library support.
5. Short and readable code.
6. Used in many applications (AI, Web, ML, Data Science).
⚠️3. Limitations of Python
1. Slower than C/C++ (because it is interpreted).
2. Not suitable for mobile app development.
3. Uses more memory.
4. Runtime errors may occur (no compile-time checking).
5. Doesn’t support real multithreading (due to GIL).
💻 4. Ways to Write Python Programs
Mode Description Example
Interactive Mode Type and run line by line in Python shell >>> print("Hello")
Script Mode Write full program in .py file and run python [Link]
🔁 5. Mutable and Immutable Objects in Python
💡 Basic Idea
Every variable in Python stores a reference (memory address) to an object in memory.
When you change the value of a variable:
If the object can be changed → it is mutable.
If the object cannot be changed → it is immutable.
🔷 1. Mutable Objects
👉 Mutable objects can be changed or modified after they are created.
When you modify them, the same memory location (id) is used.
✅ Examples:
list
dictionary
set
🔹 Example 1: Mutable List
numbers = [10, 20, 30]
print("Before:", numbers)
print("Memory id before:", id(numbers))
[Link](40) # modify list
print("After:", numbers)
print("Memory id after:", id(numbers))
Output:
Before: [10, 20, 30]
Memory id before: 140210194493888
After: [10, 20, 30, 40]
Memory id after: 140210194493888
✅ The memory id is the same, which means the list changed in place — that’s why it’s
mutable.
🔹 Example 2: Mutable Dictionary
person = {"name": "Riya", "age": 20}
print(id(person))
person["age"] = 21 # change value
print(person)
print(id(person))
✅ Same id → object updated inside same memory block.
🔷 2. Immutable Objects
👉 Immutable objects cannot be changed after they are created.
If you try to modify them, Python creates a new object in a new memory location.
✅ Examples:
int
float
string
tuple
🔹 Example 1: Immutable String
name = "Riya"
print("Before:", name)
print("Memory id before:", id(name))
name = name + " Sharma" # modify string
print("After:", name)
print("Memory id after:", id(name))
Output:
Before: Riya
After: Riya Sharma
Memory id before: 140206283493296
Memory id after: 140206283496784
⚠️The memory id changed, meaning the old object was replaced by a new one → strings are
immutable.
🔹 Example 2: Immutable Tuple
t = (1, 2, 3)
print(id(t))
t = t + (4,)
print(id(t))
⚠️The id changes because tuples cannot be changed, so Python makes a new tuple.
🔁 Comparison Summary
Property Mutable Immutable
Can value be changed? ✅ Yes ❌ No
Memory location after modification Same Changes
Examples list, dict, set int, float, str, tuple
Used when You want editable data You want constant or fixed data
➕ 6. Operators in Python
Operators are symbols used to perform operations on values or variables.
🧮 A. Arithmetic Operators
Operator Meaning Example Output
+ Addition 10 + 5 15
Operator Meaning Example Output
- Subtraction 10 - 5 5
* Multiplication 4*3 12
/ Division 10 / 4 2.5
// Floor Division 10 // 4 2
% Modulus (remainder) 10 % 3 1
** Power 2 ** 3 8
📝 B. Assignment Operators
Operator Meaning Example
= Assign value x = 10
+= Add and assign x += 5 (x = x + 5)
-= Subtract and assign x -= 2
*= Multiply and assign x *= 3
/= Divide and assign x /= 2
%= Modulus and assign x %= 2
**= Power and assign x **= 2
⚖️C. Comparison Operators
Operator Meaning Example Output
== Equal to 5 == 5 True
!= Not equal to 5 != 3 True
> Greater than 5>3 True
< Less than 2<5 True
>= Greater or equal 5 >= 5 True
<= Less or equal 3 <= 5 True
🧠 D. Logical Operators
Operator Meaning Example Output
and True if both true (5 > 3) and (6 > 4) True
or True if one true (5 > 3) or (6 < 4) True
not Reverse result not(5 > 3) False
⚙️E. Identity Operators
Operator Meaning Example Output
is Same object x is y True/False
is not Not same object x is not y True/False
📚 F. Membership Operators
Operator Meaning Example Output
in Value present 3 in [1,2,3] True
not in Value not present 4 not in [1,2,3] True
🔤 7. Strings in Python
A string is a collection of characters inside quotes.
Example:
name = "Python"
Strings are immutable (cannot change characters).
✂️String Slicing
s = "Python"
print(s[0:3]) # Pyt
print(s[:]) # Python
print(s[::-1]) # Reverse → nohtyP
🧰 String Functions
Function Description Example Output
upper() Convert to uppercase "hi".upper() HI
lower() Convert to lowercase "HI".lower() hi
title() First letter capital "hello world".title() Hello World
capitalize() First letter capital only "python".capitalize() Python
strip() Remove spaces " hello ".strip() hello
replace(a,b) Replace substring "good".replace("g","f") food
split() Split string into list "a,b,c".split(",") ['a','b','c']
join() Join list to string " ".join(['Hi','All']) Hi All
find(x)
Find first position of "banana".find("a") 1
substring
rfind(x) Find last position "banana".rfind("a") 5
Function Description Example Output
index(x)
Same as find(), error if not "banana".index("a") 1
found
rindex(x)
Same as rfind(), error if not "banana".rindex("a") 5
found
count(x) Count substring "banana".count("a") 3
startswith(x) Check start "python".startswith("py") True
endswith(x) Check end "[Link]".endswith(".txt") True
isdigit() Check all digits "123".isdigit() True
isalpha() Check all letters "abc".isalpha() True
isalnum() Check letters & numbers "abc123".isalnum() True
💬 String Formatting
1️⃣ Using format()
name = "Tina"
age = 21
print("My name is {} and I am {} years old.".format(name, age))
2️⃣ Using f-string
name = "Tina"
age = 21
print(f"My name is {name} and I am {age} years old.")
🧾 8. Simple Python Programs
✅ 1. Input a Digit and Print in Words
digit = int(input("Enter a digit (0-9): "))
if digit == 0:
print("Zero")
elif digit == 1:
print("One")
elif digit == 2:
print("Two")
elif digit == 3:
print("Three")
elif digit == 4:
print("Four")
elif digit == 5:
print("Five")
elif digit == 6:
print("Six")
elif digit == 7:
print("Seven")
elif digit == 8:
print("Eight")
elif digit == 9:
print("Nine")
else:
print("Invalid input")
✅ 2. List Comprehension Example
# Squares of numbers
squares = [x**2 for x in range(1, 6)]
print(squares)
# Even numbers
evens = [x for x in range(10) if x % 2 == 0]
print(evens)
# Cube divided by 3
x = [int(i**3/3) for i in range(0,5,2)]
print(x)
✅ 3. Display ASCII Code and Character
for i in range(65, 91):
print(i, "->", chr(i))
✅ 4. Alphabet Pattern Program
A
A B
A B C
A B C D
n = 4
for i in range(1, n+1):
for j in range(65, 65+i):
print(chr(j), end=' ')
print()
✅ 5. Star Pattern Program
*
**
***
****
n = 4
for i in range(1, n+1):
print('*' * i)