Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
173 views
in Computer by (51.9k points)
closed by

Write a function that returns the largest element of the list passed as parameter.

1 Answer

+1 vote
by (49.5k points)
selected by
 
Best answer

The function can be written in two ways:

1. Using max() function of the list
2. Using for loop to iterate every element and checking for the maximum value

Program 1:

#Using max() function to find largest number

def largestNum(list1):

l = max(list1)

return l

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]

#Using largestNum function to get the function

max_num = largestNum(list1)

#Printing all the elements for the list

print("The elements of the list",list1)

#Printing the largest num

print("\nThe largest number of the list:",max_num)

OUTPUT:

The elements of the list [1, 2, 3, 4, 5, 6, 7, 8, 9]

The largest number of the list: 9​

Program 2:

​#Without using max() function of the list

def largestNum(list1):

length = len(list1)

num = 0

for i in range(length):

if(i == 0 or list1[i] > num):

num = list1[i]

return num

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]

#Using largestNum function to get the function

max_num = largestNum(list1)

#Printing all the elements for the list

print("The elements of the list",list1)

#Printing the largest num

print("\nThe largest number of the list:",max_num)

OUTPUT:

The elements of the list [1, 2, 3, 4, 5, 6, 7, 8, 9]

The largest number of the list: 9​

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...