Daily Python Puzzle: List Indexing

What is the output of this code snippet?

 

squares = [1, 4, 9, 16, 25]
print(squares[0])

 

This puzzle introduces the simple but powerful list data structure in Python. You have to search very hard to find an algorithm that is not building upon a list. Many famous algorithms such as quicksort are based on only one list as core data structure.

Wikipedia defines a list as

“an abstract data type that represents a countable number of ordered values”.

It is abstract because it is independent of the concrete data type of the values in the list.

The Python way of handling lists and list access is super simple and clean. Create a list by writing comma-separated values between the opening and closing square brackets.

In the Java programming language, you must use redundant natural language function calls such as get(i) to access a list value. In Python, this is much easier. You access the i-th element in a list l with the intuitive bracket notation l[i]. This notation is consistent for all compound data types such as strings and arrays.

This leads to small and repeated time savings during programming. The time savings of millions of developers add up to a strong collective argument for Python.


Are you a master coder?
Test your skills now!

 

Related Video

Solution

1

 

Leave a Comment