List Basics

Sometimes we want to deal with lots of values at once. For that, Python uses lists!

listbasics.py
1
2
3
4
5
6
7
8
9
10
def main():
    solfege = [ "do", "re", "mi", "fa", "sol", "la", "ti" ]

    print(solfege)
    print(type(solfege))

    for syllable in solfege:
        print("{}\t{}".format(syllable, syllable.upper()))

main()

What You Should See

['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.

Study Drills

  1. 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