Check an Armstrong Number in Python
153 = 1³ + 5³ + 3³. Raise each digit to the number of digits and see if the total matches.
- What an Armstrong number is
- Counting digits with len(str(n))
- Summing powered digits
1 ** 315 ** 31253 ** 327total153 = n → TrueEach digit to the power of the digit count
The problem
Armstrong numbers are a classic exercise because they combine several basic skills in one short program: splitting a number into digits, counting them and raising each to a power. Getting it right shows you can chain small steps together.
Think of it like this
Checking an Armstrong number is a three-step test on the number itself. Split 153 into its digits: 1, 5 and 3. Because there are three digits, raise each one to the power 3: 1, 125 and 27. Add them up, and if you land back on 153, it is an Armstrong number.
digits✂️ Split the number into digits: [1, 5, 3]len(digits)🔢 How many digits: 3digit ** len(digits)📈 Raise each digit to that power: 1, 125, 27total == number⚖️ Do they add up to 153? YesAn Armstrong number equals the sum of its digits, each raised to the power of the number of digits, such as 153 = 1³ + 5³ + 3³.
Key points
[int(digit) for digit in str(number)]turns 153 into the list[1, 5, 3].len(digits)is the power to use: 3 for a three-digit number.sum(digit ** len(digits) for digit in digits)adds 1 + 125 + 27 = 153.resultis"is"whentotal == number, otherwise"is not".
When should you use it?
- Practicing digit extraction and powers in one exercise.
- Searching for all Armstrong numbers in a range with a loop.
- Classic exam and interview questions.
When not to use it
- Using a fixed power of 3 for every number: the power must match the digit count, so 9474 uses 4.
- Very large ranges: checking every number is slow, and there are only 88 Armstrong numbers in base 10.
Example code
number = 153
digits = [int(digit) for digit in str(number)]
# Raise every digit to the total number of digits.
total = sum(digit ** len(digits) for digit in digits)
result = "is" if total == number else "is not"
print(f"{number} {result} an Armstrong number")
Result of running the code
153 is an Armstrong number
Code explanation
The list comprehension turns each character of str(number) into an integer, giving [1, 5, 3]. len(digits) is 3, so each digit is raised to the power 3 and the results 1, 125 and 27 are added together. Because the total equals the original number, result is "is".
Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.
Practice exercises
Modify the runnable example with the exercises below to build understanding beyond copying the result.
- Change the input and predict the output before running.
- Handle empty or invalid input.
- Wrap the logic in a function and add more test cases.
Common mistakes
Always cubing the digits
The power must be the number of digits, not 3.
total = sum(digit ** 3 for digit in digits)
total = sum(digit ** len(digits) for digit in digits)
Finished this lesson?
Mark it as learned to track your progress.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
- Armstrong Number Check whether a number equals the sum of its digits raised to the digit count. Easy
- Digit Power Sum Add each digit raised to its position from the left. Medium