Lists
Lists are a A way of organising data efficiently. data structure in Python that can store collections of data under one variable name. Lists can hold multiple data types. You can create a list in Python using square brackets as shown below.
Creating a shopping list (no pun intended)
shopping_list = ['strawberry', 'apple', 'pear', 'bread']
In the example above we have created a list that holds four strings.
Try creating your own lists. For example, create some lists that hold different data types.
Indexing lists
To select the individual elements of a list, you can use The process of selecting an element in a sequence. indexing. To index, you put the positional number of the element you want within a square bracket. In Python, indexing begins with 0. Therefore, the first element of a list is indexed using [0], the second is indexed using [1], and so forth. Below, we will print the second element of shopping_list.
Printing the second item of shopping_list
print(shopping_list[1])
Output
apple
In the code above, we accessed the second element of the shopping_list using [1]. Try performing some indexing of your own. For example, try indexing the other elements of shopping_list. Or, creating another list and performing indexing on that list.