P. 1
Python Programming

Python Programming

|Views: 148|Likes:
Published by Eric Kivuva

More info:

Published by: Eric Kivuva on Jul 23, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

01/01/2013

pdf

text

original

Previous:

Lists

IndexNext: Dictionaries

About tuples in Python

A tuple in Python is much like a list except that it is immutable (unchangeable) once created. They are generally

used for data which should not be edited.

Tuple notation

Tuples may be created directly or converted from lists. Generally, tuples are enclosed in parenthesis.

>>> l = [1, 'a', [6, 3.14]]

>>> t = (1, 'a', [6, 3.14])

>>> t

(1, 'a', [6, 3.1400000000000001])

>>> tuple(l)

(1, 'a', [6, 3.1400000000000001])

>>> t == tuple(l)

True

>>> t == l

False

A one item tuple is created by a item in parens followed by a comma:

>>> t = ('A single item tuple',)

>>> t

('A single item tuple',)

Python Programming/Tuples

78

Also, tuples will be created from items separated by commas.

>>> t = 'A', 'tuple', 'needs', 'no', 'parens'

>>> t

('A', 'tuple', 'needs', 'no', 'parens')

Packing and Unpacking

You can also perform multiple assignment using tuples.

>>> article, noun, verb, adjective, direct_object = t

>>> noun

'tuple'

Note that either, or both sides of an assignment operator can consist of tuples.

>>> a, b = 1, 2

>>> b

2

Assigning a tuple to a several different variables is called "tuple unpacking," while assigning multiple values to a

tuple in one variable is called "tuple packing." When unpacking a tuple, or performing multiple assignment, you

must have the same number of variables being assigned to as values being assigned.

Operations on tuples

These are the same as for lists except that we may not assign to indices or slices, and there is no "append" operator.

>>> a = (1, 2)

>>> b = (3, 4)

>>> a + b

(1, 2, 3, 4)

>>> a

(1, 2)

>>> b

(3, 4)

>>> print a.append(3)

Traceback (most recent call last):

File "", line 1, in ?

AttributeError: 'tuple' object has no attribute 'append'

>>> a

(1, 2)

>>> a[0] = 0

Traceback (most recent call last):

File "", line 1, in ?

TypeError: object does not support item assignment

>>> a

(1, 2)

For lists we would have had:

>>> a = [1, 2]

>>> b = [3, 4]

Python Programming/Tuples

79

>>> a + b

[1, 2, 3, 4]

>>> a

[1, 2]

>>> b

[3, 4]

>>> a.append(3)

>>> a

[1, 2, 3]

>>> a[0] = 0

>>> a

[0, 2, 3]

Tuple Attributes

Length: Finding the length of a tuple is the same as with lists; use the built in len() method.

>>> len( ( 1, 2, 3) )

3

>>> a = ( 1, 2, 3, 4 )

>>> len( a )

4

Conversions

Convert list to tuples using the built in tuple() method.

>>> l = [4, 5, 6]

>>> tuple(l)

(4, 5, 6)

Converting a tuple into a list using the built in list() method to cast as a list:

>>> t = (4, 5, 6)

>>> list(t)

[4, 5, 6]

Dictionaries can also be converted to tuples of tuples using the items method of dictionaries:

>>> d = {'a': 1, 'b': 2}

>>> tuple(d.items())

(('a', 1), ('b', 2))

Uses of Tuples

Tuples can be used like lists and are appropriate when a list may be used but the size is known and small. One very

useful situation is returning multiple values from a function. To return multiple values in many other languages

requires creating an object or container of some type, but in Python it is easy:

def func(x,y):

# code to compute a and b

return (a,b)

This can be combined with the unpacking technique above in later code to retrieve both return values:

Python Programming/Tuples

80

(a,b) = func(1,2)

Previous:

Lists

IndexNext: Dictionaries

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->