Python Programming – Tuple

You can learn about Tuples in Python Programs with Outputs helped you to understand the language better.

Python Programming – Tuple

Tuple

A tuple is a compound data type in Python. It is a sequence of multiple values in an ordered sequence. Tuples are very similar to lists, but tuples cannot be changed. In other words, the tuple can be viewed as a “constant list”. While lists use square brackets[ ], tuples are written with standard parentheses( ) or no parentheses, and elements are separated with comma as given below:

# elements written without parentheses
>>> t1 = ‘a’, ‘b’, ‘c’, ‘d’, ‘ e’# elements written within parentheses
>>> t2 = (‘a’, ‘b’, ‘c’, ‘d’, ‘e’)# tuple of numbers
>>> tuplel = (0, 1, 2, 3)# tuple of mixed numbers
>>> tuple2 = (33, 3.3, 3+3j)# tuple of mixed data types
>>> Tl = (101, “Aman”, 77])

# tuple of srtings
>>> T2 = (“Apple”, “Banana”, “Orange”)

# a tuple containing a single element or item
>>> t3 = ‘a’,
>>> type(t3)
<type ‘class’>

# An empty tuple
>>> empty_tup = ( )
( )

a. Syntactically, the tuple is a comma-separated (e) list of values. For example,
>>> tl = ‘a’, ‘b’, ‘C, ‘d’, ‘e’

b. Although parentheses is not necessary, it is common to enclose tuples in parentheses. For example,
>>> t2 = (‘a’, ‘b’, ‘c’, ‘d’, ‘e’)

c. To create a tuple with a single element/item, you have to include the final comma after the item:
>>> t3 = ‘a’, <– note trailing comma

Unlike Python Strings, Tuple allows you to store dif¬ferent types of data such as integer, float, string, etc.

Difference between List and Tuple

Differences between lists and tuples are as follows:

(a) Lists are enclosed in square brackets [ ], and their elements and size can be changed, while tuples are enclosed in parentheses ( ) and cannot be updated.

(b) Tuples can be thought of as read-only lists. They protect against accidental changes of their con¬tents whereas lists can grow and shrink as needed. In other words, the list has the variable length and the tuple has the fixed length.

(c) Tuples are immutable like strings, i.e., you cannot modify them. However, lists are mutable; individual items or entire slices can be replaced through assignment statements.

(d) Tuples can hold both homogeneous as well as heterogeneous values. You generally use tuple for heterogeneous (different) datatypes and list for homogeneous (similar) datatypes.

(e) Tuples are faster than lists. Iterating over the elements of a tuple is faster compared to iterating over a list.

Tuples are Immutable

Tuples are immutable which means you cannot update them or change values of their elements. A new object has to be created if a different value has to be stored. You can take portions of an existing tuple to create a new tuple as given below:
>>> tup1 = (1, 2, 3)
>>> tup2 = (5, 6, 7)
>>> tup1 (1, 2, 3)
>>> tup2 (5, 6, 7)
>>> tup2 [0] = 4 # action is not valid for tuples, error will be displayed
Traceback (most recent call last):
File “<pyshell#15>”, line 1, in <module>
tup2[0] =4
TypeError: ‘tuple’ object does not support item assignment So, let’s create the new tuple as given below:
>>> tup3 = tupl + tup2 >>> tup3
(1, 2, 3, 5, 6, 7)
>>>
Using the addition operator(+), with two or more tuples, adds up all the elements into a new tuple.

  • Tuples respond to the + and * operators much like strings since they mean concatenation and repetition operators, respectively, except that the return value is a new tuple, not a string.

Let’s Try

# Zero-element tuple.
a = ( )
# one-element tuple.
b = (“one”,)
#Two-element tuple.
c = (“one” , “two”)
print ( a )
print ( b )
print ( c )
tup1 = (10,20,30)
tup2 = (40,50,60)
tup3 = tup1 + tup2
print (“The third tuple elements:” ,tup3)

Packing and unpacking Tuples

You can also create a Python tuple without parentheses. This is called tuple packing. Multiple assignments can be performed using tuples. Let us take an example:
>>> lyrics, songs, music, videos, obj = o
# o is already defined
>>> songs
‘tuple’
Note that either or both sides of an assignment operator (=) consist of tuples. For example,
>>> i, j = 11, 12
>>> j
12
In the above example, lyrics, songs, music, videos, obj = o is called “tuple unpacking” because the tuple o was unpacked, and its values were assigned to each of the variables on the left. The concept of “Tuple packing” is considered as the reverse process,
i. e., o = lyrics, songs, music, videos, obj. Python tuples unpacking is when you assign values from a tuple to a sequence of variables in python. While unpacking the tuple, or performing multiple assign¬ments, you must have the same number of variables ‘
being assigned to values being assigned. Let us discuss more examples of packing and unpacking of tuples. In packing, you place values into a new tuple. And in unpacking, you extract those values back into variables i.e., tuples items value assigned to variables.
# Python Tuples Packing
>>> b= 1, 2.0, ‘three’
# Python Tuples Unpacking
>>> percentages=(99,95,90,89)
>>> a,b,c,d=percentages
>>> b
95

  • Python tuple unpacking is when you assign values from a tuple to a sequence of variables in python.

Let’s Try

Type the following Python codes and write the output:

fruitlist = (“Apple”, “Mango”, “Ba¬nana”, “Grapes”)
app, man, ban, gra =fruitlist
print(app)
print(man)
print(ban)
print(gra)

Note that Tuple’s items have been assigned to app, man, ban, and gra variables correspondingly.

Leave a Reply

Your email address will not be published. Required fields are marked *