Why do we not follow the off by one rule in python

0 votes

I am new learner and learning Python from a book.
I did what was written in the book....
I made a list like this below.....
 

players = ['charles', 'martina', 'michael', 'florence', 'eli']
 Then I wrote...
print(players[0:3])

#The results were like this....
['charles', 'martina', 'michael']

So my question is this.... When we write the starting number (which is 0 in this case) we follow the "off by one" rule.... but when we write 3 as the end value... we have the third element!!!! Why do we not follow the "off by one" rule here?

Nov 15, 2020 in Python by Rajiv
• 8,880 points
86 views

1 answer to this question.

0 votes

It's because of the way in which slicing notation works. [0:3] goes up to the 3rd element but does not include it. You can see this by slicing from the 3rd element until the end of the list
print(players[3:0])
#['florence, 'eli']

The 3 at the end is not the 3rd item in the list it is the 4th

answered Nov 15, 2020 by Gitika
• 65,870 points

Related Questions In Python

0 votes
2 answers

Why do we use return statement in python?

def maximum(x, y):     if x > y:     ...READ MORE

answered Apr 4, 2019 in Python by anonymous
616 views
0 votes
1 answer

How do I make one turtle follow another turtle in python?

Try something like this: from turtle import Turtle, ...READ MORE

answered Jul 8, 2019 in Python by Ayushi
1,587 views
+1 vote
7 answers
0 votes
1 answer

How do you get the logical xor of two variables in Python?

If you're already normalizing the inputs to ...READ MORE

answered May 29, 2018 in Python by aryya
• 7,440 points
9,676 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 2,032 views
0 votes
0 answers
+4 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,810 points
1,556 views
0 votes
1 answer

I want to download a file from the website by web scraping. Can anyone explain how to do this in jupyter lab (python) with an example?

Hey, Web scraping is a technique to automatically ...READ MORE

answered Apr 7, 2020 in Python by Gitika
• 65,870 points
664 views
0 votes
1 answer

How can we do the below modulo operation in python guys?

Expecting some operations, not loop. As because of ...READ MORE

answered Oct 14, 2020 in Python by anonymous
• 65,870 points
73 views