Iloc ending slice

I’m getting confused with using the iloc [ ] slicing. In the course it states that using iloc the ending slice is not included. Here is the question and URL link its asking me to complete:

" Assign the first five rows of the null_previous_rank dataframe to the variable top5_null_prev_rank by choosing the correct method out of either loc[] or iloc[]."

Since the ending slice is not included, I thought the answer would be

top5_null_prev_rank = null_previous_rank.iloc[:6]

The right answer:

top5_null_prev_rank = null_previous_rank.iloc[:5]

Why is this? I’m trying to wrap my head around this. If anyone can help, greatly appreciate it!

Please pardon me being straightforward, this way of studying is less effective than one can be.
It shows that the student is writing code in large chunks before doing a single run at the end, with the hopes of getting the correct answer/passing a checker, but not developing the skill to investigate, debug, go beyond what is asked of to do something in an alternative/simpler/better way.

Ideally, the goal of exercises should be to learn how a tool behaves, with skeleton code provided to save the student setup time, while the student tweaks the tool parameters and inputs in all ways possible to understand it’s behaviour.

So 1 way to do this is do print your results and see for yourself what do you get when you do

  1. null_previous_rank.iloc[:6]
  2. null_previous_rank.iloc[:5]
  3. null_previous_rank.iloc[:]
  4. null_previous_rank.iloc[:0]
  5. null_previous_rank.iloc[4:5]
  6. null_previous_rank.iloc[5:5]
  7. null_previous_rank.iloc[6:5]
  8. null_previous_rank.iloc[:-1]
  9. null_previous_rank.iloc[-2:-1]
  10. null_previous_rank.iloc[-2:-2]
  11. null_previous_rank.iloc[-2:-3]
  12. null_previous_rank.iloc[0:5]

In software engineering this is called testing. Knowing what cases to use is a skill to develop in itself. For now i’m trying to expose a way of learning that allows the student to discover more for himself than what the lesson can provide.

1 Like

Thank you for the input and advice. I’ll get to it!