Tuples
Tuples
Similar to list, data type tuple is an ordered sequence of one or more items or elements of
different types such as integer, float, string, list or even a tuple.
Elements of a tuple are enclosed in round brackets and are separated by comma.
Like list and string, elements of a tuple can be accessed using index values, starting from 0.
The only difference between list and tuple is that while list is mutable, tuple is immutable.
For Example:
#tuple1 is the tuple of integers
>>> tuple1 = (1,2,3,4,5)
>>> tuple1
(1, 2, 3, 4, 5)
We can also create a new tuple which contains the result of this concatenation
operation.
>>> tuple3 = ('Red','Green','Blue')
>>> tuple4 = ('Cyan', 'Magenta', 'Yellow' ,'Black')
>>> tuple5 = tuple3 + tuple4
>>> tuple5
('Red','Green','Blue','Cyan','Magenta','Yellow','Black')
Tuple Operations: Repetition
We can repeat a tuple elements. The repetition operator requires the first operand
to be a tuple and second operand to be integer only.
>>> tuple1 = ('Hello',)
>>> tuple1 * 4 #replicates tuple1 four times
('Hello', 'Hello', 'Hello', 'Hello')
The ‘not in’ operator returns ‘True’ if the element is not present in the tuple else it
returns ‘False’.
>>> tuple1 = ('Red','Green','Blue')
>>>'Green' not in tuple1
False
Tuple Operations: Slicing
Like string and list, slicing can be applied to tuples also.
>>> tuple1 = (10,20,30,40,50,60,70,80) #tuple1 is a tuple
>>> tuple1[2:7] #elements from index 2 to index 6
(30, 40, 50, 60, 70)
For Example:
>>> (num1,num2) = (10,20)
>>> print(num1)
10
>>> print(num2)
20
OUTPUT:
S_No Roll_No Name Marks
1 101 Aman 98
2 102 Geet 95
3 103 Sahil 87
4 104 Pawan 79
LIST VS TUPLE
✔Lists and Tuples both are sequences and can have elements of any type
✔Both lists and tuples maintain the order of the elements (unlike dictionaries and
sets).
✔We generally use lists to store elements of the same data type and tuples to store
elements of different data types.
✔The main difference between lists and tuples is that lists are mutable sequences
(can be changed) and tuples are immutable sequences (cannot be changed). Lists
can grow or shrink while tuples cannot.
✔If we have data that does not change then storing this data in a tuple will make sure
that it is not changed accidentally.
✔As we know tuples are immutable, iterating through tuple is faster as compare to
lists.
Q1. Write a program to swap two numbers without using a
temporary variable.
Print another tupple whose values are even number in the given tuple.
Concatenate tuple t2={11,13,15} with t1.
Return maximum and minimum value from this tuple.
◆ STEPS-
Firstly we will create a for loop which will check whether a element in
tuple is even or not by using (i%2==0) condition and then place them in
another tuple.
Then we will use + operator for concatenating following tuples.
And for finding minimum and maximum element in t1 tuple we will use
min() and max() built in functions.
CODE :-
OUTPUT :-
DRY RUN
t1=(1,2,5,7,9,2,4,6,8,10)
s=len(t1)
mid=int(s/2)
print(t1[0:mid])
print(t1[mid:])
empty_tuple=()
i=0
while(i<s):
if(t1[i]%2==0):
empty_tuple+=(t1[i],)
i=i+1
print("new tuple with even elements\n",empty_tuple)
t2=(11,13,15)
print("adding t1 and t2=",t1+t2)
print("largest element in t1 is:",max(t1))
print("smallest element in t1 is:",min(t1))
The tuple t is defined as:
t = ("Ram","Shyam",[40,38])
Give the output/ indicate error in each of the following code snippets:
Ans:
(i) Tuples are unchangeable or immutable so to change values inside we need to convert it
to a list then change the values then again change it back to tuple.
(ii) On the other hand, mutable objects stored in a tuple do not lose their mutability e.g.
you can still modify inner lists.
Code: output:
Q6. Write a function that takes the lengths of three sides:
side1, side2 and side3 of the triangle
as the input from the user using input function and
return the area and perimeter of the triangle as a tuple.
Also, assert that
sum of the length of any two sides is greater than the third side.
Write a statement using a single operator that creates another tuple namesX
that adds the name "Lakhan" to the tuple names.