Indexing - Teaching Things Better609 words. Time to Read: About 6 minutes.
I want to talk about indexing. More importantly, I want to talk about how we are explaining indexing to new programmers. But first, I need to lay some background.
Right now I’m working as an engineer, but I’m a teacher at heart. I love taking someone through the process of learning something, finding the analogy or way of looking at a concept that really makes it click for them. Especially in the areas of math, mechanics, and programming, where things are very predictable and there’s always concrete reasoning behind each concept. I’m a firm believer that students should never just accept what the teacher says on faith, and there should definitely be no magic.
items. You’ll just have to remember that computers always start counting at zero.” To which I replied:
There’s got to be a better way. You shouldn’t have to just remember anything. And so, without further background sections, I present to you:
A Better Way
I’m also going to use Python for my explanation because I like it better and I do what I want. Let us consider the string
beans. Not string beans, but– you get it.
The main thing to understand is that indices don’t actually fall on a particular item in a list or character in a string (since they are accessed the same way). They surround the letters. They surround the letters. They surround the letters. One more time for the people in the back.
Indices surround the letters.
Thus, when you try this:
You get “ea” because those are the letters in between the two indices you specified. When you only specify one index, the program defaults to making the second limit the next index.
The benefit of understanding things this way is that negative indices get a lot less confusing.
In Python, in order to say “all the way to the very end,” you just leave the index off. This is mostly convenience and because -0 doesn’t make any sense as an index (since -0 technically equals 0).
A good number of the 2 people that read this post will have already learned (one way or another) about indexing strings and lists. But do you see now that there is a good reason for starting at zero and ending our for loops at
len(string) - 1? It’s not magic. It’s not because computers are weird and count from zero because that’s how their culture does it. There’s no little gremlin decreeing new ways to count inside your computer. Now go forth and spread the logical step-by-step teaching approach!