Skip to main content

List in Python पायथन में लिस्ट, List operations लिस्ट ऑपरेशन, List functions and methods लिस्ट फंक्शनस एवं मेथड्स

In Python, a list is a mutable, ordered collection of elements. Lists are one of the most commonly used data structures in Python because they can store elements of various data types and allow for flexible operations such as adding, removing, and modifying elements. Lists are defined by enclosing elements in square brackets [], with elements separated by commas.

पायथन में, लिस्ट (सूची) तत्वों का एक परिवर्तनशील, क्रमबद्ध संग्रह है। पायथन में सूचियाँ सबसे अधिक इस्तेमाल की जाने वाली डेटा संरचनाओं में से एक हैं क्योंकि वे विभिन्न डेटा प्रकारों के तत्वों को संग्रहीत कर सकती हैं और तत्वों को जोड़ने, हटाने और संशोधित करने जैसे लचीले संचालन की अनुमति देती हैं। सूचियों को वर्गाकार कोष्ठकों [] में तत्वों को संलग्न करके परिभाषित किया जाता है, जिसमें तत्वों को अल्पविराम से अलग किया जाता है।
Syntax:-

# Creating a list लिस्ट तैयार करना 

my_list = [1, 2, 3, "apple", [4, 5]]

# Lists can be nested लिस्ट को नेस्ट किया जा सकता है 

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(matrix[1]) # Output: [4, 5, 6]
print(matrix[1][2]) # Output: 6

List operations लिस्ट ऑपरेशन:-

1. Concatenation जोड़ना:- We can concatenate two or more lists using the + operator.
हम + ऑपरेटर का उपयोग करके दो या अधिक सूचियों को जोड़ सकते हैं।

list1 = [1, 2, 3]
list2 = [4, 5, 6]
combined_list = list1 + list2
print(combined_list) # Output: [1, 2, 3, 4, 5, 6]

2. Repetition दोहराव:- We can repeat a list multiple times using the * operator.
हम * ऑपरेटर का उपयोग करके किसी सूची को कई बार दोहरा सकते हैं।
my_list = [1, 2, 3]
print(my_list * 3) # Output: [1, 2, 3, 1, 2, 3, 1, 2, 3]

3. Indexing अनुक्रमाणिका:- We can access elements in a list by their index, starting at 0.
हम किसी सूची में तत्वों तक उनके सूचकांक (0 से शुरू करके) के आधार पर पहुंच सकते हैं।

my_list = [10, 20, 30, 40]
print(my_list[0]) # Output: 10
print(my_list[2]) # Output: 30

4. Slicing स्लाइसिंग:- We can access a range of elements using slicing.
स्लाइसिंग का उपयोग करके हम अनेक तत्वों तक पहुँच सकते हैं।

my_list = [10, 20, 30, 40, 50]
print(my_list[1:4]) # Output: [20, 30, 40]

5. Membership Testing मेम्बरशिप की जांच:- We can check if an element exists in a list using the in keyword.
हम in कीवर्ड का उपयोग करके यह जांच सकते हैं कि कोई तत्व सूची में मौजूद है या नहीं।

my_list = [10, 20, 30]
print(20 in my_list) # Output: True
print(50 in my_list) # Output: False

6. Iteration पुनरावृत्ति:- We can iterate through the elements of a list using a for loop.
हम फॉर लूप का उपयोग करके सूची के तत्वों के माध्यम से पुनरावृत्ति कर सकते हैं।

my_list = [1, 2, 3]
for elem in my_list:
   print(elem)

# Output:-
1
2
3

7. List Comprehension लिस्ट कॉम्प्रिहेंशन:- List comprehensions provide a concise way to create lists.
सूची समझ सूचियाँ बनाने का एक संक्षिप्त तरीका प्रदान करती है।

squares = [x**2 for x in range(5)]
print(squares) # Output: [0, 1, 4, 9, 16]

List functions and methods लिस्ट फंक्शनस एवं मेथड्स :-

1. len():- Returns the number of elements in the list.
यह फंक्शन सूची में तत्वों की संख्या लौटाता है।

my_list = [1, 2, 3, 4]
print(len(my_list)) # Output: 4

2. max():- Returns the maximum element in the list (if the elements are comparable).
यह फंक्शन सूची में अधिकतम तत्व लौटाता है (यदि तत्व तुलनीय हैं)।

my_list = [1, 5, 3, 7]
print(max(my_list)) # Output: 7

3. min():- Returns the minimum element in the list (if the elements are comparable).
यह फंक्शन सूची में न्यूनतम तत्व लौटाता है (यदि तत्व तुलनीय हैं)।

my_list = [1, 5, 3, 7]
print(min(my_list)) # Output: 1

4. sum():- Returns the sum of all numeric elements in the list.
यह फंक्शन सूची में सभी संख्यात्मक तत्वों का योग लौटाता है।

my_list = [1, 2, 3, 4]
print(sum(my_list)) # Output: 10

5. sorted():- Returns a sorted copy of the list without modifying the original list.
यह फंक्शन मूल सूची को संशोधित किए बिना सूची की क्रमबद्ध प्रतिलिपि लौटाता है।

my_list = [3, 1, 4, 2]
sorted_list = sorted(my_list)
print(sorted_list) # Output: [1, 2, 3, 4]
print(my_list) # Output: [3, 1, 4, 2]

6. list():- Creates a list from an iterable (such as a string, tuple, or set).
यह फंक्शन किसी पुनरावृत्तीय (जैसे कि स्ट्रिंग, टपल या सेट) से सूची बनाता है।

my_string = "hello"
my_list = list(my_string)
print(my_list) # Output: ['h', 'e', 'l', 'l', 'o']

7. append():- Adds an element to the end of the list.
यह फंक्शन सूची के अंत में एक तत्व जोड़ता है

my_list = [1, 2, 3]
my_list.append(4)
print(my_list) # Output: [1, 2, 3, 4]

8. extend():- Extends the list by appending all elements from another list or iterable.
यह फंक्शन किसी अन्य सूची या पुनरावृत्तीय से सभी तत्वों को जोड़कर सूची का विस्तार करता है

my_list = [1, 2, 3]
my_list.extend([4, 5])
print(my_list) # Output: [1, 2, 3, 4, 5]

9. insert():- Inserts an element at a specific position.
यह फंक्शन किसी तत्व को किसी विशिष्ट स्थान पर सम्मिलित करता है।

my_list = [1, 2, 4]
my_list.insert(2, 3) # Insert 3 at index 2
print(my_list) # Output: [1, 2, 3, 4]

10. remove():- Removes the first occurrence of a specific value.
यह फंक्शन किसी विशिष्ट मान की पहली घटना को हटाता है।

my_list = [1, 2, 3, 4, 3]
my_list.remove(3)
print(my_list) # Output: [1, 2, 4, 3]

11. pop():- Removes and returns the element at a specific position (default is the last element).
यह फंक्शन तत्व को किसी विशिष्ट स्थान पर हटाता है और लौटाता है (डिफ़ॉल्ट अंतिम तत्व है)

my_list = [1, 2, 3]
last_element = my_list.pop()
print(last_element) # Output: 3
print(my_list) # Output: [1, 2]

12. clear():- Removes all elements from the list, leaving it empty.
यह फंक्शन सूची से सभी तत्वों को हटा देता है, तथा उसे रिक्त छोड़ देता है।

my_list = [1, 2, 3]
my_list.clear()
print(my_list) # Output: []

13. index():- Returns the index of the first occurrence of a specific value. Raises a ValueError if the value is not found.
यह फंक्शन किसी विशिष्ट मान की पहली घटना का सूचकांक लौटाता है। यदि मान नहीं मिलता है तो ValueError प्रदर्शित करता है।

my_list = [1, 2, 3, 4, 3]
print(my_list.index(3)) # Output: 2

14. count():- Returns the number of times a specified value appears in the list.
यह फंक्शन सूची में निर्दिष्ट मान के प्रकट होने की संख्या लौटाता है

my_list = [1, 2, 3, 3, 4]
print(my_list.count(3)) # Output: 2

15. sort():- Sorts the list in ascending order (modifies the original list). 
यह फंक्शन सूची को आरोही क्रम में सॉर्ट करता है (मूल सूची को संशोधित करता है)। 

my_list = [3, 1, 4, 2]
my_list.sort()
print(my_list) # Output: [1, 2, 3, 4]

To sort in descending order, use reverse=True. अवरोही क्रम में सॉर्ट करने के लिए, रिवर्स=ट्रू का उपयोग करें।

my_list.sort(reverse=True)
print(my_list) # Output: [4, 3, 2, 1]

16. reverse():- Reverses the order of the elements in the list.
यह फंक्शन सूची में तत्वों का क्रम उलट देता है

my_list = [1, 2, 3, 4]
my_list.reverse()
print(my_list) # Output: [4, 3, 2, 1]

17. copy():- Creates a shallow copy of the list.
यह फंक्शन सूची की एक शाल्लो प्रतिलिपि बनाता है

my_list = [1, 2, 3]
copy_list = my_list.copy()
print(copy_list) # Output: [1, 2, 3]

Comments

Popular posts from this blog

Python IDLE पायथन आईडीएलई (इंटीग्रेटेड डेवलपमेंट एंड लर्निंग एनवायरनमेंट)

IDLE (Integrated Development and Learning Environment) is a graphical user interface (GUI) for the Python programming language. It includes the Python standard library and provides a convenient environment for writing, running, and debugging Python code. IDLE (इंटीग्रेटेड डेवलपमेंट एंड लर्निंग एनवायरनमेंट) पायथन प्रोग्रामिंग भाषा के लिए एक ग्राफिकल यूजर इंटरफेस (GUI) है। इसमें पायथन मानक लाइब्रेरी शामिल है और पायथन कोड लिखने, चलाने और डीबग करने के लिए एक सुविधाजनक वातावरण प्रदान करता है।  Key features of IDLE are:- आईडीएलई की मुख्य विशेषताएं हैं:-  1. Interactive Shell:- IDLE provides an interactive Python shell, allowing you to execute Python commands and see the immediate results. 1. इंटरएक्टिव शेल:- आईडीएलई एक इंटरैक्टिव पायथन शेल प्रदान करता है, जो आपको पायथन कमांड निष्पादित करने और तत्काल परिणाम देखने की अनुमति देता है।  2. Script Editor:- It includes a text editor for writing Python scripts. You can create, edit, and save Python files with a ".py" extension. 2. स्क्...

Operator's in Python पायथन में ऑपरेटर्स

Python supports various types of operators for performing operations on variables and values. Here are some common types of operators in Python: पायथन वेरिएबल्स और मानों पर संचालन करने के लिए विभिन्न प्रकार के ऑपरेटरों का समर्थन करता है। यहां पायथन में कुछ सामान्य प्रकार के ऑपरेटर हैं: 1. Arithmetic Operators:    + # Addition     - # Subtraction    * # Multiplication    / # Division    % # Modulus (remainder)    ** # Exponentiation    // # Floor Division 2. Comparison Operators:        == # Equal to    != # Not equal to    < # Less than    > # Greater than    <= # Less than or equal to    >= # Greater than or equal to     3. Logical Operators:       and # Logical AND    or # Logical OR    not # Logical NOT    4. Assignment Operators:    ...

Programming with Python Language Tutorial available in hindi and english पाइथन प्रोग्रामिंग टुटोरिअल हिंदी एवं अंग्रेजी भाषा में

 Unit -01 1. Introduction to python (पाइथन भाषा का परिचय) 2. History of Python Programming Language पाइथन प्रोग्रामिंग लैंग्वेज का इतिहास 3. Python Programming Language’s Features and Advantages पायथन प्रोग्रामिंग लैंग्वेज की प्रमुख विशेषताएं 4. Python’s Programming Language's Popularity पाइथन प्रोग्रामिंग लैंग्वेज की लोकप्रियता 5.The Future of Python Programming Language पायथन प्रोग्रामिंग लैंग्वेज का भविष्य 6. The Python Interpreter पायथन इंटरप्रेटर 7. The Python IDLE पाइथन आईडीएलई 8. Installation process of Python IDLE पायथन आईडीएलई की इंस्टालेशन प्रक्रिया 9. Dynamically typed and strongly typed features of python गतिशील रूप से टाइप और दृढ़ता से टाइप की गई भाषा पायथन 10. Basic Data Types of Python पायथन के बेसिक डेटा टाइप 11. Variables in python पायथन में वेरिएबल 12. Expressions in Python पायथन में अभिव्यक्तियां 13. Statements in Python पायथन में कथन 14. Operator's in Python पायथन में ऑपरेटर्स 15. Flow of execution of a python program पाइथन प्रोग्राम के निष्पादन का प्रवाह 16...