Shape Rule
Print then shrink
Each line shows the current num; the next line drops the rightmost digit — 86523 → 8652 → 865.

Program 60 prints a shrinking number pattern: start with an integer, print it, then remove the last digit with num // 10 until the value reaches zero. This tutorial covers the while-loop core, integer division, a live preview, worked Python examples, edge cases, and O(d) complexity.
Print then shrink
Each line shows the current num; the next line drops the rightmost digit — 86523 → 8652 → 865.
num != 0
while num != 0 repeats until integer division reduces the value to zero.
num // 10
num = num // 10 (or num //= 10) drops the last digit — Python uses floor division for integers.
One line per step
print(num) prints the current value before the division step.
Any integer
Enter a starting number and watch the digit-removal pattern in the browser.
Complexity
One iteration per digit — 86523 has five lines; total steps equal digit count.
A remove-last-digit number pattern prints an integer, then repeatedly strips the rightmost digit until nothing remains. With num = 86523, you get 86523, 8652, 865, 86, 8.
In Python use while num != 0, print with print(num), then update with num = num // 10.
Integer division and modulo are building blocks for digit counting, reversing numbers, palindrome checks, and sum-of-digits problems.
while num != 0 — one step per digit.
Integer division drops the last digit.
Program 60 shrinks the original; Program 61 builds a growing reverse.
Follow Program 59; continue to Program 61 next.
In short: while num != 0, print(num), then num //= 10.
Given starting integer num = 86523, print the number on each line while removing the last digit until num becomes 0.
# num = 86523
#86523
#8652
#865
#86
#8 | Item | Type | Description |
|---|---|---|
num | int | Starting integer — updated each loop iteration. |
| Loop condition | bool | num != 0 — stops when all digits are removed. |
| Print step | void | print(num) before dividing. |
| Update step | int | num = num // 10 drops the last digit. |
| Line count | int | Equals digit count of the starting number (86523 → 5 lines). |
| Final value | int | Loop ends at 0 — zero is not printed with != 0. |
while num is not 0:
print num
num = num // 10 | Approach | Idea | Best for |
|---|---|---|
| while num != 0 | Print then divide by 10 | Standard digit-removal pattern |
| abs first | Handle negative input safely | User-input programs |
| Compact trace | num = 123 on paper first | Quick dry-runs |
| Track removed digit | num % 10 before dividing | Extension exercises |
long | Arbitrary-precision integers | Very large starting values |
| Goal | Pattern |
|---|---|
| Loop | while num != 0: |
print(num) | |
| Remove digit | num = num // 10 or num //= 10 |
| Negative input | num = abs(num) before the loop |
Same digit-removal pattern — three ways to set the starting number and trace the logic.
num = 86523Hard-coded start for demos
int(input())Read starting number from console
num = 123Quick dry-run on paper
while != 0One iteration per digit
num //= 10Drop last digit each step
Reach for this pattern when teaching while loops, integer division, and digit manipulation in Python.
Natural follow-up after grid patterns — switch from nested loops to a single while loop.
Foundation for counting digits, reversing numbers, and palindrome checks.
Classic while-loop question — explain print-then-divide before coding.
Compare shrinking the original with building a growing reverse number.
Use Python’s arbitrary-precision integers when inputs exceed typical 32-bit limits.
Key benefit: one tiny program that locks in while loops, integer division, and O(d) thinking.
Enter a positive integer between 10 and 99999999 and draw the digit-removal pattern in the browser.
Three complete Python programs — fixed starting value, user input with negative handling, and a compact num = 123 trace. Click View Output to reveal sample console results.
Print the digit-removal pattern for a hard-coded starting integer with a while loop.
num = 86523Hard-coded start — ideal for first demos and screenshots.
num = 86523
while num != 0:
print(num)
num = num // 10 Print 86523, divide to get 8652, repeat until num becomes 0. The loop runs once per digit — five lines for a five-digit start.
Read the starting number from the user and handle negatives with abs.
Read num with int(input()), validate with try/except, and use abs for negatives.
try:
num = int(input("Enter a number: "))
except ValueError:
print("Please enter a valid integer.")
else:
num = abs(num)
while num != 0:
print(num)
num //= 10 Same loop core as Example 1; only the source of num changes from a literal to user input, with validation and abs.
Use num = 123 for a quick paper trace before larger demos.
num = 123Same while loop with a smaller starting value — easy to dry-run on paper.
num = 123
while num != 0:
print(num)
num //= 10 Three iterations: print 123, then 12, then 1 — trace this small case before scaling to larger numbers.
Set num to a fixed value or read it with int(input()).
while num != 0 keeps running until integer division reduces the value to zero.
print(num) outputs the current number on its own line.
num = num // 10 uses floor division to drop the rightmost digit.
One line per digit removed — O(d) time for d digits, O(1) extra memory.
num = 86523Trace each loop iteration: print the current num, then apply integer division by 10.
| Step | After num //= 10 | Digits left | |
|---|---|---|---|
| 1 | 86523 | 8652 | 4 |
| 2 | 8652 | 865 | 3 |
| 3 | 865 | 86 | 2 |
| 4 | 86 | 8 | 1 |
| 5 | 8 | 0 (loop ends) | 0 |
Total lines printed: 5 = digit count of the starting number.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Step count depends on input — a natural introduction to condition-driven loops.
Example: trace num = 123 and count three iterations.
Foundation for counting digits, reversing numbers, and palindrome checks.
Example: track num % 10 before dividing to print removed digits.
// 10 drops the last digit — concrete floor division, not float division.
Example: compare 86523 // 10 with 86523 / 10.
Shrinking the original pairs naturally with building a growing reverse number.
Example: Program 61 prints 3, 32, 325 from the same starting value.
One iteration per digit — makes O(d) concrete for beginners.
Example: num = 1000000 prints seven lines — seven digits.
Pair the pattern with int(input()) and zero-checks.
Example: reject non-numeric input and handle num = 0.
Pro Tip: when an interviewer asks for digit patterns, explain print-then-divide before writing the loop — the story matters as much as the code.
Why this pattern earns a permanent spot in beginner Python courses.
Wrong loop updates show up immediately as an infinite loop or missing lines.
Only a while loop and integer division — no arrays or math libraries.
Track removed digits, include zero, or switch to modulo-based reverse builds with small edits.
Streaming output needs no storage beyond the loop variable.
Pro Tip: trace num on paper for 123 before coding — watch how each division drops one digit.
Small habits that keep digit-removal code clean.
Use num (or n) for the shrinking value.
Avoid crashes when the user types letters instead of a number.
Call print(num) before num //= 10 so each step shows the current value.
num // 10 drops the last digit — use //, not / which returns a float.
Trace num = 123 on paper before coding larger demos.
Pro Tip: if the loop never stops, you almost certainly forgot num //= 10 inside the body.
Mistakes that commonly break digit-removal patterns.
Without num //= 10, the loop condition never changes — infinite loop.
→ Always divide by 10 after printing the current value.
Dividing doubles by 10 can introduce decimals — not what you want for digit stripping.
→ Keep num as int and use integer division.
while num != 0 never enters the body — silent empty output.
→ Validate input is non-zero or handle zero as a special case.
Letters or empty input raise ValueError when int(input()) is unchecked.
→ Wrap in try/except ValueError and re-prompt on failure.
Negative num still divides correctly but may confuse beginners reading output.
→ Apply abs(num) before the loop when reading user input.
Check these inputs before calling the solution done.
while num != 0 never runs — print nothing or show a message.
One line prints 1, then loop ends — simplest case.
120 prints 120, 12, 1 — the trailing 0 vanishes on first division.
Apply abs before the loop — see Example 2.
Bare int(input()) raises ValueError — use try/except.
Python integers have arbitrary precision — no overflow for typical inputs.
Try these variations to lock in the pattern.
num % 10 on its own linenum = 123 before coding/ 10 removes exactly one digit.num = 1 prints a single line.Quick Takeaway: while num != 0, print num, then num //= 10 — that is the whole pattern.
| Program | Time | Extra space |
|---|---|---|
| While loop (Examples 1–2) | O(d) — d = digit count | O(1) |
| Compact trace (Example 3) | O(d) | O(1) |
The remove-last-digit number pattern is a small while-loop exercise with lasting payoff: integer division, digit manipulation, and O(d) intuition. Master the fixed-value version, then try user input and the compact num = 123 trace.
Practice the three examples above, then continue to Program 61 for the next pattern in the series.
Print before dividing — keep num as an integer, and validate input when reading from the console.
while num != 0 with num //= 10 inside the bodyprint(num) before each division steptry/except ValueError around int(input()) for user inputnum inside the loop// floor division, not / float divisionabsnum = 123 dry-run before larger demosPrint the shrinking number pattern the beginner-friendly way.
Print num then divide by 10
DefinitionRuns until num is 0
Codenum //= 10 drops last digit
MathOne line per step
I/OO(d) time
AnalysisEach iteration prints the current number, then num = num // 10 drops the last digit using integer division — runtime is O(d) for d digits.
Move on to the next pattern in the Python number-pattern series.
12 people found this page helpful