While Loop, break, continue, Functions, Modularization & Modules In Python
The day Python stopped being a notebook and became a real multi-file project — run from the terminal.
While Loop, break, continue, Functions, Modularization & Modules In Python
The day Python stopped being a notebook and became a real multi-file project — run from the terminal.

Part 1 — while Loop
A for loop runs a fixed number of times. A while loop runs as long as a condition is True. You control when it stops.
while condition:
# statement
counter update (i++) ← CRITICAL — without this, infinite loop!
Example 1 — Count 0 to 11
count = 0
while count <= 11:
print(count)
count += 1
# Output:
# 0 1 2 3 4 5 6 7 8 9 10 11
Three things every while loop needs:
Initialize the counter before the loop (
count = 0)
Condition that will eventually become False (
count <= 11)
Update the counter inside the loop (
count += 1)
Miss the third one — your loop runs forever. That’s called an infinite loop.
Example 2 — Multiplication Table with while
num = int(input("Enter any number: ")) # input() — Day 3
i = 1
while i <= 10:
print(f"{num} x {i} = {num * i}") # f-string — Day 3
i += 1
# Input: 10
# 10 x 1 = 10
# 10 x 2 = 20
# 10 x 3 = 30
# ...
# 10 x 10 = 100
for vs while — When to Use Which?
| Situation | Use |
|---|---|
| You know how many times to repeat | for loop |
| You repeat until something changes | while loop |
| Iterating over a sequence (string, list) | for loop |
| Waiting for user input to be correct | while loop |
## break and continue
break and continue
These two keywords give you control inside a loop.
break → exits the loop immediately (prematurely)
continue → skips current iteration and goes to the next one
Example 3 — break exits immediately
for i in range(0, 10):
print(i)
break # exits after first iteration
# Output: 0
The loop would normally run 10 times. break stops it after the very first step.
Example 4 — continue skips one iteration
for i in range(0, 10):
if i == 5:
continue # skip 5, go to next iteration
print(i)
# Output: 0 1 2 3 4 6 7 8 9
# ↑ notice 5 is missing
Example 5 — Find First Even Number ≤ 50 in a List
numbers = [3, 7, 52, 45, 67, 8, 34, 2]
for num in numbers:
if num % 2 == 0 and num <= 50:
print(num)
break # stop as soon as we find it
# Output: 8
Walks through the list one by one. The moment it finds a number that is both even and ≤ 50 — it prints it and stops immediately.
Example 6 — Print Only Even Numbers from a List (using continue)
numbers = [3, 7, 52, 45, 67, 8, 34, 2]
for num in numbers:
if num % 2 != 0: # if odd
continue # skip odd numbers
if num <= 50:
print(num)
break
# Output: 8
`continue` skips any odd number. `break` stops after finding the first qualifying even number.
Modularization → Making multiple modules
Benefits:
Reusability — write once, use across different programs
Maintainability — smaller, independent pieces are easier to fix
Scalability — easy to add new modules without breaking others
Organization — keeps code clean and manageable
Functions
A function is a named, reusable block of code that performs a specific task.
def functionName(parameters):
statement
return value
def— keyword to define a function
functionName— what you call it
parameters— inputs it receives
return— what it gives back
# defining the function
def addition(a, b):
return a + b
# calling the function
x = addition(2, 8)
print(x) # 10
### The Folder Structure
project/
├── main.py ← runs everything
├── example.py ← contains addition()
├── day8.ipynb ← Jupyter notebook
└── mod/
└── example2.py ← contains suntract() and multiple()
example.py — The First Module
def addition(a, b):
return a + b
mod/example2.py — The Second Module (inside a folder)
def suntract(a, b):
return a - b
def multiple(a, b):
return a * b
main.py — The Entry Point
from example import addition # import from example.py
from mod.example2 import * # import everything from mod/example2.py
x = addition(10, 5) # uses addition() from example.py
y = suntract(4, 2) # uses suntract() from mod/example2.py
z = multiple(2, 3) # uses multiple() from mod/example2.py
print(f"x={x}, y ={y}, z = {z}")
# Output: x=15, y =2, z = 6
Running from Anaconda Prompt
This is the moment the project became real.
# Navigate to the folder where your main.py file is saved
# Then run the script using the Anaconda Prompt command below:
python main.py
x=15, y =2, z = 6
Three Ways to Import
# Method 1: Basic import — use full path to call
import example
a = example.addition(4, 5)
# Method 2: Full module path — verbose, not ideal
import mod.example2
z = mod.example2.addition(4, 8) # → works but long
# Method 3: Selective import — clean and specific ✅
from mod.example2 import suntract, multiple
x = suntract(9, 1)
y = multiple(5, 3)
# Method 4: Import everything — use with care
from mod.example2 import * 메타데이터
- post_id
- 9b8df9012bc6
- slug
- while-loop-break-continue-functions-modularization-modules-in-python-9b8df9012bc6
- url
- https://medium.com/@dotsyko/while-loop-break-continue-functions-modularization-modules-in-python-9b8df9012bc6
- canonical_url
- https://medium.com/@dotsyko/while-loop-break-continue-functions-modularization-modules-in-python-9b8df9012bc6
- author_url
- https://medium.com/@dotsyko
- status
- ok
- fetched_at
- 2026-06-13 09:11:36