Table of Contents

Python Code to return the largest and smallest element in a list

 

Python code implementation without user defined functions & classes

Code: 
a = [401, 109, 74, 107, 123409, -82, 64, 309, 511, 1024, 0, 11111]

max_element_in_list = max(a)
min_element_in_list = min(a)
print(max_element_in_list)
print(min_element_in_list)

 

Output: 
123409
-82
Python Code Editor Online - Click to Expand

Python code implementation using function

In this code we will take a list containing integer numbers. We will pass the list to a function that will return the largest and smallest element in a list.

Code: 
def max_element(data):
    return max(data)


def min_element(data):
    return min(data)


a = [401, 109, 74, 107, 123409, -82, 64, 309, 511, 1024, 0, 11111]

max_element_in_list = max_element(a)
min_element_in_list = min_element(a)
print(max_element_in_list)
print(min_element_in_list)

 

Output: 
123409
-82
Python Code Editor Online - Click to Expand

Python code implementation using Classes

In this code we will take a list containing integer numbers. We will implement a class that contain function to find max and min element in a list. We will create an object and will call the function using object.function(), the function will return the max and min element.

Code: 
class Find_max_min_element(object):

    def __init__(self, data):
        self.data = data

    def max_element(self):
        return max(self.data)

    def min_element(self):
        return min(self.data)


a = [401, 109, 74, 107, 123409, -82, 64, 309, 511, 1024, 0, 11111]

# Create an object
max_min = Find_max_min_element(a)

max_element_in_list = max_min.max_element()
min_element_in_list = max_min.min_element()
print(max_element_in_list)
print(min_element_in_list)

 

Output: 
123409
-82
Python Code Editor Online - Click to Expand

 

Enjoy Python Code By Pythonbaba 🙂