Today’s Code Snippet of the Day is a simple Python function to find the largest number in an array:
Code:
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[0]
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)
Output:
7
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, 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
Latest posts by Tanmay Sinha (see all)
- What If JavaScript Never Existed? - January 10, 2025
- 10 Web Development Projects with (Source Code) - January 6, 2025
- Is HTML a Programming Language? The Answer May Surprise You - January 5, 2025
Pingback: JavaScript Program to Check if a Number is Odd or Even | CSOTD #3 - Mr Programmer