Python Tuples

Python Tuples

In this tutorial, we will see the use of Python Tuples and how they are different from Python Lists. Tuples are data types that can store more than one element just like lists. In another session, we have already seen Python Lists. Lists are variable types which can store one or more than one element, in fact they can have zero element also. In other words, we can say that lists store sequence of elements which can be accessed using indexes. We can also edit the elements of the list by using indexes. Above statement brings us to two important concepts:

Sequence: sequence is a type of data that can store more than one element and can be accesses sequentially using loops

Mutability: mutability means ability to update or edit data in code. Like we can update or add elements in list in code

Immutability: this means that we cannot update the list by adding or deleting elements from it. To update such list, we rather need to re-create the list altogether.

Such a sequence of data elements or list of elements which is immutable by nature is called as tuple.

Difference between List and Tuples:

ListTuple
MutableImmutable
Uses square brackets to enclose elementsUses parenthesis to enclose elements

Syntax of Python Tuples:

Creating a tuple
tuple1 = (1,2) – this tuple contains two elements
tuple2 = (1,2,3,4) – this tuple contains two elements
Remember that the elements should be separated by comma
Print a tuple
tuple = (4,5.6,0,6.5)
print(tuple)
Creating an empty tuple
empty_t = ()
One element tuple
t1 = (1,) or t2=2,
here we have to use a comma after first element to distinguish it from a normal scalar variable. Removing the comma would treat that as a normal variable not as a tuple.

Examples of using Python Tuple:

1. Create and print a tuple

tup1 = (1,2,2.5,)
print(tup1)

2. As we already saw, tuples are immutable, hence trying to modify the contents will throw error

tup1 = (1,2,2.5) 

tup1.append(5) -AttributeError: 'tuple' object has no attribute 'append'
del tup1[0] -TypeError: 'tuple' object doesn't support item deletion
tup1[1]=10 -TypeError: 'tuple' object does not support item assignment

3. We can however delete the whole tuple

tuple = 1, 2, 3, 
del tuple
print(tuple) 
Error: # NameError: name 'my_tuple' is not defined

4. Using len() function to find length of list

tuple1 = ('Python',3.0)
tuple2 = ("Java",1.8,"PHP")
print(len(tuple1))
print(len(tuple2))

5. Joining tuples using ‘+’ operator – this will just concatenate the contents of two list

Example 1:
tup1=(1,2)
tup2=(3,4,5)
tup3=tup1+tup2
print(tup3)
Example 2:
tup1=('Python',3.2)
tup2=tup1+("is",'very','easy')
print(tup2)

6. Remember that we can concatenate a tuple by a tuple only. If we try to concatenate a tuple by an integer, then we will see error:

num1=(1,2,3)
num2=num1+3
print(num2)
TypeError: can only concatenate tuple (not "int") to tuple

7. Multiplying tuples using ‘*’ operator – this will just repeat the tuple contents as many times as we have multiplied by the number

num=(1,2,3)
num1=num * 2
print(num1)

8. Remember that we cannot multiple the tuple with another tuple. A tuple can be multiplied with an integer type value only else we will see error

num1=(1,2,3)
num2=(4,5)
num3=num1 * num2
print(num3)
TypeError: can't multiply sequence by non-int of type 'tuple'

9. Using in and not in operator on tuple just like lists

num1=(1,2,3)
num2=(4,5,6)
print(1 in num1) -True
print(2 not in num1) -False
print(100 in num2) -False
print(5 in num2) -True

10. Assigning one tuple to another tuple – in below example, t1 contents will be copied to t2 and t2 contents will be copied to t1

t1=(1,2)
t2=('Python','Java')
print(t1)
print(t2)
t1,t2=t2,t1
print(t1)
print(t2) 

11. Creating a tuple using tuple method

tup = tuple((1,2,3))
print(tup)
list=[3,4,5]
print(type(list))
tup2=tuple(list)
print(tup2)
print(type(tup2))

12. Creating a list from a tuple

tuple=(1,2,3,4)
list1=list(tuple)
print(type(list1))
print(list1)

13. Assigning tuple elements to variables

tuple = 1, 2, 3
a, b, c = tup
print(a * b * c)

14. Count number of occurrences of an element in tuple

tuple=(1,2,3,1,4,5,3,2,1,3,4)

count_3=tuple.count(3)
print(count_3)

One thought on “Python Tuples

Leave a Reply

Your email address will not be published.