Python में कई बार हमें एक से अधिक values को एक साथ store करना होता है।
ऐसी स्थिति में हम अलग-अलग variables बनाने के बजाय List का उपयोग करते हैं।
List Python का एक बहुत important data structure है, जिसमें हम multiple items को एक ही variable में store कर सकते हैं।
यह flexible होता है और इसमें अलग-अलग प्रकार के data भी रखे जा सकते हैं।
List क्या होती है
List items का एक ordered collection होता है, जिसे square brackets [ ] के अंदर लिखा जाता है।
List के elements को comma , से अलग किया जाता है।
Example
fruits = ["Apple", "Banana", "Mango"]
print(fruits)
Output
['Apple', 'Banana', 'Mango']
List की विशेषताएँ
- Ordered होती है (items का order fixed रहता है)
- Mutable होती है (change की जा सकती है)
- Duplicate values allow करती है
- Different data types store कर सकती है
List Indexing
List के हर element का एक index होता है, जो 0 से शुरू होता है।
numbers = [10, 20, 30, 40]
print(numbers[0])
print(numbers[2])
Output
10
30
Negative Indexing
numbers = [10, 20, 30, 40]
print(numbers[-1])
Output
40
Explanation-1 last element को represent करता है।
List Slicing
List का एक हिस्सा निकालने के लिए slicing का उपयोग किया जाता है।
numbers = [10, 20, 30, 40, 50]
print(numbers[1:4])
Output
[20, 30, 40]
List को modify करना
List mutable होती है, इसलिए हम इसके elements को change कर सकते हैं।
numbers = [10, 20, 30]
numbers[1] = 25
print(numbers)
Output
[10, 25, 30]
List Methods
1. append() → element add करना
numbers = [1, 2, 3]
numbers.append(4)
print(numbers)
Output
[1, 2, 3, 4]
2. insert() → specific position पर add करना
numbers = [1, 2, 4]
numbers.insert(2, 3)
print(numbers)
Output
[1, 2, 3, 4]
3. remove() → element delete करना
numbers = [1, 2, 3, 4]
numbers.remove(3)
print(numbers)
Output
[1, 2, 4]
4. pop() → last element हटाना
numbers = [1, 2, 3]
numbers.pop()
print(numbers)
Output
[1, 2]
List पर Loop चलाना
fruits = ["Apple", "Banana", "Mango"]
for item in fruits:
print(item)
Output
Apple
Banana
Mango
Real-life Example: Student marks
marks = [80, 75, 90, 85]
total = sum(marks)
average = total / len(marks)
print("Total =", total)
print("Average =", average)
Real-life Example: Shopping list
items = ["Milk", "Bread"]
items.append("Eggs")
print(items)
Explanation
यह example daily life shopping list को manage करने जैसा है।
List में different data types
data = [10, "Python", 5.5]
print(data)
Nested List
List के अंदर दूसरी list भी हो सकती है।
matrix = [[1, 2], [3, 4]]
print(matrix[0][1])
Output
2
Common mistakes
- indexing गलत करना
- append और insert में confusion
- list को immutable समझ लेना (यह mutable होती है)
Simple complete program
numbers = []
for i in range(1, 6):
numbers.append(i)
print(numbers)
Output
[1, 2, 3, 4, 5]
इस topic की मुख्य बात
List Python का एक powerful data structure है, जो multiple values को store और manage करने के लिए उपयोग किया जाता है।
इस topic में student यह सीखता है:
- list क्या होती है
- indexing और slicing
- list methods
- loop के साथ use
- real-life applications