Python Tuple Datatypes

aimtocode

DataType Definition

Data types are means to identify the type of data and associated operations for handling it. In python, we need not to define the type of the variable while declaring it. The interpreter implicitly binds the value with its type accordingly.

Data types represent a kind of value which determines what operations can be performed on that given data.


Tuple

A tuple is a sequence of immutable Python objects.

Tuples are sequences, just like lists.

The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses (), whereas lists uses square brackets [].

Creating Tuples

Similar to List, Tuple is a sequence of values. The values can be any type, and they are indexed by integers.

The important difference is that tuples are immutable. Syntactically, a tuple is a comma-separated list of values:

 >>>message= 'h','a','i'
 >>> type(message)
 <type 'tuple'>

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

 >>> message= ('h','a','i')
 >>> type(message)
 <type 'tuple'>

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

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

A value in parentheses is not a tuple:

 >>> t2 = ('a')
 >>> type(t2)
 <class 'str'>

Another way to create a tuple is the built-in function tuple. With no argument, it creates an empty tuple:

 >>> t = tuple()
 >>> t ()

If the argument is a sequence (string, list or tuple), the result is a tuple with the elements of the sequence:

 >>> course=tuple('python')
 >>> course
 ('p', 'y', 't', 'h', 'o', 'n')

Because tuple is the name of a built-in function, you should avoid using it as a variable name.

Most list operators also work on tuples. The square bracket operator indexes an element:

 >>> course = ('p', 'y', 't', 'h', 'o', 'n')
 >>> cource[0] 'p'

Slice Operator

And the slice operator selects a range of elements.

 >>> course[3:5] ('h', 'o')

But if you try to modify one of the elements of the tuple, you get an error:

 >>> course[0]='P'

Output:

 TypeError: 'tuple' object does not support item assignment

Because tuples are immutable, you can‘t modify the elements. But you can replace one tuple with another:

 >>> course=('P',)+course[1:]
 >>> course
 ('P', 'y', 't', 'h', 'o', 'n')

This statement makes a new tuple and then makes course refer to it. The relational operators work with tuples and other sequences; Python starts by comparing the first element from each sequence. If they are equal, it goes on to the next elements, and so on, until it finds elements that differ. Subsequent elements are not considered, even if they are really big.

Tuple Assignment

It is often useful to swap the values of two variables. With conventional assignments, you have to use a temporary variable. For example, to swap a and b:

 >>> temp = a
 >>> a = b
 >>> b = temp

This solution is cumbersome; tuple assignment is more elegant:

 >>> a, b = b, a	
 

The left side is a tuple of variables; the right side is a tuple of expressions.

Each value is assigned to its respective variable. All the expressions on the right side are evaluated before any of the assignments.

The number of variables on the left and the number of values on the right have to be the same:

 >>> a, b = 1, 2, 3

 ValueError: too many values to unpack

More generally, the right side can be any kind of sequence (string, list or tuple). For example, to split an email address into a user name and a domain, you could write:

>>> email='support@aimtocode.com'
>>> username,domain=email.split('@')

The return value from split is a list with two elements; the first element is assigned to username, the second to domain.

 >>> username 'support'
 >>> domain 'aimtocode.com'

Tuples as return Values

Strictly speaking, a function can only return one value, but if the value is a tuple, the effect is the same as returning multiple values.

For example, if you want to divide two integers and compute the quotient and remainder, it is inefficient to compute x/y and then x%y. It is better to compute them both at the same time.

The built-in function divmod takes two arguments and returns a tuple of two values, the quotient and remainder. You can store the result as a tuple:

 >>> t = divmod(7, 3)
 >>> t (2, 1)

Here is an example of a function that returns a tuple:

 def min_max(t):
 return min(t), max(t)

max and min are built-in functions that find the largest and smallest elements of a sequence. min_max computes both and returns a tuple of two values.

 >>> quot 2
 >>> rem 1


Read Also:


aimtocode