Skip to main content

Command Palette

Search for a command to run...

Python Basics — Day 7 While Loops + Mini Projects

Updated
2 min read
S

Aspiring Full-Stack Developer | Python · Django · React · SQL | documenting my learning journey

Building skills in Python and full-stack development, with a focus on web apps and system design

Junior developer in training — Python, Django, React — preparing for a career in full-stack engineering

From Python basics to full-stack projects, sharing my progress as I grow into a developer

Future full-stack engineer | Learning in public: Python, APIs, Databases, and Web Development

01. Basic Structure of a while Loop

  • Repeats as long as the condition is True

  • Stops when the condition becomes False

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

👉 Output:

Hello 1
Hello 2
Hello 3
Hello 4
Hello 5

02. Infinite Loops with while

If the condition is always True, the loop runs forever.
Usually, break is used to exit.

while True:
    cmd = input("Enter q to quit: ")
    if cmd == "q":
        print("Program terminated!")
        break

03. Using continue

Skip the current iteration and move to the next one.

num = 0
while num < 10:
    num += 1
    if num % 2 == 0:
        continue
    print(num)   # Prints only odd numbers

04. While vs For

  • for loop → when the number of repetitions is fixed

  • while loop → when repetitions depend on a condition and may vary


05. Practice Examples

Example 1: Print Numbers 1 to 10

n = 1
while n <= 10:
    print(n)
    n += 1

Example 2: Password Checker

password = "1234"

while True:
    guess = input("Enter password: ")
    if guess == password:
        print("Login successful!")
        break
    else:
        print("Incorrect. Try again.")

06. Mini Projects ✨

(1) Multiplication Table

dan = int(input("Enter a number for the times table: "))
i = 1

while i <= 9:
    print(f"{dan} x {i} = {dan * i}")
    i += 1

(2) Sum Calculator

Keeps adding numbers entered by the user until 0 is typed.

total = 0

while True:
    num = int(input("Enter a number (0 to quit): "))
    if num == 0:
        break
    total += num

print("Total sum:", total)

(3) Number Guessing Game

import random

secret = random.randint(1, 20)  # Random number between 1 and 20
tries = 0

while True:
    guess = int(input("Guess the number (1–20): "))
    tries += 1
    if guess == secret:
        print(f"Correct! You guessed it in {tries} tries.")
        break
    elif guess < secret:
        print("Try a higher number.")
    else:
        print("Try a lower number.")
L

Nice mini-project ideas with while loops — very hands-on! I like to keep my dev setups clean when switching between projects, so I’ve been using ServBay (servbay.com) to spin up isolated environments without messing up global installs.

Python Basics

Part 7 of 21

A collection of study notes from my university and self-learning journey. This series covers Python fundamentals step by step — from setup to core concepts — to help both myself and others build a solid foundation in coding.

Up next

Python Basics — Day 8 Lists in

01. What is a List? A list is a data structure that stores multiple values in order. Use square brackets [ ] and separate values with commas. fruits = ["Apple", "Banana", "Grape"] numbers = [10, 20, 30, 40] mixed = [1, "Hello", True, 3.14] 👉 Ke...

More from this blog

S

Sabin’s DevLog

21 posts

I plan to upload everything I study and prepare here. I hope it can be helpful to others, and I also want my future self to see the learning journey I’ve been through.