Tuples Online
Tuples Online
immutable
A tuple is an ordered sequence of elements of different data types, such as integer, float,
string, list or even a tuple.
Elements of a tuple are enclosed in parenthesis (round brackets) and are separated by
commas.
Like list and string, elements of a tuple can be accessed using index values, starting from
0.
>>> tuple1[1+4]
12
>>> tuple1[0:len(tuple1):2]
(10, 30, 50, 70)
#negative indexing
>>> tuple1[-6:-4]
(30, 40)
#tuple is traversed in reverse order
>>> tuple1[::-1]
(80, 70, 60, 50, 40, 30, 20, 10)
Tuple Methods
and
Built-in Functions
len()
>>> tuple1.index(90)
ValueError: tuple.index(x): x not in
tuple
sorted() : Takes elements in the tuple and returns a new sorted list. It
should be noted that, sorted() does not make any change to the
original tuple
>>>tuple1=("Rama","Heena","Raj","Mohsin","Aditya
")
>>> sorted(tuple1)
['Aditya', 'Heena', 'Mohsin', 'Raj',
'Rama']
min()
Returns minimum or smallest element of the tuple
max()
Returns maximum or largest element of the tuple
sum()
Returns sum of the elements of the tuple
>>> tuple1 = (19,12,56,18,9,87,34)
>>> min(tuple1)
9
>>> max(tuple1)
87
>>> sum(tuple1)
235
Tuple Assignment
• Assignment of tuple is a useful feature in Python.
• It allows a tuple of variables on the left side of the
assignment operator to be assigned respective values
from a tuple on the right side.
• The number of variables on the left should be same as the
number of elements in the tuple.
Tuple Assignment
##The first element 10 is assigned to num1 and the
second element 20 is assigned to num2.
>>> (num1,num2) = (10,20)
>>> print(num1)
10
>>> print(num2)
20
>>> record = ( "Pooja",40,"CS")#packing
>>> (name,rollNo,subject) = record #unpacking
>>> name
'Pooja' >>> (a,b,c,d) = (5,6,8)
>>> rollNo ValueError: not enough
40 values to unpack
>>> subject (expected 4, got 3)
'CS'
Tuple Assignment
If there is an expression on the right side then first that expression is evaluated
and finally the result is assigned to the tuple.
t=(11,21,31,42,51)
t1=list(t)
t1[-2]=41
t=tuple(t1)
print("modified tuple:",t)
Given a tuple T(1,2,”a”,”b”). There is a list L with some elements. Replace first
four elements of list with all four elements of the tuple in single statement.
Print the list before and after the list is modified.
================== RESTART: C:/python37/class 11/tuple/2.py
==================
List before unpacking:
['hello', 'there', 'are', 'some', 'numbers', 'coming', 'from', 'tuples']
List after unpacking:
[1, 2, 'a', 'b', 'numbers', 'coming', 'from', 'tuples']
>>>
Given a tuple T(1,2,”a”,”b”). There is a list L with some elements. Replace first
four elements of list with all four elements of the tuple in single statement.
Print the list before and after the list is modified.
t=(1,2,"a","b")
l=["hello","there","are","some","numbers","coming","from","tuples"]
print("List before unpacking:")
print(l)
l[0],l[1],l[2],l[3]=t
print("List after unpacking:")
print(l)
Tuples are immutable. t=([11,12],[14,15,16])
The following code gives no error when executed. Why? print(t)
Predict the output t[0].append(13)
pritn(t)
Tuples are immutable. t=([11,12],[14,15,16])
The following code gives no error when executed. Why? print(t)
Predict the output t[0].append(13)
print(t)
The mutable elements can always change, thus line 3 of the code
is actually modifying the list without modifying its memory
address
OUTPUT
([11,12],[14,15,16])
([11,12,13],[14,15,16])
WAP to check if a tuple contains any duplicate element
t=eval(input("Enter tuple"))
for ele in t:
if t.count(ele)>1:
print("tuple contains duplicate elements")
break
☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺