5.2 Lists
Here, we discuss lists in more detail and explain how to refer to particular list elements. Many of the capabilities shown in this section apply to all sequence types.
Creating a List
Lists typically store homogeneous data, that is, values of the same data type. Consider the list c
, which contains five integer elements:
In [1]: c = [-45, 6, 0, 72, 1543]
In [2]: c
Out[2]: [-45, 6, 0, 72, 1543]
They also may store heterogeneous data, that is, data of many different types. For example, the following list contains a student’s first name (a string), last name (a string), grade point average (a float
) and graduation year (an int
):
['Mary', 'Smith', 3.57, 2022]
Accessing Elements of a List
You reference a list element by writing ...
Get Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and The Cloud now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.