Lists
If we have more than one model and we want to store each one's name we can create one variable to each model as:
>>> model_0 = 'BMW Z4' >>> model_1 = 'Audi TT' >>> model_0 'BMW Z4' >>> model_1 'Audi TT'
And if there were 10 or 20 models? It would be impractical to create one variable to each one.
In instead of creating one variable to each model we can create a list of models:
>>> model = ['BMW Z4', 'Audi TT'] >>> model ['BMW Z4', 'Audi TT'] >>>
Like in the variables names we created, model_0 and model_1, each position in a list is acessible by an index starting with 0. So the first model is model[0]
>>> model[0] 'BMW Z4' >>> model[1] 'Audi TT' >>>
We can add new models to the list:
>>> model = model + ['Mercedez CLK350'] >>> model ['BMW Z4', 'Audi TT', 'Mercedez CLK350'] >>> model[2] 'Mercedez CLK350' >>>
For each model we can associate a mileage in another list
>>> mpg = [21, 20, 19] >>> mpg [21, 20, 19] >>>
Now just print it all
>>> print model[0], 'mileage is', mpg[0], 'miles per gallon' BMW Z4 mileage is 21 miles per gallon >>> print model[1], 'mileage is', mpg[1], 'miles per gallon' Audi TT mileage is 20 miles per gallon >>> print model[2], 'mileage is', mpg[2], 'miles per gallon' Mercedez CLK350 mileage is 19 miles per gallon >>>
Proposed exercise
Since these are sports cars the mileage is not that important. Build a list with each car's acceleration time from 0 to 60 mph and print it for each car. Ask for help or contribute your solution to the forum