🔁 Loops in Python: My First Taste of Real Coding Power

Bibi ZainabBibi Zainab
4 min read

After getting comfortable with conditions and logic, I noticed something:
I was repeating code way too often. It felt off — clunky, inefficient.

That’s when I discovered one of Python’s most powerful tools: loops.
They didn’t just simplify code — they reshaped how I think.


🔁 1. while and for Loops — Repeating with Purpose

I started with the while loop. The idea was simple:

“Keep doing something as long as a condition is true.”

i = 1
while i <= 5:
    print("Hello", i)
    i += 1

With this, I could repeat tasks without copy-pasting code endlessly.

Then came the for loop — more concise and more powerful, especially with range():

for i in range(1, 6):
    print("Hello", i)

Using range(start, stop, step), I could control iterations precisely — and it unlocked number games, pattern-making, and more.


⭐ Real Practice: Pattern Printing

One of my proudest beginner moments? Printing my first pattern using loops:

rows = 5
for i in range(1, rows + 1):
    print("*" * i)

Output:

*
**
***
****
*****

That simple task taught me about nested loops, visual output, and how logic creates structure — even art — on the screen.


🔁 Nested Loops — Loop Inside a Loop

Once I got comfortable with simple loops, I stepped into the world of nested loops — where one loop runs inside another.

Let’s print unique pairs:

for i in range(1, 5):
    for j in range(1, 5):
        print(i, j)

This is where I realized how multiple layers of logic could be stacked together to handle grids, combinations, or patterns.


🧩 Bonus: More Complex Patterns

Here's a pattern with increasing and decreasing numbers using nested loops:

rows = int(input("Enter number of rows: "))

for i in range(1, rows + 1):
    for j in range(1, i + 1):
        print(j, end='')
    for k in range(i - 1, 0, -1):
        print(k, end='')
    print()

Example Output (for 4 rows):

1
121
12321
1234321

This taught me how symmetry, control, and logic can all blend together in creative ways.


🧮 Sum of Digits — My First Real Algorithm

Then came a logical challenge:
“Take a number and print the sum of its digits.”

n = int(input("Enter a number: "))
sum = 0

while n > 0:
    digit = n % 10
    sum += digit
    n //= 10

print("Sum of digits is:", sum)

This wasn't just a coding exercise — it was my first real algorithm.
It involved math, logic, iteration, and flow control — and gave me confidence that I could solve problems using code.


🔐 Control Statements — Breaking Rules (Intentionally!)

Once I started working on loops, I faced situations where I needed to exit a loop, skip something, or just hold a spot in code. Python gave me tools for that too.


🚨 break: Exiting Early

Problem: Print all prime numbers in a given range.

lower = int(input("Enter lower range: "))
upper = int(input("Enter upper range: "))

for i in range(lower, upper + 1):
    for j in range(2, i):
        if i % j == 0:
            break
    else:
        print(i)

Here, break helps exit the inner loop early if the number isn’t prime.


⏭️ continue: Skip This One!

for i in range(1, 10):
    if i == 5:
        continue
    print(i)

Output:
1
2
3
4
6
7
8
9

It skipped 5 but continued the rest. Powerful in filtering data or avoiding specific cases.


💤 pass: Do Nothing (Literally)

Sometimes I needed to write a block of code but didn’t want to do anything (yet).
That’s where pass came in:

for i in range(1, 10):
    pass  # I’ll add logic later here

It helped avoid errors while I was planning or testing.


🧠 Shift in Thinking

Loops didn’t just save time — they reshaped how I think.
They taught me to:

  • Break problems into small, repeated steps

  • Visualize control flow

  • Write cleaner, more logical code


💬 Final Thoughts

If you're learning Python, loops are where the real thinking starts.
It's not about memorizing syntax — it’s about developing the problem-solving mindset.

Once loops click, you’ll see code differently.


#python #loops #beginners #codingjourney #learnpython #controlstatements #patternprinting

0
Subscribe to my newsletter

Read articles from Bibi Zainab directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Bibi Zainab
Bibi Zainab