Understanding Tuples- Introduction
What is Tuples
A tuple can stores aby group of elements or items. They are same as Lists but only different is that Tuple cannot be editable or we can say they are immutable whereas lists are mutable.
As they are mutable, we cannot do any operation like append (), extend (), insert (), remove (), pop () etc.
There are various ways though which a tuple can be created. Let’s understand this by example:
- Tup1 = () # Empty tuple
- Tup2 = (3) # tuple with one element
- Tup3 = (2,3,4,5,6, ‘dclessons’)
- Tup4 = 1,2,3,4, # tuple with no braces.
- Tuple can also be created by list, Let’s see in below example
List1 = [1,2,3,4]
Tup1 = tuple(List1)
- A tuple can also be created by range() function, lets see in below example
Tup1 = tuple(range(1,6,2))
Accessing Tuple Elements
We can access the tuple element by using either indexing or slicing method. We will see each method that how they are used to access tuple elements.
Lets take a tuple and see how element is accessed by indexing, In this we will provide the position number of element to access that particular element.
Tup1 = (1,2,3,4,5,6,7)
- Now print(Tup) : It will print 1
- Now print(Tup) : It will print 6
- Now print(Tup[-1]) : it will print 100
Slicing a Tuple:
In Slicing, we access or extract some portion of tuple by using below format [start: stop: step size]
Let’s understand this with an example
Tup1 = (1, 2, 3, 4, 5, 6, 7)
print (Tup1 [:]): It will print 1,2,3,4,5,6,7
print (Tup1 [1:4]): It will print 2, 3, 4
print (Tup1 [: 2]): It will print 1,3,5,7
Tuple based Operation
Below are some operation that can be done on tuple. They are stated as:
- How to find Length of tuple
- How to concatenate Tuple
- How to repeat element in tuple
- Membership Operation
- Iteration Operation
There are also some defined functions or modules, that can be used while doing some operation on Tuple.
- Len() : To find length of tuple
- Min(): Return smallest element in tuple
- Max(): Return biggest value in tuple
- Count() : return how many times an element is present in that tuple
- Index(): Return first occurrence of element x
Now in order to find the length of particular Tuple, we should use len () function
Tup1 = (1,2,3,4,5)
print (len (Tup1)): Output: 5
Now if we want to concatenate two tuple, we should use ‘+’ operator.
Tup1 = (1,2,3,4,5)
Tup2 = (6,7,8,9)
Tup3 = Tup1 + Tup2
print(Tup3) : Output : (1,2,3,4,5,6,7,8,9)
In order to search any element from tuple, we should use ‘in ‘or ‘not in’ operator
Tup1 = (1, 2, 3, 4, 5)
Num = 3
Num in Tup1
Output : True
Let’s do some programming to understand basic operation on tuple.
Program1: Write a python program to accept element in form of tuple and add their sum and average.
Here we are using eval() function , which helps to evaluate whether the typed element are a list or tuple , depending upon format of brackets given in typing the elements.
If we type [1,2,3,4,5] it will be considered as List
If we type (1,2,3,4,5) it will be considered as tuple.
Program 2: Write a python program to find first occurrence of element in tuple.
When a tuple is inside tuple, it is called as nested tuple. See below example
Tup = (1, 2,3,4,5, (7, 8))
print (‘nested tuple:’ tup )
Output: Nested tuple = (7, 8)