Wednesday, October 4, 2023
HomeProgrammingPythonPython 3 Program To Find Largest Element In An Array or List

Python 3 Program To Find Largest Element In An Array or List

This post was last Updated on June 16, 2023 by Himanshu Tyagi to reflect the accuracy and up-to-date information on the page.

In this tutorial, you will learn a simple Python 3 program to find the largest element in an array or list using functions. Before proceeding further, you must know Python programming basics, such as how to use variables, create functions, etc. Check out these recommended books if you have just begun your Python journey.

Also ReadSending Emails Using Python With Image And PDF Attachments.

Python 3 program to find the largest element in an array or list

To find the largest element in an array, we will create a function called largestFun() with two arguments. The first argument will take the array, and the second will take the array’s length (for traversing the array).

Inside the function, we will initialize a variable named maxNum to store the maximum value or the largest element of the passed array. Next, we will use the Python For loop to traverse the array and compare every array element with the value stored in the variable.

Once the array is traversed completely, maxNum will store the value of the largest element. We will return the maxNum value from the function. We will initialize an array with some integer values and find its length using the len() function.

In the end, we will call the largestFun() and store the value returned in the largestElement variable and print its value to display the maximum number in an array or list.

Please refer to the code below, and if you have any doubts, then share them in the comments so I can assist you.

Source Code:

def largestFun(arr,n):      #function to find the largest element in an array with two arguements - array and size of array.
    maxNum = arr[0]         #maximum element initialized
    for i in range(1, n):   #Traverse through the array to find the largest element
        if arr[i] > maxNum: 
            maxNum = arr[i] 
    return maxNum
  
arr = [13430, 33424, 45000, 56390, 938408]   #Array intialized
n = len(arr)                                 #Finding the length of array
largestElement = largestFun(arr,n)           #Calling the function to find the largest element in the array. 
print ("Largest element in the given array is",largestElement) #Printing output.

Program Output

Python Program To Find The Largest Element In Array Using function

Run The Program

Download the Program as PDF.

Download the Source Code.

Related Python Programs

  1. Python 3 Program To Add Two Matrices.
  2. Python 3 Program to Check Armstrong Number.
  3. Python 3 Program to Find the Sum of Natural Numbers.
  4. Python 3 Program To Find The Factorial Of A Number.
  5. Python 3 Program to Find Largest Among Three Numbers.
  6. Python 3 Program to Check Leap Year.
  7. Python 3 Program To Add Two Numbers.
  8. Python 3 Program to Find the Square Root of A Number.
  9. Python 3 Program To Solve A Quadratic Equation.
  10. Python 3 Program to Calculate The Area of a Triangle.
  11. Python 3 Program To Check If Number Is Positive Or Negative.
  12. Python 3 Program To Convert Celsius To Fahrenheit.
  13. Python 3 Program to Convert Kilometres to Miles.
  14. Python 3 Program to Generate A Random Number.
  15. Python 3 Program To Swap Two Numbers.
Himanshu Tyagi
Himanshu Tyagi
Hello Friends! I am Himanshu, a hobbyist programmer, tech enthusiast, and digital content creator. Founder Cool SaaS Hunter. With CodeItBro, my mission is to promote coding and help people from non-tech backgrounds to learn this modern-age skill!
RELATED ARTICLES