In this python tutorial, you will learn how to Find Largest Element in an Array with the for loop, built in statements and operators of the python programming language.
How to Find Largest Element in an Array?
Let’s take a look at the source code, here the values are assigned in the code, the built in statements carry out the function.RUN CODE SNIPPET
#Python Program to Find Largest Element in an Array arr = [25, 11, 72, 85, 56]; max = arr; for i in range(0, len(arr)): if(arr[i] > max): max = arr[i]; print("Largest element present in given array: " + str(max));
Largest element present in given array: 85
- At first we declare the variable
arrwith the values
[25, 11, 72, 85, 56], after which we declare the first element of the array with the variable
max = arr
- We declare a
forloop where the range for the variable
(0, len(arr))after which we declare an
ifstatement with the condition
(arr[i] > max), followed by the declaration
max = arr[i]
- In the above code, we compare the elements of the array with the max variable, where if any element is greater than
maxvariable will hold the value of that element
- We declare a
("Largest element present in given array: " + str(max)), where the variable
maxholds the final value.
- For loops are used to loop through an iterable object and perform the same action for each entry.
- The str() function converts values to a string form
- The if statement evaluates whether an expression is true. If a condition is true, the “if” statement is executed otherwise, it moves to the next statement.
- The statement for the input function are enclosed in single quotes and parenthesis.
- The \n in the code indicates a new line or the end of a statement line or a string.
- The print statement is followed by a period, to initiate the format function.
- The print statement/string to be displayed in enclosed in double quotes.