You are on page 1of 13

CLASS 10

ADVANCE PYTHON

TUPLE
• Tuple is a collection of elements which is ordered
and unchangeable (Immutable). Immutable
means you cannot change elements of a tuple in
place.
• Consists the values of any type, separated by
Tuple comma.
• Allows duplicate members.
• Tuples are enclosed within parentheses ( ).
• Cannot remove the element from a tuple.
• Eg:
T1=(45,23,33.5,'hello',40,'world')
print(T1)
ACCESSING TUPLES
• Tuples are very much similar to lists.
• Tuple elements are also indexed.
• Forward indexing as 0,1,2,3,4………
• backward indexing as -1,-2,-3,-4,………
• The values stored in a tuple can be accessed using the slice operator ([:]) with
indexes.
Eg: Tuple_name[start:end] will give you elements between start to end-1.
• The first item in the tuple has the index zero (0).

Example:
>>> text=(‘c’,’o’,’m’,’p’,’u’,’t’,’e’,’r’)
TUPLE METHODS
Function name Description

len( ) Find the length of a tuple

max( ) Returns the largest value from a tuple.

min( ) Returns the smallest value from a tuple.

count( ) Return the number of times the value appears.

Index( ) index() method finds the first occurrence of the specified


value. The index() method raises an exception if the value
is not found.
• DELETE A TUPLE

• The del statement is used to delete elements and objects but as you
know that tuples are immutable, which also means that individual
element of a tuple cannot be deleted.
T=(2,4,6,8,10,12,14)
del(t[3]) #error as tuple is immutable
Examples
#create tuple
T1=(45,23,33.5,'hello',40,'world')
print(T1)

#Accessing values
text=(2,4,8,9,3,'abc',12)
print("Value",text[4])
print("Value",text[2:5])
print("Value",text[-3])
print("Value",text[-5:-1])
#Operations
t1=(4,6,9,2)
t2=(2,3,6,7)
print(t1+t2)
print(t1*3)

#Find the length of a tuple


company=("IBM","WIPRO","TCS","CTS")
print("Length:",len(company))
#Length, Maximum value ,minimum value, count, Index
tp=(33,55,12,24,31,9,40)
print("Length:",len(tp))
print("Largets value:",max(tp))
print("Smallest value:",min(tp))
print("Number of times occuring:",tp.count(12))
print("Index:",tp.index(12))

#can’t delete values


p=(45,22,34,21,20)
print(p)
del(p[3])
del(p)
print(p)
#find the sum and average
text=(10,20,30,40,50)

s=0

for i in text:

s=s+i

print("Sum:",s)

print("Average=",s/len(text))
# Write a program to create two tuples with different values and
combine both the tuple and make a new tuple.
t1=(34,25,46,65)
t2=(12,16,35,56)
t3= t1+t2
print(t3)
PRACTICE QUESTIONS ON TUPLE
1. Create a tuple with given values
Tup = (10, 20, 30, 40, 50)
• Access value 20 from the above tuple
• Access value from 1st index to 4th index from Tup.
2. Count the number of occurrences of item 50 in tuple1
tuple1 = (50, 10, 60, 70, 50)
3. Write a python program to create a tuple with different values and count the number of items in the tuple.

4. Write a program to create a tuple ‘Marks’ with the marks of a student in five subjects. Print the maximum and
minimum marks.
5. WAP to create a tuple with values t1= (102, 230, 385, 438, 505).

• Do the following using tuple methods


a. Find the maximum value of t1
b. Find the minimum value of t1.
c. Access the value from the index number 1 to 4.
d. Find the sum of values of t1 using for loop.
THANK YOU

You might also like