Decode School

Decode School - The School for Problem Solvers
Welcome to My Blog.
Need Assignment / Project Guidance (or) Training in Python?

largest element in the list python | without using built-in function

Python Program to get list size n and n elements of list, then find the largest element among those elements.

Sample Input 1:

5 5 7 9 3 1

Sample Output 1:

9

Program or Solution

					    
							
l=list(map(int,input("Enter array elements:").split(" ")))
max1=l[0]
for i in range(1,len(l)):
    if(l[i]>max1):
        max1=l[i]
print(max1)

        
    

					    
					

Program Explanation

Input: To get list of numbers seprated by space, use split(" ") method.

Split() method splits the numbers as seprate elements. By default this methods are considered as string, since input() method returs string.

Use map() function to convert all elements as integer and store it in list.

Process: assign the element located at 0 to max1 using max1 = l[0].

using for loop visit each location serially from 1 to len(l)-1. if the element located in any position is greater than max1, then assign the element as max by using max1 = l[i] finally max1 holds the maximum value in the list.