Indexes and Slices

Learn how to work with indexes and slices in Python.

We'll cover the following

In Python, as in other languages, some data structures or types support accessing its elements by index. Another thing it has in common with most programming languages is that the first element is placed in the index number 0. However, unlike those languages, when we want to access the elements in a different order than usual, Python provides extra features.

For example, how do we access the last element of an array in C? If we think about it the same way we would in C, we would get the element in the position of the length of the array minus one. This would work in Python too, but we could also use a negative index number, which will start counting from the last element, as shown in the following commands:

Get hands-on with 1400+ tech skills courses.