🐍 Unit 2 – Python Programming · Lesson 08
Python में Lists
List क्या है?
List एक नाम के अंदर कई values क्रम से square brackets में रखती है। यह mutable है — items जोड़े, हटाए या बदले जा सकते हैं। Lists datasets के साथ काम का पहला कदम हैं: marks का column, हफ़्ते का temperature, shopping cart।
Items access और बदलना
Python
cart = ['Milk', 'Bread', 'Eggs', 'Butter'] prices = [56, 40, 84, 60] print(cart[0], cart[-1], prices[1:3]) cart[1] = 'Brown Bread' # change an item print(cart) print(len(cart), 'Eggs' in cart)
Milk Butter [40, 84]
['Milk', 'Brown Bread', 'Eggs', 'Butter']
4 True
List operations
Python
week1 = [12, 15, 9] week2 = [20, 18] print(week1 + week2) # concatenation print(['-'] * 5) # repetition print(week1 * 2) all_sales = week1 + week2 print(all_sales[::-1], all_sales[::2])
[12, 15, 9, 20, 18]
['-', '-', '-', '-', '-']
[12, 15, 9, 12, 15, 9]
[18, 20, 9, 15, 12] [12, 9, 18]
| Operation | Operator | Example |
|---|---|---|
| Concatenation | + | [1, 2] + [3] → [1, 2, 3] |
| Repetition | * | [0] * 3 → [0, 0, 0] |
| Membership | in, not in | 'Eggs' in cart → True |
| Slicing | [start:stop:step] | L[1:3], L[::-1] |
List traverse करना
Python
cart = ['Milk', 'Bread', 'Eggs', 'Butter']
prices = [56, 40, 84, 60]
total = 0
for i in range(len(cart)):
print(f'{i + 1}. {cart[i]:<7} Rs.{prices[i]}')
total += prices[i]
print('Total: Rs.', total)1. Milk Rs.56
2. Bread Rs.40
3. Eggs Rs.84
4. Butter Rs.60
Total: Rs. 240
List methods
Python
scores = [72, 88, 65] scores.append(91) ; print(scores) scores.insert(0, 59) ; print(scores) scores.extend([77, 80]) ; print(scores) scores.remove(65) ; print(scores) last = scores.pop() ; print(last, scores) print(scores.index(91), scores.count(88)) scores.sort() ; print(scores) scores.reverse() ; print(scores)
[72, 88, 65, 91]
[59, 72, 88, 65, 91]
[59, 72, 88, 65, 91, 77, 80]
[59, 72, 88, 91, 77, 80]
80 [59, 72, 88, 91, 77]
3 1
[59, 72, 77, 88, 91]
[91, 88, 77, 72, 59]
➕
जोड़नाappend(x), insert(i, x), extend(list)➖
हटानाremove(x), pop(), pop(i), clear(), del L[i]🔍
ढूँढनाindex(x), count(x)↕️
क्रमsort(), sort(reverse=True), reverse()Lists पर built-in functions
Python
rain = [12.5, 0, 33.2, 8.4, 0, 51.0, 19.3]
print('Total:', sum(rain), '| Max:', max(rain), '| Min:', min(rain))
print('Average:', round(sum(rain) / len(rain), 2))
print('Sorted:', sorted(rain))
print('Dry days:', rain.count(0))Total: 124.4 | Max: 51.0 | Min: 0
Average: 17.77
Sorted: [0, 0, 8.4, 12.5, 19.3, 33.2, 51.0]
Dry days: 2
Data जैसे list programs
1. Pass और fail अलग करना
Python
marks = [45, 78, 32, 91, 66, 29, 84]
passed, failed = [], []
for m in marks:
if m >= 33:
passed.append(m)
else:
failed.append(m)
print('Passed:', passed)
print('Failed:', failed)
print('Pass %:', round(len(passed) / len(marks) * 100, 1))Passed: [45, 78, 91, 66, 84]
Failed: [32, 29]
Pass %: 71.4
2. List input करके search
Python
n = int(input('How many items? '))
items = []
for i in range(n):
items.append(input('Item: '))
key = input('Search: ')
if key in items:
print(key, 'found at position', items.index(key) + 1)
else:
print(key, 'not found')How many items? 3
Item: Pen
Item: Ruler
Item: Eraser
Search: Ruler
Ruler found at position 2
3. List comprehension से convert और filter
Python
temps_c = [30, 25, 37, 41] temps_f = [c * 9 / 5 + 32 for c in temps_c] print(temps_f) high = [c for c in temps_c if c > 35] print(high)
[86.0, 77.0, 98.6, 105.8]
[37, 41]
सारांश और practice
- List = [ ] में ordered, mutable collection।
- Index 0 से (या अंत से −1); [a:b:c] से slice।
- + जोड़ता, * दोहराता, in membership check करता है।
- Methods: append, insert, extend, remove, pop, index, count, sort, reverse।
- Functions: len, max, min, sum, sorted।
✅ Practice
1) एक batsman के 6 matches के runs store करके total, average, best और half-centuries (≥ 50) की संख्या निकालें। 2) Prices की list से 10% discount वाली नई list बनाएँ। 3) Output: L = [3, 1, 2]; L.append(L.pop(0)); print(L)।
Answer 3: [1, 2, 3]
अक्सर पूछे जाने वाले प्रश्न (FAQ)
क्या list में अलग-अलग types की values हो सकती हैं?
हाँ। ["Riya", 16, 92.5, True] जैसी list valid है, हालाँकि data analysis में आमतौर पर एक list में एक ही type रखते हैं।
pop() क्या return करता है?
pop() आख़िरी item हटाकर return करता है; pop(i) index i वाला item हटाकर return करता है।
sort() और sorted() में क्या फ़र्क है?
list.sort() original list बदलता है और None return करता है; sorted(list) नई sorted list देता है और original वैसी ही रहती है।
