Tuples

Sequence Types

We know two sequence types. What are they?

How Are Strings and Lists Similar?

Sequence types support a number of operations…

Sequence Operations and Functions

But Strings and Lists Are Different!

How are strings and lists different?

But there's also another big difference.

A Third Sequence Type

Surely you must be thinking: I really wish that my list was not mutable

(well… probably not… but…)

Shirley

Tuples

A tuple is an immutable grouping of data. Think of it as an immutable list.

Tuple Syntax

A tuple is really just a group of comma separated values. A tuple literal is just values and commas. Really! (btw, chugs exist, and are a thing)

>>> dogs = "chihuahua", "pug", "chug"
>>> print(dogs)
('chihuahua', 'pug', 'chug')

However… it's common to put parentheses around a tuple (in fact, when you print a tuple, parentheses are always placed around it).(apparently Pelicans is now the name of a professional basketball team!)

>>> birds = ("pelican", "owl", "pigeon")
>>> print(birds)
('pelican', 'owl', 'chug')

Seem Familiar?

We've actually seen and used tuples in the past! Does anyone remember where we've seen tuples before?

String formatting with the % operator:
"I've %s this %s before!" % ("seen", "this")
Multiple assignment
a, b, c = 1, 2, 3

What About Function Parameters?

Aren't function parameters comma separated?

>>> print(1, 2)
1 2
>>> print((1, 2))
(1, 2)

Tuple Operations and Built-In Functions

Tuple Operations and Built-In Functions Continued

Let's try the following:

Tuple Operations Examples

Some operations:

a = (1,2,3)
print(a + (4, 5, 6))
print(a[0])
print(a[:2])
print(len(a))
print(5 in a)
(1, 2, 3, 4, 5, 6)
1
(1, 2) # note that slicing a tuple returns a tuple!
3
False

Iteration

Just like iterating over strings or lists.

for value in (1, 2, 3):
	print(value)

Tuple Unpacking / Multiple Assignment

We talked briefly talked about multiple assignment. This is generally done with tuples, and when it's done with tuples, it's called tuple unpacking.

>>> first_name, last_name = ("Hiro", "Protagonist")
>>> print(first_name)
Hiro
>>> print(last_name)
Protagonist

Tuple Unpacking Examples

What does this code output?

values = (1, 2, 3)
a, b, c = values
print(a)
print(b)

c, a, b = values
print(a)
print(b)
1
2
2
3

More Info About Multiple Assignment

Tuple unpacking is the most common way of performing multiple assignment… but the assignment operator is actually super flexible:

More than you ever wanted to know about the assignment operator

List of Tuples

A tuple within a list is retrieved as a single object, as with every other element in a list, when using our regular for loop_variable in some_list syntax:

characters = [("Hiro", "Protagonist"), ("Yours", "Truly")]
for character in characters:
	print(character)

You get each actual tuple, so this prints out:

('Hiro', 'Protagonist')
('Yours', 'Truly')

List of Tuples Continued

Unpacking works in for loops as well! Imagine that each element is retrieved form the outer list. Each element is a tuple which can be unpacked into multiple loop variables.

characters = (("Hiro", "Protagonist"), ("Yours", "Truly"))
for first, last in characters:
	print("first name is " + first)
	print("last name is " + last)

List of Tuples Example

pairs_of_numbers = [(1, 2), (2, 3)]
for a, b in pairs_of_numbers:
	print(a + b)
3
5

Returning Tuples

Tuples and tuple unpacking can provide a method of returning multiple values from a function. What do you think this prints out?

def calculate_3d_point():
	result = (2, 4, 0)
	return result

x, y, z = calculate_3d_point()
print("the z coordinate is %s" % (z))
print("the x coordinate is %s" % (2))
the z coordinate is 0
the x coordinate is 2

Tuples Exercise

Tuples Exercise

  1. create a tuple of tuple literals that represent x, y coordinates of corners of a square
  2. assume that the bottom left corner is at (0, 0) and the upper right is (0, 50)
  3. assign that tuple to a variable
  4. use a for loop and tuple unpacking to get each x, y value
  5. within the for loop, use goto with your loop variables to draw the square

Template

import turtle
t = turtle.Turtle()
wn = turtle.Screen()
#  your code here!
wn.mainloop()

Tuples Solution

import turtle
t = turtle.Turtle()
wn = turtle.Screen()
for x, y in [(0,50),(50,50),(50,0),(0,0)]:
	t.goto(x, y)
wn.mainloop()

That's cool and all… but why would an immutable list ever be useful?

When To Use Tuples Continued