#13 Indexing

About Indexing

Indexing is a way to access a specific element in a iteritable sequence.

Syntax

Sample list:

To access a specific element in this list, we’d do:

The ‘…” is representing the position of element we want to access, so for example:

This has the value of “World!” as it’s the second element in the list. In python we count from 0 hence this being 1 and not 2. Indexing isn’t limited to lists, we can use it on anything that’s iteritable, such as strings…

We also have something named slicing that allows us to do a bunch of neat operations to a iteritable sequence. 

Unlike in indexing, we count from -1 not 0. Running “m[-2]” would give us Hello. We can also use the “:” keyword:

Examples

I won’t be explaining much as it’s pretty much said in the comments provided.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.