230 likes | 593 Views
Day 4 – Lesson 15 Tuples. Python Mini-Course University of Oklahoma Department of Psychology . Lesson objectives. Describe the characteristics of the tuple data structure in Python Perform basic operations with tuples including creation, conversion, repetition, slicing, and traversing
E N D
Day 4 – Lesson 15Tuples Python Mini-Course University of Oklahoma Department of Psychology Python Mini-Course: Day 4 – Lesson 15
Lesson objectives • Describe the characteristics of the tuple data structure in Python • Perform basic operations with tuples including creation, conversion, repetition, slicing, and traversing • Use tuples in functions • Use tuples to traverse multiple sequences simultaneously Python Mini-Course: Day 4 – Lesson 15
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 Python Mini-Course: Day 4 – Lesson 15
Creating tuples numbers = (1, 2, 3, 4) print numbers cheeses = ('swiss', 'cheddar', 'ricotta', 'gouda') print cheeses Python Mini-Course: Day 4 – Lesson 15
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 Python Mini-Course: Day 4 – Lesson 15
Creating tuples alist = [1, 2, 3, 4] atuple = tuple(alist) print atuple str = 'parrot' atuple = tuple(str) print atuple Python Mini-Course: Day 4 – Lesson 15
Tuple indexing • Just like other sequences, elements within a tuple are indexed print cheeses[0] • Tuples are immutable cheeses[0] = 'Feta' Python Mini-Course: Day 4 – Lesson 15
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. Python Mini-Course: Day 4 – Lesson 15
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 Python Mini-Course: Day 4 – Lesson 15
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 Python Mini-Course: Day 4 – Lesson 15
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 Python Mini-Course: Day 4 – Lesson 15
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) Python Mini-Course: Day 4 – Lesson 15
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 Python Mini-Course: Day 4 – Lesson 15
Example def printall(*args): print args printall(1, 2.0, 'three') Python Mini-Course: Day 4 – Lesson 15
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]) Python Mini-Course: Day 4 – Lesson 15
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 Python Mini-Course: Day 4 – Lesson 15
The zip function • Example: s = 'abc' t = [0, 1, 2] z = zip(s, t) print z Python Mini-Course: Day 4 – Lesson 15
The zip function • If the sequences are not the same length, the result has the length of the shorter one print zip('Anne', 'Elk') Python Mini-Course: Day 4 – Lesson 15
Using tuple assignment in a for loop t = [('a', 0), ('b', 1), ('c', 2)] for letter, number in t: print number, letter Python Mini-Course: Day 4 – Lesson 15
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) Python Mini-Course: Day 4 – Lesson 15