Lesson 15 - Tuples

advertisement
Day 4 – Lesson 15
Tuples
Python Mini-Course
University of Oklahoma
Department of Psychology
1
Python Mini-Course: Day 4 – Lesson 15
5/02/09
Lesson objectives
1. Describe the characteristics of the
tuple data structure in Python
2. Perform basic operations with tuples
including creation, conversion,
repetition, slicing, and traversing
3. Use tuples in functions
4. Use tuples to traverse multiple
sequences simultaneously
2
Python Mini-Course: Day 4 – Lesson 15
5/02/09
The tuple data structure
 In Python, a tuple is an
immutable sequence of values
 Each value in the tuple is an
element or item
 Elements can be any Python data
type
 Tuples can mix data types
 Elements can be nested tuples
3
Python Mini-Course: Day 4 – Lesson 15
5/02/09
Creating tuples
numbers = (1, 2, 3, 4)
print numbers
cheeses = ('swiss', 'cheddar',
'ricotta', 'gouda')
print cheeses
4
Python Mini-Course: Day 4 – Lesson 15
5/02/09
Creating tuples
t1 = ('a')
print t1, type(t1)
t2 = ('a',)
print t2, type(t2)
t3 = tuple('a')
print t3, type(t3)
empty = tuple()
print empty
5
Python Mini-Course: Day 4 – Lesson 15
5/02/09
Creating tuples
alist = [1, 2, 3, 4]
atuple = tuple(alist)
print atuple
str = 'parrot'
atuple = tuple(str)
print atuple
6
Python Mini-Course: Day 4 – Lesson 15
5/02/09
Tuple indexing
 Just like other sequences,
elements within a tuple are
indexed
print cheeses[0]
 Tuples are immutable
cheeses[0] = 'Feta'
7
Python Mini-Course: Day 4 – Lesson 15
5/02/09
Slicing a tuple
 Like other sequences, tuples can
be sliced
print cheeses[1:4]
* Slicing a tuple creates a new tuple. It
does not change the original tuple.
8
Python Mini-Course: Day 4 – Lesson 15
5/02/09
Using the + operator
a = (1, 2, 3)
b = (4, 5, 6)
c = a + b
print a, b, c
*The + operator returns a new tuple
that is a concatenation of two tuples
9
Python Mini-Course: Day 4 – Lesson 15
5/02/09
Operations on tuples
 Tuples support all the standard
sequence operations, including:
 Membership tests (using the in keyword)
 Comparison (element-wise)
 Iteration (e.g., in a for loop)
 Concatenation and repetition
 The len function
10
Python Mini-Course: Day 4 – Lesson 15
5/02/09
Tuples and functions
 Many Python functions return
tuples
 Remember that a function can
only return one value
 However, if multiple objects are
packaged together into a tuple, then
the function can return the objects
inside a single tuple
11
Python Mini-Course: Day 4 – Lesson 15
5/02/09
Example:
min_max.py
def min_max(t):
"""Returns the smallest and largest
elements of a sequence as a tuple"""
return (min(t), max(t))
seq = [12, 98, 23, 74, 3, 54]
print min_max(seq)
string = 'She turned me into a newt!'
print min_max(string)
12
Python Mini-Course: Day 4 – Lesson 15
5/02/09
Passing tuples as arguments
 A parameter name that begins
with * gathers all the arguments
into a tuple
 This allows functions to take a
variable number of arguments
13
Python Mini-Course: Day 4 – Lesson 15
5/02/09
Example
def printall(*args):
print args
printall(1, 2.0, 'three')
14
Python Mini-Course: Day 4 – Lesson 15
5/02/09
Example:
pointless.py
def pointless(required, optional=0, *args):
print 'Required: %s' % required
print 'Optional: %s' % optional
if args: print 'Others:
%s' % str(args)
print
pointless(1)
pointless(1, 2)
pointless(1, 2.0, 'three')
pointless(1, 2.0, 'three', [4])
15
Python Mini-Course: Day 4 – Lesson 15
5/02/09
The zip function
 Built-in function that takes two or
more sequences and “zips” them
into a list of tuples, where each
tuple contains one element from
each sequence
16
Python Mini-Course: Day 4 – Lesson 15
5/02/09
The zip function
 Example:
s = 'abc'
t = [0, 1, 2]
z = zip(s, t)
print z
17
Python Mini-Course: Day 4 – Lesson 15
5/02/09
The zip function
 If the sequences are not the
same length, the result has the
length of the shorter one
print zip('Anne', 'Elk')
18
Python Mini-Course: Day 4 – Lesson 15
5/02/09
Using tuple assignment in a for loop
t = [('a', 0), ('b', 1), ('c', 2)]
for letter, number in t:
print number, letter
19
Python Mini-Course: Day 4 – Lesson 15
5/02/09
Synchronized traversing:
has_match.py
def has_match(t1, t2):
for x, y in zip(t1, t2):
if x == y:
return True
return False
a = [5, 4, 9, 7, 10]
b = [4, 3, 5, 7, 15]
print has_match(a, b)
20
Python Mini-Course: Day 4 – Lesson 15
5/02/09
Download