Sometimes we want to deal with lots of values at once. For that, Python uses lists!
1 2 3 4 5 6 7 8 9 10 |
|
['do', 're', 'mi', 'fa', 'sol', 'la', 'ti'] <type 'list'> do DO re RE mi MI fa FA sol SOL la LA ti TI
Lists in Python are very similar to arrays in Java. A single variable holds many values of the same type. In this assignment, the list solfege contains seven strings.
The typical way to access each value in a list is with a for
loop. When you say for blah in list:
then the loop moves through the list, one item at a time. The variable blah gets set to the first value in the list the first time through, gets set to the second value the second time, and so on.
- Create a second list of strings and put several values into it. Then add a second
for
loop to display those values on the screen.
©2017 Graham Mitchell