max() and min(): These functions are used to calculate the max and minimum element from the tuple.
t=(1,2,3)
print(max(t)) # 3
print(min(t)) # 1
sum() function: This function is used to calculate the sum of all the elements of the tuple
t=(1,2,3)
print(sum(t)) # 6
len() : This function is used to calculate the length of the tuple.
t=[1,2,3,3,4]
print(len(t)) # 5
Accessing element of tuple using for loop:
We can access elements of tuple using for and while loop.
t=(1,2,3)
for x in t:
print(x)
Accessing elements of tuple using while loop:
t=(1,2,4,4)
l=len(t)
i=0
while i<l:
print(t[i])
i+=1
Taking tuple from user:
t=tuple([eval(x) for x in input().split()])
Note: If we are giving string input here we have to use quotes in the input as well.
If we wan to store only integer in the tuple then we can do like this.
t=tuple([int(x) for x in input().split()])
print(t)
We can also use map() function for this purpose.
t=tuple(map(int,input().split()))
print(t)
t=(1,2,3)
print(max(t)) # 3
print(min(t)) # 1
sum() function: This function is used to calculate the sum of all the elements of the tuple
t=(1,2,3)
print(sum(t)) # 6
len() : This function is used to calculate the length of the tuple.
t=[1,2,3,3,4]
print(len(t)) # 5
Accessing element of tuple using for loop:
We can access elements of tuple using for and while loop.
t=(1,2,3)
for x in t:
print(x)
Accessing elements of tuple using while loop:
t=(1,2,4,4)
l=len(t)
i=0
while i<l:
print(t[i])
i+=1
t=tuple([eval(x) for x in input().split()])
Note: If we are giving string input here we have to use quotes in the input as well.
If we wan to store only integer in the tuple then we can do like this.
t=tuple([int(x) for x in input().split()])
print(t)
We can also use map() function for this purpose.
t=tuple(map(int,input().split()))
print(t)
Be First to Post Comment !
Post a Comment