Techno Blender
Digitally Yours.

Python Program to Find Largest Item in a Tuple

0 29


Given a tuple, the task is to write a Python program to find the greatest number in a Tuple.

Example:

Input: (10,20,23,5,2,90) #tuple
Output: 90

Values of a tuple are syntactically separated by ‘commas’. Although it is not necessary, it is more common to define a tuple by closing the sequence of values in parentheses. This helps in understanding the Python tuples more easily.

Python program to demonstrate the maximum element in a Tuple.

Method 1: Using the max() method

We can use Python built-in max() Method to find the maximum element in a tuple.

Python3

print("Enter number separated by comma:")

t1 = tuple([int(e) for e in input().split(',')])

print("Greatest number in the tuple is:", max(t1))

Output:

Enter number separated by comma:
10,20,23,5,2,80
Greatest number in the tuple is: 80

Method 2: Using for-loop

Here we iterate on the for-loop and compare each element to find the maximum element in the tuple.

Python3

t = (25, 17, 55, 63, 40)

  

max_val = t[0]

  

for i in range(len(t)):

    if t[i] > max_val:

        max_val = t[i]

  

print("Maximum value is:", max_val)

Output:

Maximum value is: 63

Method 3: Using Recursive Functions

Here, we have used a recursive function to find the maximum element in the tuple.

Python3

def tupleLargest(num_tuple, index=0, max_item=float("-inf")):

    

    if index == len(num_tuple):

        return max_item

        

    

    current_item = num_tuple[index]

      

    

    if current_item > max_item:

        max_item = current_item

          

    

    return tupleLargest(num_tuple, index + 1, max_item)

  

  

if __name__ == '__main__':

    maxTuple = (11, 65, 54, 23, 76, 33, 82, 98)

    print("Tuple Items = ", maxTuple)

  

    largest_element = tupleLargest(maxTuple)

    print("Maximum Item in Tuple = ", largest_element)

Output:

Tuple Items =  (11, 65, 54, 23, 76, 33, 82, 98)

Maximum Item in Tuple =  98


Given a tuple, the task is to write a Python program to find the greatest number in a Tuple.

Example:

Input: (10,20,23,5,2,90) #tuple
Output: 90

Values of a tuple are syntactically separated by ‘commas’. Although it is not necessary, it is more common to define a tuple by closing the sequence of values in parentheses. This helps in understanding the Python tuples more easily.

Python program to demonstrate the maximum element in a Tuple.

Method 1: Using the max() method

We can use Python built-in max() Method to find the maximum element in a tuple.

Python3

print("Enter number separated by comma:")

t1 = tuple([int(e) for e in input().split(',')])

print("Greatest number in the tuple is:", max(t1))

Output:

Enter number separated by comma:
10,20,23,5,2,80
Greatest number in the tuple is: 80

Method 2: Using for-loop

Here we iterate on the for-loop and compare each element to find the maximum element in the tuple.

Python3

t = (25, 17, 55, 63, 40)

  

max_val = t[0]

  

for i in range(len(t)):

    if t[i] > max_val:

        max_val = t[i]

  

print("Maximum value is:", max_val)

Output:

Maximum value is: 63

Method 3: Using Recursive Functions

Here, we have used a recursive function to find the maximum element in the tuple.

Python3

def tupleLargest(num_tuple, index=0, max_item=float("-inf")):

    

    if index == len(num_tuple):

        return max_item

        

    

    current_item = num_tuple[index]

      

    

    if current_item > max_item:

        max_item = current_item

          

    

    return tupleLargest(num_tuple, index + 1, max_item)

  

  

if __name__ == '__main__':

    maxTuple = (11, 65, 54, 23, 76, 33, 82, 98)

    print("Tuple Items = ", maxTuple)

  

    largest_element = tupleLargest(maxTuple)

    print("Maximum Item in Tuple = ", largest_element)

Output:

Tuple Items =  (11, 65, 54, 23, 76, 33, 82, 98)

Maximum Item in Tuple =  98

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.
Leave a comment