Lsn15_Tuples

  • Upload
    huseyi

  • View
    215

  • Download
    0

Embed Size (px)

Citation preview

  • 8/7/2019 Lsn15_Tuples

    1/20

    Python Mini-Course

    University of Oklahoma

    Department of Psychology

    Day 4 Lesson 15Tuples

    5/02/09Python Mini-Course: Day 4 Lesson 151

  • 8/7/2019 Lsn15_Tuples

    2/20

    Lesson objectives

    1. Describe the characteristics of thetuple data structure in Python

    2. Perform basic operations with tuplesincluding creation, conversion,repetition, slicing, and traversing

    3. Use tuples in functions4. Use tuples to traverse multiple

    sequences simultaneously

    5/02/09Python Mini-Course: Day 4 Lesson 152

  • 8/7/2019 Lsn15_Tuples

    3/20

    The tuple data structure

    yIn Python, a tuple is animmutable sequence of valuesyEach value in the tuple is anelementor item

    yElements can be any Python data

    typeyTuples can mix data types

    yElements can be nested tuples

    5/02/09Python Mini-Course: Day 4 Lesson 153

  • 8/7/2019 Lsn15_Tuples

    4/20

    Creating tuples

    numbers = (1, 2, 3, 4)

    print numbers

    cheeses = ('swiss', 'cheddar','ricotta', 'gouda')

    print cheeses

    5/02/09Python Mini-Course: Day 4 Lesson 154

  • 8/7/2019 Lsn15_Tuples

    5/20

    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/09Python Mini-Course: Day 4 Lesson 155

  • 8/7/2019 Lsn15_Tuples

    6/20

    Creating tuples

    alist = [1, 2, 3, 4]

    atuple = tuple(alist)

    print atuplestr = 'parrot'

    atuple = tuple(str)

    print atuple

    5/02/09Python Mini-Course: Day 4 Lesson 156

  • 8/7/2019 Lsn15_Tuples

    7/20

    Tuple indexing

    yJust like other sequences,elements within a tuple are

    indexedprint cheeses[0]

    yTuples are immutable

    cheeses[0] = 'Feta'

    5/02/09Python Mini-Course: Day 4 Lesson 157

  • 8/7/2019 Lsn15_Tuples

    8/20

    Slicing a tuple

    yLike other sequences, tuples canbe sliced

    print cheeses[1:4]

    *Slicing a tuple creates a new tuple. Itdoes not change the original tuple.

    5/02/09Python Mini-Course: Day 4 Lesson 158

  • 8/7/2019 Lsn15_Tuples

    9/20

    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/09Python Mini-Course: Day 4 Lesson 159

  • 8/7/2019 Lsn15_Tuples

    10/20

    Operations on tuples

    y Tuples support all the standardsequence operations, including:y

    Membership tests (using the in keyword)yComparison (element-wise)

    y Iteration (e.g., in a for loop)

    yConcatenation and repetition

    yThe len function

    5/02/09Python Mini-Course: Day 4 Lesson 1510

  • 8/7/2019 Lsn15_Tuples

    11/20

    Tuples and functions

    yMany Python functions returntuples

    yRemember that a function canonly return one valueyHowever, if multiple objects are

    packaged together into a tuple, thenthe function can return the objectsinside a single tuple

    5/02/09Python Mini-Course: Day 4 Lesson 1511

  • 8/7/2019 Lsn15_Tuples

    12/20

  • 8/7/2019 Lsn15_Tuples

    13/20

    Passing tuples as arguments

    yA parameter name that beginswith * gathers all the arguments

    into a tupleyThis allows functions to take avariable number of arguments

    5/02/09Python Mini-Course: Day 4 Lesson 1513

  • 8/7/2019 Lsn15_Tuples

    14/20

    Example

    def printall(*args):

    print args

    printall(1, 2.0, 'three')

    5/02/09Python Mini-Course: Day 4 Lesson 1514

  • 8/7/2019 Lsn15_Tuples

    15/20

    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/09Python Mini-Course: Day 4 Lesson 1515

  • 8/7/2019 Lsn15_Tuples

    16/20

    The zip function

    yBuilt-in function that takes two ormore sequences and zips them

    into a list of tuples, where eachtuple contains one element fromeach sequence

    5/02/09Python Mini-Course: Day 4 Lesson 1516

  • 8/7/2019 Lsn15_Tuples

    17/20

    The zip function

    yExample:s = 'abc'

    t = [0, 1, 2]

    z = zip(s, t)

    print z

    5/02/09Python Mini-Course: Day 4 Lesson 1517

  • 8/7/2019 Lsn15_Tuples

    18/20

    The zip function

    yIf the sequences are not thesame length, the result has the

    length of the shorter one

    print zip('Anne', 'Elk')

    5/02/09Python Mini-Course: Day 4 Lesson 1518

  • 8/7/2019 Lsn15_Tuples

    19/20

    Using tuple assignment in a for loop

    t = [('a', 0), ('b', 1), ('c', 2)]

    for letter, number in t:

    print number, letter

    5/02/09Python Mini-Course: Day 4 Lesson 1519

  • 8/7/2019 Lsn15_Tuples

    20/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/09Python Mini-Course: Day 4 Lesson 1520