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,870 points
688 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,770 points

Related Questions In Python

0 votes
2 answers

Why do we use return statement in python?

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

answered Apr 4, 2019 in Python by anonymous
2,051 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
5,392 views
0 votes
0 answers

What do the three different types of brackets in Python code mean? I'm not sure if this is right, so if I'm wrong, please tell me:

[] - Normally used for dictionaries, list items () - ...READ MORE

Sep 11, 2023 in Python by Satyawrat
• 460 points
291 views
+1 vote
7 answers
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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,466 views
0 votes
1 answer
+5 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,790 points
4,127 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,770 points
2,423 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,770 points
876 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP