A list is, as the name hints, a list of objects of any kind:
L = ['a' 20.0, 5] M = [3,['a', -3.0, 5]]
The individual objects are enumerated by assigning each element an index. The first element in the list gets index 0. This zero-based indexing is frequently used in mathematical notation. Consider the usual indexing of coefficients of a polynomial.
The index allows us to access the following objects:
L[1] # returns 20.0 L[0] # returns 'a' M[1] # returns ['a',-3.0,5] M[1][2] # returns 5
The bracket notation here corresponds to the use of subscripts in mathematical formulas. L
is a simple list, while M
itself contains a list so that one needs two indexes to access an element of the inner list.
A list containing subsequent integers can easily be generated by the command range
:
L=list(range(4)) # generates a list with four elements: [0, 1, 2 ,3]
A more general use is to provide this command with start, stop, and step parameters:
L=list(range(17,29,4)) # generates [17, 21, 25...