Python program to find smallest number in a list using for loop

Here’s a example code that demonstrates how to find the smallest number in a list using a for loop :-

# Objective: Finding smallest number in a list using for loop
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Initializing the smallest number
smallest = numbers[0]

# Iterating through the list
for num in numbers:
    if num < smallest:
        smallest = num

print("Smallest number:", smallest) # Output: Smallest number: 1

This program uses a for loop to iterate through the list of numbers, and compares each number to the current smallest value stored in the variable smallest. If the current number is smaller than the smallest value, the smallest value is updated. At the end of the loop, the final value of smallest will be the smallest value in the list.

Leave a Comment