Skip to content

Instantly share code, notes, and snippets.

@RaMSFT
Created October 12, 2022 05:00
Show Gist options
  • Select an option

  • Save RaMSFT/9226f84780b1c6c9880b5bb7d12e8009 to your computer and use it in GitHub Desktop.

Select an option

Save RaMSFT/9226f84780b1c6c9880b5bb7d12e8009 to your computer and use it in GitHub Desktop.
def find_largest_num_using_for(lst):
"""Find the largest number from the list using for loop
Args:
lst (Numeric): a list of numbers
Returns:
Numeric: return the largest of all numbers in the list
"""
# Initiate a variable largest with first element of list
largest = lst[0]
#loop through the list
for val in lst:
#when any element of the list is smaller then the largest, re-assign largest with that value
if val > largest:
largest = val
#After loop is complete return the value
return largest
print(find_largest_num_using_for([34, 15, 88, 2])) # 80
print(find_largest_num_using_for([34, -345, -1, 100])) # 100
print(find_largest_num_using_for([-76, 1.345, 1, 0])) # 1.345
print(find_largest_num_using_for([0.4356, 0.8795, 0.5435, -0.9999])) # 0.8795
print(find_largest_num_using_for([7, 7, 7])) # 7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment