Sei sulla pagina 1di 4

6.

Tuple

In Python programming, a tuple is similar to a list. The difference between the two is that we
cannot change the elements of a tuple once it is assigned whereas in a list, elements can be
changed. Tuples are also comparable and hashable .

Advantages of Tuple over List


 We generally use tuple for heterogeneous (different) datatypes and list for
homogeneous (similar) datatypes.
 Since tuple are immutable, iterating through tuple is faster than with list. So there is
a slight performance boost.
 Tuples that contain immutable elements can be used as key for a dictionary. With
list, this is not possible.
 If you have data that doesn't change, implementing it as tuple will guarantee that it
remains write-protected.

6.1. Creating a Tuple

Syntactically, a tuple is a comma-separated list of values:

>>> t = 'a', 'b', 'c', 'd', 'e'

Although it is not necessary, it is common to enclose tuples in parentheses to help


us quickly identify tuples when we look at Python code:

>>> t = ('a', 'b', 'c', 'd', 'e')

To create a tuple with a single element, you have to include the final comma:

>>> t1 = ('a',)
>>> type(t1)
<type 'tuple'>

Without the comma Python treats ('a') as an expression with a string in


parentheses that evaluates to a string:

>>> t2 = ('a')
>>> type(t2)
<type 'str'>
6.2 Accessing values in Tuples

#!/usr/bin/python

tup1 = ('physics', 'chemistry', 1997, 2000);

tup2 = (1, 2, 3, 4, 5, 6, 7 );

print "tup1[0]: ", tup1[0];

print "tup2[1:5]: ", tup2[1:5];

Result:

tup1[0]: physics
tup2[1:5]: (2, 3, 4, 5)

6.3. Updating Tuples

#!/usr/bin/python

tup1 = (12, 34.56);

tup2 = ('abc', 'xyz');

# Following action is not valid for tuples

# tup1[0] = 100;

# So let's create a new tuple as follows

tup3 = tup1 + tup2;

print tup3;

6.4. Delete Tuple Elements

#!/usr/bin/python

tup = ('physics', 'chemistry', 1997, 2000);

print tup;

del tup;

print "After deleting tup : ";

print tup;
6.5. Tuple Methods

my_tuple = ('a','p','p','l','e',)

# Count

# Output: 2

print(my_tuple.count('p'))

# Index

# Output: 3

print(my_tuple.index('l'))

6.5.1. Tuple Membership Test


my_tuple = ('a','p','p','l','e',)

# In operation

# Output: True

print('a' in my_tuple)

# Output: False

print('b' in my_tuple)

# Not in operation

# Output: True

print('g' not in my_tuple)

6.5.2. Iteration
# Output:

# Hello John

# Hello Kate

for name in ('John','Kate'):

print("Hello",name)

6.5.3 Builin Fumctions


Function Description

all() Return True if all elements of the tuple are true (or if the tuple is empty).

Return True if any element of the tuple is true. If the tuple is empty,
any()
return False.

Return an enumerate object. It contains the index and value of all the items of
enumerate()
tuple as pairs.

len() Return the length (the number of items) in the tuple.

max() Return the largest item in the tuple.

min() Return the smallest item in the tuple

Take elements in the tuple and return a new sorted list (does not sort the
sorted()
tuple itself).

sum() Retrun the sum of all elements in the tuple.

tuple() Convert an iterable (list, string, set, dictionary) to a tuple.

6.5.4 Comparing tuples

Ex:
>>> (0, 1, 2) < (0, 3, 4)
True
>>> (0, 1, 2000000) < (0, 3, 4)
True

Potrebbero piacerti anche