"Why do we use a method to get the index from a value but we use square brackets for the other way round?"
I was asked this question this week while looking at
>>> cities = ["London", "New York", "Berlin", "New Delhi"]
>>> cities.index("New York")
1
>>> cities[1]
'New York'
This can be confusing for those at an early stage of their journey into programming
The two operations seem equivalent: get the value from the index or get the index from the value
Why does one use a method and the other doesn't?
And the answer is that both of them use methods
From that point of view, there is no difference
When you use the square brackets notation, the program calls the method `.__getitem__()`
This happens "behind the scenes"
Therefore, methods are at the heart of fetching a value using the index or retrieving the index of a value
The difference is that there's a "shortcut" for one of these tasks
Python's syntax allows you to use the square brackets notation to make it easier to pass an argument to `.__getitem__()`
Why does one of them get special treatment? That's a language design choice
Let's take the list in this example. The main use case for lists is one where you need to fetch items based on their position in list
Being able to find where an item is within the list is a bonus
You'll find this behaviour in other places in Python once you dig a bit underneath the surface, for example when using arithmetic operators such as + and *
You'll also find it when using built-in functions such as `len()`
More on this tomorrow (since it was also a question asked this week in The Python Coding Programme!)