This Python program generates and displays all four-digit numbers that are made using only the digits 0, 1, and 4, with no repeated digits.
Each digit must appear at most once, and the number must be exactly four digits long.
Numbers starting with zero are excluded to ensure valid four-digit formatting.
from itertools import permutations
# Allowed digits
digits = ['0', '1', '4']
print("Valid four-digit numbers using digits 0, 1, and 4:")
# Generate all 4-digit permutations using the digits
for p in permutations(digits, 4):
number = ''.join(p)
if number[0] != '0':
print(number)
1040
1401
4010
4101
...
- permutations(digits, 4) generates all 4-digit arrangements without repetition
- number[0] != '0' ensures the number does not start with zero
- ''.join(p) converts the digit tuple into a string
- Only valid four-digit numbers are printed