Python: Tuples

Go to Chapter 20 of A Smarter Way.

A tuple is like a list, but its elements cannot be changed once they’re defined.

In a list we used square brackets to define the list:

friends = ['bill', 'jeff', 'jim', 'scott', 'joel']

By contrast, in a tuple we use parens (parentheses):

colors = ('red', 'green', 'blue')

We use tuples in situations like printing out a series of values.

animals = ('armadillo', 'squirrel', 'cat')
for animal in animals:
    print("Don't run over the " + animal + ".")

Exercises

See http://www.asmarterwaytolearn.com/python/20.html

See http://introtopython.org/lists_tuples.html#Tuples.

  1. In your examples.py, create a tuple with four values.
  2. Iterate and print those values.