🔹 List (सूची)
📌 Definition: List ek aisi collection hai jo ordered (क्रमबद्ध) aur mutable (बदलने योग्य) होती है. Ismein duplicate values allowed hain.
my_list =
✅ Accessing elements:
print(my_list) # 1
print(my_list) # XYZ (last element)
✏️ Update (element बदलना):
my_list = 5
print(my_list) #
➕ Append (अंत में जोड़ना):
my_list.append('ABC')
print(my_list) #
🧩 Insert (बीच में जोड़ना):
my_list.insert(0, 'New')
print(my_list) #
❌ Delete by Index:
del my_list
print(my_list) #
❌ Remove by Value:
my_list.remove(2)
print(my_list) #
📤 Pop (निकालना और value return करना):
fruits =
print(fruits.pop()) # cherry
print(fruits) #
print(fruits.pop(0)) # apple
print(fruits) #
🔢 Sorting:
nums =
nums.sort()
print(nums) #
print(sorted(nums, reverse=True)) #
🧬 Slicing (Part of list):
players =
print(players) #
🧪 Copying list (deep copy vs shallow):
my_foods =
friend_foods = my_foods # Same reference
my_foods.append('cake')
print(friend_foods) #
# Correct way to copy
friend_foods = my_foods
squared =
print(squared) # Output:
🔸 Tuple (अपरिवर्तनीय सूची)
📌 Tuple ek aisi list hai jo immutable(No CUD) होती है.
my_tuple = (10, 20, 30)
single_element = (5,) # Tuple with one element (comma required)
🔸 Set (सेट)
📌 Set ek unordered, unchangeable (element को update नहीं कर सकते), unindexed collection hoti hai. Duplicate elements allowed नहीं हैं.
unindexed hota hai isliye isse update nahi kar sakte hai , add and remove kar sakte hai ,
my_set = {"apple", "banana", "apple", "cherry"}
print(my_set) # {'banana', 'apple', 'cherry'}
Bool
Trueaur int1ko same maana jata hai:
s = {"apple", True, 1, 2}
print(s) # {'apple', True, 2}
my_set = {1, 2, 3, 4}
my_set.add(5)
# {1, 2, 3, 4, 5}
my_set.remove(2)
# {1, 3, 4, 5}
my_set.discard(10) # No error if element not found
my_set.pop()
# Removes random element
🔸 Dictionary (शब्दकोश)
📌 Dictionary key-value pairs का collection है. Ye ordered (Python 3.7+), mutable, aur duplicate keys allow नहीं karta.
person = {
"name": "John",
"age": 30,
"occupation": "Engineer"
}
✅ Accessing values:
print(person) # John
print(person.get("age")) # 30
print(person.get("salary", "N/A")) # N/A
🔧 Methods:
print(person.keys()) # dict_keys()
print(person.values()) # dict_values()
print(person.items()) # dict_items()
🔁 Loop through dictionary:
for key, value in person.items():
print(f"{key} => {value}")
🔚 Summary in One Line:
| Type | Ordered | Mutable | Duplicates | Indexing | Syntax |
|---|---|---|---|---|---|
| List | ✅ | ✅ | ✅ | ✅ | |
| Tuple | ✅ | ❌ | ✅ | ✅ | ( ) |
| Set | ❌ | ❌ | ❌ | ❌ | { } |
| Dictionary | ✅ | ✅ | ✅ (values) | ✅ (keys) | {key:val} |