Require an explanation on Python's slice notation. I am getting confused
Feb 3, 2022 in Python 855 views

## 1 answer to this question.

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.

answered Feb 4, 2022 by
• 5,480 points

## Slice notation in Python for string reversal

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

## How to slice an array using python numpy? Is there any numpy tutorial which has covered all its operations?

Slicing is basically extracting particular set of ...READ MORE

## Understanding the map function

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

+1 vote

## Understanding Pickling in Python

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

+1 vote

## Understanding Python super() with __init__() methods

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

## Understanding repr( ) function in Python

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

## Understanding Python's slice notation

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

## How does slice notation in Python work?

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