Understanding slice notation

0 votes
Require an explanation on Python's slice notation. I am getting confused
Feb 3 in Python by Dev
• 6,000 points
17 views

1 answer to this question.

0 votes

To answer your question let me explain what is slicing first, 
A slice object is returned by the slice() function. A slice object specifies how a sequence should be sliced. You can specify where the slicing should begin and terminate. You can also specify which step to take.

slicing returns a list, slice assignment requires a list (or other iterable)

Slicing includes

start: the beginning index of the slice, it will include the element at this index unless it is the same as stop, defaults to 0, i.e. the first index. If it's negative, it means to start n items from the end.

stop: the ending index of the slice, it does not include the element at this index, defaults to length of the sequence being sliced, that is, up to and including the end.

step: the amount by which the index increases, defaults to 1. If it's negative, you're slicing over the iterable in reverse

For Example:

object[start:stop:step]

The colon, :, is what tells Python you're giving it a slice and not a regular index

Which implies:

object[slice(start, stop, step)]

In Slice object many arguments can be passed and depending upon the argument the slice object will behave differently. slice(start, stop[, step]) and slice(stop) works well. If you don’t want to specify any argument then use None, 

For Example
A[start : ] is same as  A[slice(start, None)] or

 a[::-1] is same as a[slice(None, None, -1)]

Slicing enables to access parts of any specified sequences like list, tuple, range or string and others.
Hope this answers your question.

answered Feb 4 by Nandini
• 5,480 points

Related Questions In Python

0 votes
1 answer

Slice notation in Python for string reversal

The slice notation is [start:end:step]. Step = ...READ MORE

answered Apr 25, 2018 in Python by Nietzsche's daemon
• 4,260 points
261 views
0 votes
1 answer
0 votes
1 answer

Understanding the map function

map isn't particularly pythonic. I would recommend ...READ MORE

answered Jul 31, 2018 in Python by Priyaj
• 58,060 points
152 views
+1 vote
1 answer

Understanding Pickling in Python

While others have pointed to the Python ...READ MORE

answered Aug 22, 2018 in Python by Priyaj
• 58,060 points
237 views
+1 vote
1 answer

Understanding Python super() with __init__() methods

It's been noted that in Python 3.0+ ...READ MORE

answered Aug 28, 2018 in Python by Priyaj
• 58,060 points
3,004 views
0 votes
1 answer

Understanding repr( ) function in Python

>>> x = 'foo' >>> x 'foo' So the name x is ...READ MORE

answered Sep 17, 2018 in Python by Priyaj
• 58,060 points
4,913 views
0 votes
1 answer

Understanding Python's slice notation

It's pretty simple really: a[start:end] # items start ...READ MORE

answered Oct 25, 2018 in Python by SDeb
• 13,300 points
315 views
0 votes
1 answer

How does slice notation in Python work?

The Python tutorial talks about it (scroll down a ...READ MORE

answered Oct 31, 2018 in Python by Priyaj
• 58,060 points
191 views
0 votes
2 answers
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP