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.")
Output

Input: 5 4 3 2 1

Output: Second smallest number: 2

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

Online Exam Quiz for One day Exam Online Typing Test CCC Online Test 2026 Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Best Website and Software Company in Allahabad