# Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 15 Tuples 5/02/09 Python Mini-Course: Day 4 – Lesson 15 1.

## Presentation on theme: "Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 15 Tuples 5/02/09 Python Mini-Course: Day 4 – Lesson 15 1."— Presentation transcript:

Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 15 Tuples 5/02/09 Python Mini-Course: Day 4 – Lesson 15 1

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 5/02/09 Python Mini-Course: Day 4 – Lesson 15 2

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 5/02/09 Python Mini-Course: Day 4 – Lesson 15 3

Creating tuples numbers = (1, 2, 3, 4) print numbers cheeses = ('swiss', 'cheddar', 'ricotta', 'gouda') print cheeses 5/02/09 Python Mini-Course: Day 4 – Lesson 15 4

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/02/09 Python Mini-Course: Day 4 – Lesson 15 5

Creating tuples alist = [1, 2, 3, 4] atuple = tuple(alist) print atuple str = 'parrot' atuple = tuple(str) print atuple 5/02/09 Python Mini-Course: Day 4 – Lesson 15 6

Tuple indexing Just like other sequences, elements within a tuple are indexed print cheeses[0] Tuples are immutable cheeses[0] = 'Feta' 5/02/09 Python Mini-Course: Day 4 – Lesson 15 7

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. 5/02/09 Python Mini-Course: Day 4 – Lesson 15 8

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 5/02/09 Python Mini-Course: Day 4 – Lesson 15 9

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 5/02/09 Python Mini-Course: Day 4 – Lesson 15 10

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 5/02/09 Python Mini-Course: Day 4 – Lesson 15 11

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) 5/02/09 Python Mini-Course: Day 4 – Lesson 15 12

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 5/02/09 Python Mini-Course: Day 4 – Lesson 15 13

Example def printall(*args): print args printall(1, 2.0, 'three') 5/02/09 Python Mini-Course: Day 4 – Lesson 15 14

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]) 5/02/09 Python Mini-Course: Day 4 – Lesson 15 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 5/02/09 Python Mini-Course: Day 4 – Lesson 15 16

The zip function Example: s = 'abc' t = [0, 1, 2] z = zip(s, t) print z 5/02/09 Python Mini-Course: Day 4 – Lesson 15 17

The zip function If the sequences are not the same length, the result has the length of the shorter one print zip('Anne', 'Elk') 5/02/09 Python Mini-Course: Day 4 – Lesson 15 18

Using tuple assignment in a for loop t = [('a', 0), ('b', 1), ('c', 2)] for letter, number in t: print number, letter 5/02/09 Python Mini-Course: Day 4 – Lesson 15 19

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) 5/02/09 Python Mini-Course: Day 4 – Lesson 15 20

Download ppt "Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 15 Tuples 5/02/09 Python Mini-Course: Day 4 – Lesson 15 1."

Similar presentations