Python Mini-CourseUniversity of OklahomaDepartment of Psychology Day 4 Lesson 15Tuples5/02/09Python Mini-Course: Day 4 Lesson 15*
Python Mini-Course: Day 4 Lesson 15
Lesson objectivesDescribe the characteristics of the tuple data structure in PythonPerform basic operations with tuples including creation, conversion, repetition, slicing, and traversingUse tuples in functionsUse tuples to traverse multiple sequences simultaneously5/02/09Python Mini-Course: Day 4 Lesson 15*
Python Mini-Course: Day 4 Lesson 15
The tuple data structureIn Python, a tuple is an immutable sequence of valuesEach value in the tuple is an element or itemElements can be any Python data typeTuples can mix data typesElements can be nested tuples5/02/09Python Mini-Course: Day 4 Lesson 15*
Python Mini-Course: Day 4 Lesson 15
Creating tuplesnumbers = (1, 2, 3, 4)print numberscheeses = ('swiss', 'cheddar', 'ricotta', 'gouda')print cheeses
5/02/09Python Mini-Course: Day 4 Lesson 15*
Python Mini-Course: Day 4 Lesson 15
Creating tuplest1 = ('a')print t1, type(t1)t2 = ('a',)print t2, type(t2)t3 = tuple('a')print t3, type(t3)empty = tuple()print empty
5/02/09Python Mini-Course: Day 4 Lesson 15*
Python Mini-Course: Day 4 Lesson 15
Creating tuplesalist = [1, 2, 3, 4]atuple = tuple(alist)print atuplestr = 'parrot'atuple = tuple(str)print atuple
5/02/09Python Mini-Course: Day 4 Lesson 15*
Python Mini-Course: Day 4 Lesson 15
Tuple indexingJust like other sequences, elements within a tuple are indexed print cheeses[0]Tuples are immutable cheeses[0] = 'Feta'
5/02/09Python Mini-Course: Day 4 Lesson 15*
Python Mini-Course: Day 4 Lesson 15
Slicing a tupleLike other sequences, tuples can be sliced print cheeses[1:4]
* Slicing a tuple creates a new tuple. It does not change the original tuple.
5/02/09Python Mini-Course: Day 4 Lesson 15*
Python Mini-Course: Day 4 Lesson 15
Using the + operatora = (1, 2, 3)b = (4, 5, 6)c = a + bprint a, b, c
*The + operator returns a new tuple that is a concatenation of two tuples5/02/09Python Mini-Course: Day 4 Lesson 15*
Python Mini-Course: Day 4 Lesson 15
Operations on tuplesTuples 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 repetitionThe len function
5/02/09Python Mini-Course: Day 4 Lesson 15*
Python Mini-Course: Day 4 Lesson 15
Tuples and functionsMany Python functions return tuplesRemember that a function can only return one valueHowever, if multiple objects are packaged together into a tuple, then the function can return the objects inside a single tuple5/02/09Python Mini-Course: Day 4 Lesson 15*
Python Mini-Course: Day 4 Lesson 15
Example: min_max.pydef 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)
5/02/09Python Mini-Course: Day 4 Lesson 15*
Python Mini-Course: Day 4 Lesson 15
Passing tuples as argumentsA parameter name that begins with * gathers all the arguments into a tupleThis allows functions to take a variable number of arguments5/02/09Python Mini-Course: Day 4 Lesson 15*
Python Mini-Course: Day 4 Lesson 15
Exampledef printall(*args): print args
printall(1, 2.0, 'three')5/02/09Python Mini-Course: Day 4 Lesson 15*
Python Mini-Course: Day 4 Lesson 15
Example: pointless.pydef 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])5/02/09Python Mini-Course: Day 4 Lesson 15*
Python Mini-Course: Day 4 Lesson 15
The zip functionBuilt-in function that takes two or more sequences and zips them into a list of tuples, where each tuple contains one element from each sequence5/02/09Python Mini-Course: Day 4 Lesson 15*
Python Mini-Course: Day 4 Lesson 15
The zip functionExample:s = 'abc't = [0, 1, 2]z = zip(s, t)print z5/02/09Python Mini-Course: Day 4 Lesson 15*
Python Mini-Course: Day 4 Lesson 15
The zip functionIf the sequences are not the same length, the result has the length of the shorter one
print zip('Anne', 'Elk')5/02/09Python Mini-Course: Day 4 Lesson 15*
Python Mini-Course: Day 4 Lesson 15
Using tuple assignment in a for loopt = [('a', 0), ('b', 1), ('c', 2)]for letter, number in t: print number, letter
5/02/09Python Mini-Course: Day 4 Lesson 15*
Python Mini-Course: Day 4 Lesson 15
Synchronized traversing: has_match.pydef 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)5/02/09Python Mini-Course: Day 4 Lesson 15*
Python Mini-Course: Day 4 Lesson 15
**