Lists
A list in Python is a sequence of values. Unlike strings which are a group of characters, a list can have any data type. It can also be a group of different data types at once which can be list type as well. For eg,
a = [12, 3.4, 34, 'cloudxlab', [2.3, 1.4]]
print(a)
It prints [12, 3.4, 34, 'cloudxlab', [2.3, 1.4]]
A list within another list is nested. Although a list can contain another list, the nested list still counts as a single element. A list that contains no elements is called an empty list. We can create one with empty brackets, []
.
Lists are mutable unlike strings.
Therefore, we can write something like this,
a[1] = 4
print(a)
The value at index 1
and it prints [12, 4, 34, 'cloudxlab', [2.3, 1.4]]
. For lists also, the indexing starts from 0
.
Loading comments...