Find out the Largest and Smallest Number (separately) in a List
# largest number in a list
largest_num = None
numbers = [9, 22, 3, 566, 43, 432, 34, 233, 123, 2223, 67, 3030, 5]
for number in numbers:
if largest_num is None:
largest_num = number
elif number > largest_num:
largest_num = number
print("Largest number is", largest_num)
# Smallest number in a list
smallest_num = None
numbers = [9, 22, 3, 566, 43, 432, 34, 233, 123, 2223, 67, 3030, 5]
for number in numbers:
if smallest_num is None:
smallest_num = number
elif number < smallest_num:
smallest_num = number
print("Smallest number is", smallest_num)
Comments
Post a Comment
Write something to CodeWithAbdur!