Today’s Code Snippet of the Day is a simple Python function to find the largest number in an array:
def find_largest_number(array): """Finds the largest number in an array. Args: array: A list of numbers. Returns: The largest number in the array. """ largest_number = array for number in array: if number > largest_number: largest_number = number return largest_number # Example usage: array = [1, 5, 3, 7, 2] largest_number = find_largest_number(array) print(largest_number)
In the above example, we have first defined a function in Python, in which there is a variable named
largest_number is defined which triggers the first element in the array. Then it iterates over the array and compares each element present in the array, If any element is greater than the
largest_number variable is updated to that element. Finally, the function returns the
largest_number variable and displays the greatest number present in the array.
Also Read: Reverse a String in Python CSOTD #1