How to Store Ordered Data: List
In Python, a List
is a datatype that represents ordered data. It allows storing and modifying elements of various data types.
Lists are created by enclosing elements in square brackets []
and separating each element with a comma (,
).
Each element in a list can be accessed by an Index
.
An index is a number that indicates the position of a specific element within a data structure that contains sequential elements, like a list.
Indexes in a list start from 0
, with 0
pointing to the first element in the list.
# Creating a list of integers numbers = [100, -2, 32, 450]
In the example above, the elements corresponding to each index in the numbers
list are as follows:
-
numbers[0]
: 100 -
numbers[1]
: -2 -
numbers[2]
: 32 -
numbers[3]
: 450
What Data Can Be Stored in a List?
Lists can store various types of data, such as integers, strings, and other lists.
# Creating a list that includes various data types mixed = [1, "apple", True, 3.14]
In the example above, the mixed
list includes elements of various data types such as an integer, string, boolean, and floating-point number.
Moreover, lists can contain other lists as well.
mixed_list = [1, "apple", ["Python"]]
In the example above, the third element of mixed_list
, i.e., mixed_list[2]
, includes another list ["Python"]
.
Does the index of a list start at 1?
Guidelines
AI Tutor
Publish
Design
Upload
Notes
Favorites
Help
Code Editor
Execution Result