Slicing in Python

Slicing in Python

It is a Python methodology that enables accessing parts of data from the given sequence like lists, tuples, strings etc. and objects that support sequence protocol. A slice object is created based on a set of indices (specified by range) as using the slice constructor where you may specify the start, stop and step indices.

स्लाइसिंग एक पायथन कार्यप्रणाली है जो दिए गए अनुक्रमों जैसे सूचियों, टपल्स, स्ट्रिंग्स आदि से डेटा के कुछ हिस्सों तक पहुँचने में सक्षम बनाता है और सिक्यूएंस प्रोटोकॉल का समर्थन करने वाली ऑब्जेक्ट्स । एक स्लाइस ऑब्जेक्ट इंडिसेस के एक सेट (रंज द्वारा निर्दिष्ट) के आधार पर बनाया जाता है, क्योंकि आप स्लाइस कंस्ट्रक्टर का उपयोग करते हैं, जहां आप स्टार्ट, स्टॉप और स्टेप इंडेक्स निर्दिष्ट कर सकते हैं।

Syntax: strObj [ begin : end : step ]

Begin: Starting index where the slicing of the object begins.

इंडेक्स शुरू करना जहां ऑब्जेक्ट का सिक्वेन्स करना शुरू होता है।

End: Ending index where the slicing of the object ends.

एंडिंग इंडेक्स जहां ऑब्जेक्ट का स्लाइसिंग समाप्त होता है

Step: Increment value. -‘इंक्रीमेंट वैल्यू

slice() function returns a slice object used to slice a sequence in the given indices.

slice() फंक्शन दिए गए इंडेक्सों में एक अनुक्रम को स्लाइस करने के लिए उपयोग की जाने वाली एक स्लाइस

ऑब्जेक्ट देता है।

Note: नोट

1. Step value can be +Ve or -Ve, but cannot be zero otherwise PVM gives an error message.

स्टेप वैल्यू +Ve या -Ve हो सकता है, लेकिन शून्य नहीं हो सकता अन्यथा पीवीएम एक एरर मैसेज देता है।

2. If +ve then it should be from left to right forward direction (0 to end-1).

यदि +ve तो यह बाएं से दाएं आगे की दिशा में होना चाहिए (0 से end-1)।

में होना चाहिए (0 से end-1)।

3. If-ve then it should be from right to left backward direction (-1 to end+1).

यदि -ve तो यह दाईं से बाईं ओर की दिशा में होना चाहिए (-1 से end+1)।

For example: Using in List object – लिस्ट ऑब्जेक्ट में उपयोग करना

num = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
num1 = num [3:8]
print (num1)

In our example start equals 3, so our slice starts from value 40. stop is 8, so the last element of the slice is 80, here we are passing +Ve index, then it should be from left to right forward direction (3 to 8-1). In the end, slice creates a new list(we named it num1) with selected elements.

हमारे उदाहरण में स्टार्ट 3 के बराबर है, इसलिए हमारा स्लाइस वैल्यू 40 से शुरू होता है। स्टॉप 8 है, इसलिए स्लाइस का अंतिम एलीमेंट 80 है, यहां हम +Ve इंडेक्स से गुजर रहे हैं, तो यह बाएं से दाएं आगे की दिशा में होना चाहिए (3 से 8-1)। अंत में, स्लाइस चयनित एलीमेंट के साथ एक नई लिस्ट बनाता है (हमने इसे num1 नाम दिया है)।

Output:
[40, 50, 60, 70, 80]

Note:    

1. If you don’t specify any begin index value then it will. consider from beginning of the sequence.

यदि आप किसी आरंभिक इंडेक्स वैल्यू को निर्दिष्ट नहीं करते हैं तो यह अनुक्रम की शुरूआत से विचार करेगा।

2. If you don’t specify any end index value then will consider up to end of the sequence.

यदि आप किसी इंडेक्स को निर्दिष्ट नहीं करते हैं तो अनुक्रम के अंत तक विचार करेंगे।

3. If you don’t specify value of begin & end index then total sequence will be taken by default.

यदि आप आरंभ और अंत इंडेक्स का वैल्यू निर्दिष्ट नहीं करते हैं तो कुल अनुक्रम डिफॉल्ट रूप से लिया जाएगा।

4. If you don’t specify value of step then ‘1’ will be taken by default.

यदि आप स्टेप का वैल्यू निर्दिष्ट नहीं करते हैं, तो डिफॉल्ट रूप से ‘1’ लिया जाएगा।

For example:

num = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
print (num[:7])
print (num[4:])
print (num [::])

Output:
[10, 20, 30, 40, 50, 60, 70]
[50, 60, 70, 80, 90, 100]
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

Note:

1. If index is invalid in indexing process, we get index out of range error, where as in slicing never get index error.

यदि इंडेक्स को इंडेक्सिंग प्रोसेस में अमान्य कर दिया जाता है, तो हमें इंडेक्स को एरर से बाहर कर दिया जाता है, जैसे कि स्लाइसिंग में कभी भी इंडेक्स एरर नहीं मिलती है।

2. In slicing, end index is bigger than ‘end index -1’, PVM will take up to the last position character only.

स्लाइसिंग में इंड इंडेक्स ‘इंड इंडेक्स -1’ से बड़ा है, पीवीएम केवल अंतिम स्थिति के कैरेक्टर तक ले जाएगा।

For example:

num = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
print (num [4:70])
print (num [-11 :-100])
print (num [-2:10])

Output:
[50, 60, 70, 80, 90, 100]
[]
[90, 100]

Note:

1. Step value can be +ve or -ve, but cannot be zero otherwise PVM gives an error message.

स्टेप वैल्यू +ve या -ve हो सकता है, लेकिन शून्य नहीं हो सकता, अन्यथा पीवीएम एक एरर मैसेज देता है।

For example:

>>> print (num [ : :0] )
ValueError: slice step cannot be zero

2. If +ve then it should be from left to right forward direction (0 to end-1).

यदि + ve तो यह बाएं से दाएं आगे की दिशा में होना चाहिए (0 से end-1)

For example:

>>> no= [1,2,3,4,5,6,7,8,9]
>>> print (no[::1])
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> print (no [::2] )
[1, 3, 5, 7, 9]
>>> print (no[-6:8] )
[4, 5, 6, 7, 8]

3. If -ve then it should be from right to left backward direction (-1 to end+1).

यदि – ve तो यह दाई ओर से बाईं ओर की दिशा में होना चाहिए (0 से end+1)

For example:

>>> no= [1, 2, 3, 4,5,6,7,8,9]
>>> print (no[::-1] )
[9, 8, 7, 6, 5, 4, 3, 2, 1]
>>> print (no [ : : -2] )
[9, 7, 5, 3, 1]
>>> print (no[-8:-5])
[2, 3, 4]

In Python, slice operator while using forward direction:

पायथन में, आगे की दिशा का उपयोग करते हुए स्लाइस ऑपरेटर:

1. Default value for begin index is ‘0’.

आरंभिक इंडेक्स का डिफाल्ट वैल्यू ‘0’ है।

2. Default value for end index is ‘length of sequence’.

इंड इंडेक्स का डिफाल्ट वैल्यू length of sequence’ है।

3. Default value for step is ‘1’.

स्टेप के लिए डिफाल्ट वैल्यू ‘1’ है।

In Python, slice operator while using backward direction:

पायथन में, पिछड़े दिशा का उपयोग करते समय स्लाइस ऑपरेटरः

1. Default value for begin index is ‘-1’.

आरंभिक इंडेक्स का डिफॉल्ट वैल्यू -1′ है।

2. Default value for end index is ‘length of sequence+1′.

इंड इंडेक्स के लिए डिफॉल्ट वैल्यू length of sequence+1′ है।

3. Default value for step is ‘-1’.

स्टेप के लिए डिफाल्ट वैल्यू -1′ है।

For example:

>>> no= [1,2,3,4,5,6,7,8,9]
>>> print (no[::1])
>>> print (no[::2])
>>> print (no [-6:8])
>>> print (no[::-1])
>>> print (no[::-2])
>>> print (no [-8:-5])
>>> print (no [1:4:1])
>>> print (no [1:4:-1])
>>> print (no[-1:-9:-1])
>>> print (no [-1:-9:1])
>>> print (no[-1:-9:1])
>>> print (no [2:-2:1])
>>> print (no [-1:2:-1])
>>> print (no[::-1])
>>> print (no[::1])

Output:[1, 2, 3, 4, 5, 6, 7, 8, 9]
Output:[1, 3, 5, 7, 9]
Output: [4, 5, 6, 7, 8]
Output:[9, 8, 7, 6, 5, 4, 3, 2, 1]
Output:[9, 7, 5, 3, 1]
Output:[2, 3, 4]
Output:[2, 3, 4]
Output:[]
Output:[9, 8, 7, 6, 5, 4, 3, 2]
Output:[]
Output:[]
Output:[3, 4, 5, 6, 7]
Output:[9, 8, 7, 6, 5, 4]
Output:[9, 8, 7, 6, 5, 4, 3, 2, 1]
Output:[1, 2, 3, 4, 5, 6, 7, 8, 9]

Example: Create a tuple and a slice object. Start the slice object at position 3, and slice to position 5, and return the result:

एक टपल और एक स्लाइस ऑब्जेक्ट बनाएं। पोजीशन 3 पर स्लाइस ऑब्जेक्ट को प्रारंभ करें, और स्लाईस को पोजीशन 5 पर ले जाएँ, और परिणाम पर लौटें:

a = (“a”, “b”, “c”, “d”, “e”, “f”, “g”, “h”)
x = slice (3, 5)
print (a [x])

Output:
(‘d’, ‘e’)

Slice Copying – स्लाइस कॉपी करना

Slice creates a shallow copy of the initial list.

स्लाइस प्रारंभिक लिस्ट की एक शैलो कॉपी बनाता है।

>>> num = [10, 20, 30, 40, 50, 60, 70, 80, 90]
>>> part = num[1:5]
>>> num
>>> part
[10, 20, 30, 40, 50, 60, 70, 80, 90]
>>> part[2] = 10
>>> num
[10, 20, 30, 40, 50, 60, 70, 80, 90]
>>> part
[20, 30, 10, 50]
>>> part = num[:] # short form
>>> part
[10, 20, 30, 40, 50, 60, 70, 80, 90]

Chapter wise Model Paper Link

About Me