A data type – list
A list? is another type of data. Unlike a tuple, which uses parentheses, lists use square brackets, [
and ]
, as shown in the following code. A list could include different types of data, such as string, integer, float, and A list itself:
>>>record=['John',21,'Engineer',3] >>>record ['John', 21, 'Engineer', 3]
Like tuples, the first data item starts with a subscript of zero. If we want to list all the data items from subscript 1
to the end of the list, we use record[1:]
, and to list all the data items from subscript 2
to the end of the list, we use record[2:]
, as shown in the following code:
>>>len(record) 4 >>>record[0] 'John' >>>record[2:] ['Engineer', 3]
Unlike tuples, which are immutable, lists can be modified.
record[0]='Mary' >>>record[0] 'Mary'