Remove-Last-Digit Number Pattern in Python

What You’ll Learn
How to print a simple number pattern in Python by repeatedly removing the last digit of a number.
You’ll use a while loop and integer division by 10 to shrink the number one digit at a time.
⭐ Pattern Output
For num = 86523, the pattern looks like this:
86523
8652
865
86
8Complete Python Program
Print the number, then update it with num //= 10 until it becomes 0.
num = 86523
while num != 0:
print(num)
num //= 10🧠 How It Works
Start with a number
num = 86523 is the initial value we want to shrink line by line.
Loop until it becomes 0
while num != 0 keeps printing until there are no digits left.
Print the current value
print(num) outputs the current value on its own line.
Remove the last digit
num //= 10 removes the last digit by integer division.
Shrinking digits
Each iteration removes exactly one digit, so the loop runs once per digit.
Variation — User Input Version
Let the user enter a number and print the same remove-last-digit pattern.
num = int(input("Enter a number: "))
num = abs(num)
if num == 0:
print(0)
else:
while num != 0:
print(num)
num //= 10💡 Tips for Enhancement
Try These
- Use a different base (e.g.,
//= 2) to explore other shrinking patterns - Store the values in a list first if you want to reuse them later
- Print the removed digit each step using
num % 10 - Handle negative numbers by printing the sign separately
- Format output with prefixes like
Step 1:for clarity
Avoid
- Using floating-point division (use
//for integers) - Forgetting the
num == 0case (nothing would print) - Assuming user input is always valid without handling exceptions (optional)
- Mixing up
/and//(they behave differently)
Key Takeaways
Each loop iteration prints the current number and removes one digit.
num //= 10 is the key operation to remove the last digit.
The loop runs once per digit, so it is O(d).
This idea also helps in digit-based problems like reverse and sum of digits.
❓ Frequently Asked Questions
// performs integer division, which removes the remainder and keeps num as an integer.num % 10 to get the last digit before doing num //= 10.abs(num) (as shown) to treat the digits the same way, or preserve the sign separately.Explore More Python Number Patterns!
Digit-based loops are a great foundation for reverse, sum-of-digits, and palindrome checks.
Integer division by 10 is one of the most common tricks in digit problems: it removes the last digit, while % 10 extracts it.
7 people found this page helpful
