Indexing and Slicing in Python

Indexing and Slicing in Python

Python allows you to retrieve individual members of a sequence by specifying its index of that member. It is the integer that uniquely identifies that member’s position in the sequence. Python uses a zero-based indexing system, meaning that the first element in a sequence is located at position 0. It also permits the use of negative integers to count from the end of the sequence.

पायथन आपको उस सदस्य के अपने सूचकांक को निर्दिष्ट करके अनुक्रम के व्यक्तिगत सदस्यों को पुनः प्राप्त करने की अनुमति देता है। यह पूर्णांक है जो विशिष्ट रूप से अनुक्रम में उस सदस्य की स्थिति की पहचान करता है। पायथन एक शून्य-आधारित अनुक्रमण प्रणाली का उपयोग करता है, जिसका अर्थ है कि अनुक्रम में पहला तत्व स्थिति 0 पर स्थित है। यह अनुक्रम के अंत से गणना करने के लिए नकारात्मक पूर्णांकों के उपयोग की भी अनुमति देता है।

Tuple Indexing: टपल इंडेक्सिंग

Tuples are just like lists, but you can’t change their values. The values that you give it first up, are the values that you are stuck with for the rest of the program. Again, each value is numbered starting from zero, for easy reference.

टपल केवल लिस्ट की तरह हैं, लेकिन आप उनके वैल्यूज को नहीं बदल सकते। वैल्यू जो आप इसे पहले देते हैं, वे वैल्यू हैं जो आप बाकी प्रोग्राम के लिए चिपका रहता है। फिर, प्रत्येक वैल्यू को शून्य से शुरू किया जाता है, आसान रिफरेन्स के लिए।

Example: the names of the months of the year.

वर्ष के महीनों के नाम।

There are various ways in which we can access the elements of a tuple.

ऐसे कई तरीके हैं जिनसे हम एक टपल के एलीमेंट तक पहुंच सकते हैं।

Positive Indexing: We can use the index operator [] to access an item in a tuple where the index starts from 0. So, a tuple having 6 elements will have indices from 0 to 5. Trying to access an element outside of tuple (for example, 6, 7, …) will raise an IndexError. The index must be an integer; so we cannot use float or other types. This will result in TypeError. Likewise, nested tuples are accessed using nested indexing, as shown in the example below.

पॉजिटिव इंडेक्सिंग: हम टपल में एक आइटम को एक्सेस करने के लिए इंडेक्स ऑपरेटर[] का उपयोग कर सकते हैं, जहां इंडेक्स 0 से शुरू होता है। इसलिए, 6 एलीमेंट वाले एक टपल में 0 से 5 तक इंडेक्स होंगे। टपल के बाहर एक एलीमेंट को एक्सेस करने के लिए प्रयास करना (उदाहरण के लिए 6, 7…..) एक इंडेक्स एरर रेज करेगा। इंडेक्स एक इंटीजर होना चाहिए, इसलिए हम फ्लोट या अन्य प्रकार का उपयोग नहीं कर सकते। इसका परिणाम टाईप एरर में होगा। इसी तरह, नेस्टेड टपलस को नेस्टेड इंडेक्सिंग का उपयोग करके एक्सेस किया जाता है, जैसा कि नीचे दिए गए उदाहरण में दिखाया गया है।

Example:

my_tuple = ( ‘p’, ‘y’, ‘t’, ‘h’, ‘0’, ‘n’)
print(“my tuple : “, my_tuple)
print (my_tuple[0], my_tuple[1], my_tuple[2], my_tuple[3], my_tuple[4], my_tuple[5])
# IndexError: list index out of range
# print (my_tuple[6])
# Index must be an integer
# TypeError: list indices must be integers, not float
# my_tuple(2.0)
# nested tuple
NestedTuple = (“Python”, [8, 10, 6], (1, 2, 3))
print (“NestedTuple : “, NestedTuple)
# nested index
print (“NestedTuple [0] [3] : “, NestedTuple[0][3])
print (“NestedTuple [1] [1] : “, NestedTuple [1] [1])

Output:
my tuple :  (‘p’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’)
p y t h o n
NestedTuple :  (‘Python’, [8, 10, 6], (1, 2, 3))
NestedTuple [0] [3] :  h
NestedTuple [1] [1] :  10

Negative Indexing: Python allows negative indexing for its sequences. The index of -1 refers to the last item, -2 to the second last item and so on.

निगेटिव इंडेक्सः पायथन अपने सिक्वेन्स के लिए निगेटिव इंडेक्सिंग को एलाउ करता है। -1 का इंडेक्स अंतिम आइटम को संदर्भित करता है, -2 अंतिम आईटम को, और इसी तरह अन्य।.

Example:

my_tuple = (‘p’, ‘y’,’t’,’h’,’o’,’n’)
print (“my tuple : “, my_tuple)
print (“my tuple [-6]) : “, my_tuple [-6])
print (“my tuple [-5]) : “, my_tuple [-5])
print (“my_tuple(-4]) : “, my_tuple [-4])
print (“my_tuple[-3]) : “, my_tuple[-3])
print (“my tuple [-2]): “, my_tuple[-2])
print (“my_tuple[-1]) : “, my_tuple[-1])

Output:
my tuple :  (‘p’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’)
my tuple [-6]) :  p
my tuple [-5]) :  y
my_tuple(-4]) :  t
my_tuple[-3]) :  h
my tuple [-2]):  o
my_tuple[-1]) :  n

Dictionaries indexing:
डिक्शनरीज इंडेक्सिंग

Dictionaries are similar to what their name suggests – a dictionary. In a dictionary, you have an ‘index’ of words and for each of them a definition. In python, the word is called a ‘key’, and the definition a ‘value’. The values in a dictionary aren’t numbered – they aren’t in any specific order, either – the key does the same thing. You can add, remove, and modify the values in dictionaries.

डिक्शनरीज जैसा कि उनके नाम से पता चलता है- एक डिक्शनरी। डिक्शनरी में, आपके पास शब्दों का एक ‘इंडेक्स’ है और उनमें से प्रत्येक के लिए एक परिभाषा है। पायथन में, वर्ड को एक ‘की’ कहा जाता है, और परिभाषा को एक ‘वैल्यू’। किसी डिक्शनरी की वैल्यू एक नम्बर नहीं हो सकता है- वे किसी भी विशिष्ट क्रम में नहीं हैं, या तो – की एक ही काम करती है। आप डिक्शनरी में वैल्यू को जोड़ सकते हैं, रिमूव कर सकते हैं, और मॉडिफॉई कर सकते हैं।

Example: telephone book.

    टेलीफोन बुक

In Python, we can access data from dictionaries using keys.

पायथन में, हम कीज का उपयोग करके डिक्शनरी से डेटा तक पहुँच सकते हैं।

For example 1:

d = {‘Aarya’:’1798798982′,’priya’: ‘6527356328’,’karan’:’4564326784′}
print(‘Aarya :’, d[‘Aarya’])
print(‘priya :’, d[‘priya’])
print(‘karan :’, d[‘karan’])

Output:
Aarya : 1798798982
priya : 6527356328
karan : 4564326784
If the specified key is not available then we will get KeyError message

यदि निर्दिष्ट की उपलब्ध नहीं है तो, हमें की-एरर मैसेज मिलेगा।

Example 2: Write a Python program to access dictionary key’s element by index.

इंडेक्स द्वारा डिक्शनरी की के एलीमेंट तक पहुंचने के लिए एक पायथन प्रोग्राम लिखें।

num = { ‘a’ : 80, ‘b’ : 90, ‘c’ : 70}
print (list (num) [0])
print (list (num) [1])
print (list (num) [2])

Output:
a
b
c

Concatenation – जोड़ना

The word concatenate is just another way of saying “to combine” or “to join together”. It is a term that describes combining of a string, text, or other data in a series without any gaps. In programming languages, an operator is used to denote concatenation. In python, you can use the traditional ‘+’ operator in order to merge two strings into a single object.

शब्द कॉनकैटनेट शब्द “कंबाइन करने के लिए” या “एक साथ जुड़ने के लिए” कहने का एक और तरीका है। यह एक शब्द है जो किसी भी अंतराल के बिना एक श्रृंखला में एक स्ट्रिंग, टेक्स्ट या अन्य डेटा के संयोजन का वर्णन करता है। प्रोग्रामिंग भाषाओं में, एक ऑपरेटर का उपयोग संगति को दर्शाने के लिए किया जाता है। पायथन में, आप पारंपरिक ‘+’ ऑपरेटर का उपयोग कर सकते हैं ताकि एक ही वस्तु में दो स्ट्रिंग को मर्ज किया जा सके।

For example:

>>>x=”Gyan”
>>>y=”cs”
>>> print(x+y)
Gyancs

Here, the concatenation of “Gyan” and “cs” is “Gyancs”, so that the new string that is created is referred to as a string object.

यहां, “ज्ञान ” और “सीएस” का संयोजन “ज्ञानसीएस” है, ताकि जो नया स्ट्रिंग बनाया जाता है उसे स्टिंग ऑब्जेक्ट के रूप में संदर्भित किया जाता है।

Note: Python cannot concatenate a string and integer. These are considered two separate types of objects. So, if you want to merge the two, you will need to convert the integer to a string.

नोट: पायथन एक स्ट्रिंग और पूर्णांक को समाप्त नहीं कर सकता है। इन्हें दो अलग-अलग प्रकार की ऑब्जेक्ट माना जाता है। इसलिए, यदि आप दोनों को मर्ज करना चाहते हैं, तो आपको पूर्णांक को एक स्ट्रिंग में बदलना होगा।

Tuple concatenation: टपल जोड़ना

We can use + operator to combine two tuples. This is also called concatenation. We can also repeat the elements in a tuple for a given number of times using the * operator. Both + and * operations result in a new tuple.

हम दो टपल्स को मिलाने के लिए + ऑपरेटर का उपयोग कर सकते हैं। इसे संयोजन भी कहा जाता है। हम एक ऑपरेटर में एलीमेंट को बार-बार * ऑपरेटर के उपयोग से दोहरा सकते हैं। दोनों + और * के ऑपरेशन के परिणामस्वरूप एक नया टपल बनता है।

Example:

t1 = (1,2,3,4,5)
t2 = (6,7,8,9,10)
con = t1 + t2
print (“t1 = “, t1)
print(“t2 = “, t2)
print (“Concatenation of t1 and t2 = “, con)
print (“Concatenation t1 = “, t1 * 3)
print (“Concatenation t1 = “, t2 * 3)

Output:
t1 =  (1, 2, 3, 4, 5)
t2 =  (6, 7, 8, 9, 10)
Concatenation of t1 and t2 =  (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
Concatenation t1 =  (1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5)
Concatenation t1 =  (6, 7, 8, 9, 10, 6, 7, 8, 9, 10, 6, 7, 8, 9, 10)

Dictionaries concatenation – डिक्शनरी जोड़ना

Using the method update(): By using the method update() in Python, one list can be merged into another. But in this, the second list is merged into the first list and no new list is created. It returns None.

मेथड अपडेट() का उपयोग करनाः पायथन में मेथड अपडेट) का उपयोग करके, एक लिस्ट को दूसरी में विलय किया जा सकता है। लेकिन इसमें दूसरी लिस्ट को पहली लिस्ट में मिला दिया जाता है और कोई नई लिस्ट नहीं बनाई जाती है। यह कुछ रिटर्न नहीं करता है।

Example:

dict1 = { ‘a’: 10, ‘b’: 8}
dict2 = { ‘c’: 6, ‘d’: 4}
print(“dicti :”, dict1)
print(“dict2 :”, dict2)
dict2.update (dict1)
print (“dict1:”, dict1)
print (“dict2:”, dict2)

Output:
dicti : {‘a’: 10, ‘b’: 8}
dict2 : {‘c’: 6, ‘d’: 4}
dict1: {‘a’: 10, ‘b’: 8}
dict2: {‘c’: 6, ‘d’: 4, ‘a’: 10, ‘b’: 8}

Using ** in Python: This is generally considered a trick in Python where a single expression is used to merge two dictionaries and stored in a third dictionary. The single expression is **. This does not affect the other two dictionaries. ** implies that the argument is a dictionary. Using ** [double star) is a shortcut that allows you to pass multiple arguments to a function directly using a dictionary.

पायथन ** में का उपयोग करनाः यह आमतौर पर पायथन में एक ट्रिक माना जाता है, जहाँ एक ही एक्सप्रेशन का उपयोग दो डिक्शनरी को मर्ज करने के लिए किया जाता है और एक तीसरे शब्दकोश में संग्रहीत किया जाता है। सिंगल एक्सप्रेशन ** है। यह अन्य दो डिक्शनरी को प्रभावित नहीं करता है। ** का अर्थ है कि तर्क एक डिक्शनरी है। **(डबल स्टार) का उपयोग करना एक शॉर्टकट है जो आपको सीधे डिक्शनरी का उपयोग करके फंक्शन के लिए कई तर्क पारित करने की अनुमति देता है।

Example 2:

dict1 = { ‘a’ : 10, ‘b’ : 8}
dict2 = { ‘c’: 6, ‘d’: 4}
dict3 = { **dict1, **dict2}
print (“dict1 :”, dict1)
print (“dict2 :”, dict2)
print (“dict3 :”, dict3)

Output:
dict1 : {‘a’: 10, ‘b’: 8}
dict2 : {‘c’: 6, ‘d’: 4}
dict3 : {‘a’: 10, ‘b’: 8, ‘c’: 6, ‘d’: 4}

Example 3: The task is to merge these three dictionaries into fourth dictionary.

टास्क इन तीन डिक्शनरी को चौथे डिक्शनरी में विलय करना है।

dic1= {1:10, 2:20}
dic2= {3:30, 4:40}
dic3= {5:50, 6:60}
dic4 = { }
for d in (dic1, dic2, dic3) : dic4. update (d)
print (dic4)

Output:
{1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}

Chapter wise Model Paper Link

About Me