🟢 Beginner  ·  Lesson 12

Python Lists

List क्या है?

List कई values को एक variable में, क्रम में, square brackets [ ] के अंदर रखती है। Lists mutable हैं — items जोड़, हटा और बदल सकते हैं।

Python – list.py
marks = [85, 92, 78, 90]
print(marks)
print(len(marks))   # कितने items
[85, 92, 78, 90] 4

Access और Slice

Python – access.py
fruits = ["apple", "banana", "mango"]
print(fruits[0])     # पहला
print(fruits[-1])    # आखिरी
print(fruits[0:2])   # पहले दो
apple mango ['apple', 'banana']

List Methods

Methodक्या करता है
.append(x)अंत में x जोड़ें
.insert(i,x)position i पर x जोड़ें
.remove(x)पहला x हटाएं
.pop()आखिरी हटाकर लौटाएं
.sort()जगह पर sort
.reverse()क्रम उल्टा
.count(x)कितने x

Program 1: बनाएं और Loop करें

Python – loop_list.py
names = ["Aman", "Riya", "Karan"]
for name in names:
    print("Student:", name)
Student: Aman Student: Riya Student: Karan

for loop हर item एक-एक करके लेता है — index की ज़रूरत नहीं।

Program 2: Sum, Max और Min

Python – stats.py
marks = [85, 92, 78, 90, 88]
print("Total  :", sum(marks))
print("Highest:", max(marks))
print("Lowest :", min(marks))
print("Average:", sum(marks) / len(marks))
Total : 433 Highest: 92 Lowest : 78 Average: 86.6

Built-in functions sum(), max(), min(), len() lists पर सीधे चलते हैं।

Program 3: List में Search

Python – search.py
numbers = [10, 25, 30, 45]
x = int(input("Search for: "))
if x in numbers:
    print(x, "found at position", numbers.index(x))
else:
    print(x, "not in list")
Search for: 30 30 found at position 2
  • x in numbers membership check करता है।
  • .index(x) item की position देता है।

Program 4: Sort और Duplicates हटाएं

Python – cleanup.py
data = [5, 2, 8, 2, 5, 1]
data = list(set(data))   # set duplicates हटाता है
data.sort()              # फिर sort
print(data)
[1, 2, 5, 8]

set(data) duplicates हटाता है; list() वापस list बनाता है; .sort() क्रम में लगाता है।

List Comprehension (झलक)

एक line में lists बनाने का छोटा तरीका:

Python – comp.py
squares = [x*x for x in range(1, 6)]
print(squares)
[1, 4, 9, 16, 25]

सामान्य गलतियाँ

  • Index out of range — 4-item list पर marks[10] error देता है।
  • .remove(x) error देता है अगर x मौजूद नहीं।
  • append (एक item) और extend (कई items) में confusion।

Practice Tasks

  1. 5 marks store करके total और average print करें।
  2. max() use किए बिना list में सबसे बड़ा number ढूंढें।
  3. List में even और odd numbers गिनें।
  4. List से duplicates हटाकर sort करें।

सारांश

  • Lists [ ] में क्रमबद्ध, बदलने योग्य items रखती हैं।
  • Methods: append, insert, remove, pop, sort, reverse।
  • sum/max/min/len सीधे चलते हैं; in membership check करता है।
← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.