Class 12: Passing Tuple to the Function

Example 1:
Python script to pass a tuple to the function and display its elements.
Solution:

#function definition
def tup(t):
    for i in t:
        print (i)

#function calling
t=(1,2,3,4,5)
tup(t) 
#Output:

1
2
3
4
5
>>> 

Example 2:
Python script to pass a tuple to the function and to add an element to it.
Solution:

#function definition
def tup(t):
    t=t+(6,)
    print(t)

#function calling
t=(1,2,3,4,5)
tup(t)
# Output
(1, 2, 3, 4, 5, 6)
>>> 

Example 3:
Python script to pass a tuple to the function and display the largest element.
Solution:

#function definition
def tup(t):
    m=t[0]
    for i in t:
        if(i>m):
            m=i
    print('largest element= ',m)

#function calling
t=(23,45,12,56,78,3,90)
tup(t)
# Output:
largest element=  90
>>> 

Example 4:
Python script to pass a tuple to the function and display the lowest element.
Solution:

#function definition
def tup(t):
    m=t[0]
    for i in t:
        if(i<m):
            m=i
    print('lowest element= ',m)

#function calling
t=(23,45,12,56,78,3,90)
tup(t)
# Output:
lowest element=  3
>>> 

Example 5:
Python script to pass a tuple to the function and to count the specied element.
Solution:

#function definition
def tup(t):
    e=int(input('enter the element : '))
    c=t.count(e)
    print('number of ',e,' in tuple = ',c)

#function calling
t=(1,2,5,3,2,6,4,2,7,8,2,9,1,2)
tup(t)
#Output:

enter the element : 2
number of  2  in tuple =  5
>>>