Download presentation

Presentation is loading. Please wait.

Published byMadelyn Pole Modified about 1 year ago

1
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

2
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

3
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

4
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

5
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

6
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

7
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

8
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

9
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

10
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

11
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

12
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

13
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

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

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

16
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

17
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

18
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

19
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

20
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

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google