Programming Examples

Create a Python program that finds the second smallest number in a list of positive integers (including zero).


Create a Python program that finds the second smallest number in a list of positive integers (including zero).

If input is 5 4 3 2 1, then output should be 2.

If input is 0 0 0 0 1, then output should be 1.

If input is 1 11, then output should be 11.

Solution

nums = list(map(int, input("Enter numbers separated by space: ").split()))

# Remove duplicates and sort
unique_nums = sorted(set(nums))

# Get the second smallest number
if len(unique_nums) >= 2:
    print("Second smallest number:", unique_nums[1])
else:
    print("Not enough unique numbers to find the second smallest.")
▶ RUN

Output/ Explanation:

Input: 5 4 3 2 1

Output: Second smallest number: 2

Input: 0 0 0 0 1
Output: Second smallest number: 1

Latest Current Affairs 2026 Online Exam Quiz for One day Exam Online Typing Test CCC Online Test Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Online MBA 2 years Online MCA Online BCA Best Website and Software Company in Allahabad