Beginner Python Exercises

Check an Armstrong Number

An Armstrong number raises each digit to the number of digits.

What is Check an Armstrong Number?

An Armstrong number raises each digit to the number of digits.

Check whether a number equals the sum of its powered digits.

When should you use it?

  • Learn Python syntax through a practical example.
  • Build a foundation for larger programming problems.
  • Test an idea quickly without a local setup.

Example code

Run code →
main.py
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")

Expected output

153 is an Armstrong number

How it works

For 153, the cubes of 1, 5, and 3 add back to 153.

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.

  1. Change the input and predict the output before running.
  2. Handle empty or invalid input.
  3. Wrap the logic in a function and add more test cases.
Run in Python IDE →