Python Lists
In this page:
list_name = [item1, item2, item3]
list_name[index]
List क्या है?
List items का एक ordered sequence store करता है जिसे बनाने के बाद बदला जा सकता है, और कई अन्य languages के arrays के विपरीत यह एक ही list में अलग-अलग types (numbers, strings, यहाँ तक कि दूसरी lists भी) को स्वतंत्र रूप से mix कर सकता है।
यह वह default general-purpose container है जिसे आप Python में सबसे पहले इस्तेमाल करते हैं।
उदाहरण: What is a List?
mixed = [1, "two", 3.0, [4]]
print(mixed)
List Items को Access करना
Square-bracket indexing पहले element के लिए 0 से शुरू होती है, और negative indices अंत से पीछे की ओर गिनती करते हैं ताकि my_list[-1] हमेशा list की length जाने बिना आखिरी item दे दे।
उदाहरण: Accessing List Items
items = [10, 20, 30]
print(items[0]) # first item, index 0
print(items[-1]) # last item, negative index counts from the end
List Slicing
list[start:end] से slicing करने पर original को बदले बिना एक sub-list निकल आती है, और चूँकि end index exclusive होता है, list[0:3] ठीक तीन elements देता है — यह एक ऐसी बात है जो inclusive ranges वाली languages से आने वाले beginners को confuse कर देती है।
उदाहरण: List Slicing
items = [10, 20, 30, 40, 50]
print(items[0:3])
List Items को Modify करना
चूँकि lists mutable होती हैं, आप my_list[i] = new_value दोबारा assign करके एक नया list object बनाए बिना किसी element को in place बदल सकते हैं, जो performance के लिए मायने रखता है और इसका मतलब यह भी है कि एक ही list को इशारा करने वाले दो variables दोनों में यह बदलाव दिखेगा।
उदाहरण: Modifying List Items
items = [10, 20, 30]
items[1] = 99 # reassigns the element at index 1 in place
print(items)
Lists पर Iterate करना
List पर for loop हर element को सीधे उसके क्रम में देता है (index नहीं), जो items को process करने का idiomatic तरीका है — enumerate() का इस्तेमाल तभी कीजिए जब आपको हर item की position भी चाहिए हो।
उदाहरण: Iterating over Lists
items = [10, 20, 30]
for item in items: # yields each value directly, not the index
print(item)
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: