Python Lists Explained, Your Guide in English & Hindi

A Deep Dive into Python Lists

Comprehensive Notes from Chapter 7: List Manipulation

1. What is a List?

A list is one of the most fundamental data structures in Python. Think of it as a flexible container that holds an ordered sequence of items. These items can be of any type—numbers, strings, or even other lists!

Key Properties:
  • Ordered: The items have a defined order, and that order will not change.
  • Mutable: You can change, add, and remove items in a list after it has been created.
  • Allows Duplicates: A list can contain items with the same value.

2. Creating Lists

Creating a list is simple. You just enclose a comma-separated sequence of items in square brackets `[]`.

# An empty list
empty_list = []
# A list of integers
numbers = [1, 2, 3, 4, 5]
# A list of strings
fruits = ["apple", "banana", "cherry"]
# A list with mixed data types
mixed_list = ["hello", 3.14, True, 42]
# A nested list (a list inside a list)
matrix = [[1, 2], [3, 4]]

3. Accessing and Slicing

You can access individual items using their index (position), which starts at 0. You can also access a range of items, which is called slicing.

my_list = ['p', 'r', 'o', 'g', 'r', 'a', 'm']
# Accessing the first item (index 0)
print(my_list[0])  # Output: 'p'
# Accessing the last item (negative indexing)
print(my_list[-1]) # Output: 'm'
# Slicing: Get items from index 2 up to (but not including) index 5
print(my_list[2:5]) # Output: ['o', 'g', 'r']
# Slicing from the start to index 3
print(my_list[:4]) # Output: ['p', 'r', 'o', 'g']

4. Common List Methods

Python provides many built-in methods to modify and work with lists. Here are the most essential ones:

MethodDescription
append(item)Adds a single item to the end of the list.
extend(iterable)Adds all items from another list (or any iterable) to the end.
insert(i, item)Inserts an item at a specific index `i`.
remove(item)Removes the first occurrence of the specified item.
pop(i)Removes and returns the item at index `i`. If `i` is not given, it removes the last item.
sort()Sorts the list in ascending order (in-place).
reverse()Reverses the order of items in the list (in-place).
len(list)(This is a function, not a method) Returns the number of items in the list.

Example in Action:

numbers = [30, 10, 40]
print("Original:", numbers)
# Add an item to the end
numbers.append(50)
print("After append(50):", numbers)
# Sort the list
numbers.sort()
print("After sort():", numbers)
# Remove an item
numbers.remove(30)
print("After remove(30):", numbers)
# Get length
print("Length:", len(numbers))

1. लिस्ट क्या है?

लिस्ट पाइथन के सबसे मौलिक डेटा स्ट्रक्चर्स में से एक है। इसे एक लचीले कंटेनर के रूप में सोचें जिसमें आइटम्स का एक क्रमबद्ध (ordered) अनुक्रम होता है। ये आइटम्स किसी भी प्रकार के हो सकते हैं—संख्याएं, स्ट्रिंग्स, या अन्य लिस्ट्स भी!

मुख्य गुण:
  • क्रमबद्ध (Ordered): आइटम्स का एक निश्चित क्रम होता है, और वह क्रम नहीं बदलेगा।
  • परिवर्तनीय (Mutable): आप लिस्ट बनाने के बाद उसमें आइटम्स को बदल सकते हैं, जोड़ सकते हैं, और हटा सकते हैं।
  • डुप्लिकेट्स की अनुमति (Allows Duplicates): एक लिस्ट में एक ही मान वाले आइटम्स हो सकते हैं।

2. लिस्ट बनाना

लिस्ट बनाना बहुत सरल है। आपको बस आइटम्स के एक कॉमा-सेपरेटेड अनुक्रम को स्क्वायर ब्रैकेट `[]` में संलग्न करना है।

# एक खाली लिस्ट
empty_list = []
# पूर्णांकों की एक लिस्ट
numbers = [1, 2, 3, 4, 5]
# स्ट्रिंग्स की एक लिस्ट
fruits = ["apple", "banana", "cherry"]
# मिश्रित डेटा प्रकारों वाली एक लिस्ट
mixed_list = ["hello", 3.14, True, 42]
# एक नेस्टेड लिस्ट (एक लिस्ट के अंदर दूसरी लिस्ट)
matrix = [[1, 2], [3, 4]]

3. आइटम्स तक पहुँचना और स्लाइसिंग

आप अलग-अलग आइटम्स को उनके इंडेक्स (स्थिति) का उपयोग करके एक्सेस कर सकते हैं, जो 0 से शुरू होता है। आप आइटम्स की एक रेंज भी एक्सेस कर सकते हैं, जिसे स्लाइसिंग कहा जाता है।

my_list = ['p', 'r', 'o', 'g', 'r', 'a', 'm']
# पहले आइटम तक पहुँचना (इंडेक्स 0)
print(my_list[0])  # आउटपुट: 'p'
# अंतिम आइटम तक पहुँचना (नकारात्मक इंडेक्सिंग)
print(my_list[-1]) # आउटपुट: 'm'
# स्लाइसिंग: इंडेक्स 2 से इंडेक्स 5 तक (लेकिन 5 शामिल नहीं) आइटम्स प्राप्त करें
print(my_list[2:5]) # आउटपुट: ['o', 'g', 'r']
# शुरुआत से इंडेक्स 3 तक स्लाइसिंग
print(my_list[:4]) # आउटपुट: ['p', 'r', 'o', 'g']

4. सामान्य लिस्ट मेथड्स

पाइथन लिस्ट्स को संशोधित करने और उनके साथ काम करने के लिए कई अंतर्निहित (built-in) मेथड्स प्रदान करता है। यहाँ सबसे आवश्यक मेथड्स दिए गए हैं:

मेथडविवरण
append(item)लिस्ट के अंत में एक आइटम जोड़ता है।
extend(iterable)किसी अन्य लिस्ट (या किसी भी आईटरेबल) से सभी आइटम्स को अंत में जोड़ता है।
insert(i, item)एक विशिष्ट इंडेक्स `i` पर एक आइटम सम्मिलित करता है।
remove(item)निर्दिष्ट आइटम की पहली घटना को हटाता है।
pop(i)इंडेक्स `i` पर आइटम को हटाता है और लौटाता है। यदि `i` नहीं दिया गया है, तो यह अंतिम आइटम को हटाता है।
sort()लिस्ट को आरोही क्रम में सॉर्ट करता है (इन-प्लेस)।
reverse()लिस्ट में आइटम्स के क्रम को उलट देता है (इन-प्लेस)।
len(list)(यह एक फंक्शन है, मेथड नहीं) लिस्ट में आइटम्स की संख्या लौटाता है।

उदाहरण:

numbers = [30, 10, 40]
print("मूल:", numbers)
# अंत में एक आइटम जोड़ें
numbers.append(50)
print("append(50) के बाद:", numbers)
# लिस्ट को सॉर्ट करें
numbers.sort()
print("sort() के बाद:", numbers)
# एक आइटम हटाएं
numbers.remove(30)
print("remove(30) के बाद:", numbers)
# लंबाई प्राप्त करें
print("लंबाई:", len(numbers))

Comments

Popular posts from this blog

O-Level M1 + M2 Test Paper | NIELIT Practice

Introduction to Python Programming in IDLE: Beginners Guide | Python परिचय

IoT Part 3: How Smart Devices Talk (Communication Models)