[python] Understanding slice notation

The rules of slicing are as follows:

[lower bound : upper bound : step size]

I- Convert upper bound and lower bound into common signs.

II- Then check if the step size is a positive or a negative value.

(i) If the step size is a positive value, upper bound should be greater than lower bound, otherwise empty string is printed. For example:

s="Welcome"
s1=s[0:3:1]
print(s1)

The output:

Wel

However if we run the following code:

s="Welcome"
s1=s[3:0:1]
print(s1)

It will return an empty string.

(ii) If the step size if a negative value, upper bound should be lesser than lower bound, otherwise empty string will be printed. For example:

s="Welcome"
s1=s[3:0:-1]
print(s1)

The output:

cle

But if we run the following code:

s="Welcome"
s1=s[0:5:-1]
print(s1)

The output will be an empty string.

Thus in the code:

str = 'abcd'
l = len(str)
str2 = str[l-1:0:-1]    #str[3:0:-1] 
print(str2)
str2 = str[l-1:-1:-1]    #str[3:-1:-1]
print(str2)

In the first str2=str[l-1:0:-1], the upper bound is lesser than the lower bound, thus dcb is printed.

However in str2=str[l-1:-1:-1], the upper bound is not less than the lower bound (upon converting lower bound into negative value which is -1: since index of last element is -1 as well as 3).

Examples related to python

programming a servo thru a barometer Is there a way to view two blocks of code from the same file simultaneously in Sublime Text? python variable NameError Why my regexp for hyphenated words doesn't work? Comparing a variable with a string python not working when redirecting from bash script is it possible to add colors to python output? Get Public URL for File - Google Cloud Storage - App Engine (Python) Real time face detection OpenCV, Python xlrd.biffh.XLRDError: Excel xlsx file; not supported Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation

Examples related to list

Convert List to Pandas Dataframe Column Python find elements in one list that are not in the other Sorting a list with stream.sorted() in Java Python Loop: List Index Out of Range How to combine two lists in R How do I multiply each element in a list by a number? Save a list to a .txt file The most efficient way to remove first N elements in a list? TypeError: list indices must be integers or slices, not str Parse JSON String into List<string>

Examples related to slice

How to search for an element in a golang slice Why can't I duplicate a slice with `copy()`? Correct way to initialize empty slice How to join a slice of strings into a single string? How to get the last element of a slice? Slice indices must be integers or None or have __index__ method Remove last item from array How do you clear a slice in Go? Concatenate two slices in Go how to get the last part of a string before a certain character?

Examples related to iterable

Convert Iterable to Stream using Java 8 JDK Python - TypeError: 'int' object is not iterable Get size of an Iterable in Java Convert Java Array to Iterable What exactly are iterator, iterable, and iteration? What is the difference between iterator and iterable and how to use them? Easy way to convert Iterable to Collection How do I add the contents of an iterable to a set? In Python, how do I determine if an object is iterable? Java: Get first item from a collection