Course Content
Module 2 – Introduction to Python Programming
In this Introduction to Python module, learners explore Python’s clear, readable syntax and powerful features. Beginning with installation and a simple “Hello, World!” script, you will progress through variables, control flow and functions using step-by-step examples. By the end, you will be equipped to write your own Python programmes, automate routine tasks and tap into an extensive library ecosystem for real-world projects.
0/7
Module 3 – Variables, Data Types and Basic Operations
In the Variables, Data Types and Basic Operations in Python module, learners explore how to store and manage data using variables, master fundamental types such as integers, floats, strings and booleans, and perform arithmetic, comparison and logical operations step by step. Clear explanations, real world examples and hands on exercises guide you through writing and debugging code. By the end of this module, you will be ready to build dynamic Python programs and automate everyday tasks.
0/6
Module 4 – Control Flow – Conditions and Loops
Control flow structures determine the order in which your program’s code executes. With conditional statements, you can make decisions and execute certain code blocks only when specific conditions are met. Loops allow you to repeat actions efficiently without writing redundant code. In this module, we will explore fundamental control flow concepts in Python in a step-by-step manner, similar to Microsoft’s learning curriculum. By the end, you’ll understand how to use if, elif, and else statements (including nested conditions) for decision-making, how truthy and falsy values work in Boolean logic, how to construct for loops (using range() and iterating over collections), how to use while loops along with loop control statements (break and continue), and how to leverage list comprehensions and generator expressions for concise looping. Finally, we’ll apply these concepts in a practical exercise to build an interactive decision-making system. Each section below includes explanations, code examples, and mini-exercises to reinforce the concepts, all formatted for clarity and easy follow-along.
0/8
Module 5 – Functions and Code Organisation
Imagine you need to clean up a messy data set or send a personalised email to each customer. Instead of writing the same steps over and over, you can create a function and call it whenever you need. In this lesson on Functions and Code Organisation, you will learn how to define functions, pass and return information, document your work and group related code into modules for easy reuse and maintenance.
0/3
Introduction to Python Programming (Copy 2)

Lesson – While Loops & Loop Control in Python

Objective

By the end of this lesson, students will be able to:

  • Understand how while loops work.

  • Use break and continue to control loops.

  • Write programs that loop until a condition is met.

  • Avoid infinite loops.

1. What is a While Loop?

A while loop repeats a block of code as long as a condition is True.
This is different from a for loop (which loops over a sequence or a set number of times).

Syntax:

while <condition>:
    <code to run>
  • The <condition> is checked before each iteration.

  • If the condition is True, the loop runs.

  • If the condition is False, the loop stops.

Example – Counting Down

count = 5
while count > 0:
    print("Counting down:", count)
    count -= 1  # decreases count
print("Blast off!")

Output:

Counting down: 5
Counting down: 4
Counting down: 3
Counting down: 2
Counting down: 1
Blast off!

Key points:

  • The loop stops when count > 0 becomes False.

  • If we forget count -= 1, the loop never ends (infinite loop!).

2. Loop Control Statements

Python gives us two important tools to control loops:

break – Exit the loop completely

while True:
    text = input("Type 'quit' to exit: ")
    if text == "quit":
        print("Goodbye!")
        break
    print("You typed:", text)
  • break jumps out of the loop, no more iterations.

continue – Skip the rest of the current loop iteration

n = 0
while n < 10:
    n += 1
    if n % 2 == 0:   # if even, skip
        continue
    print(n)

Output:

1
3
5
7
9
  • Even numbers are skipped.

  • The loop still continues for other numbers.

3. Combining while, break, and continue

Example – Stop when total > 100

total = 0
num = 1
while True:
    total += num
    if total > 100:
        print("Reached above 100. Stopping.")
        break
    num += 1
print("Final total:", total)

Example – Input validation

while True:
    val = int(input("Enter an odd number: "))
    if val % 2 == 0:
        print("That's even! Try again.")
        continue
    break
print("You entered an odd number:", val)
  • Keeps asking until a valid odd number is entered.

4. Common Mistakes to Avoid

❌ Forgetting to update loop variables → infinite loop.
❌ Using while True without a break.
❌ Writing conditions that can never be False.

5. Mini-Exercises

1- Basic While Loop
Write a loop to print numbers 1 to 5. (Hint: You can do this similarly to the for loop examples, just make sure to increment your counter or you’ll loop infinitely.)

2- Break Example
Modify it to stop if the number is 3. (so it would only print 1, 2 and then stop).

3-Number Guessing Game

  • Create a number-guessing mini-game: the program has a secret number (say 7). Use a while loop to repeatedly ask the user to guess the number. If they guess correctly, break and congratulate them. If they guess too high or too low, print a hint and continue. Use input() to get user guesses inside the loop. (Be sure to convert input to int since it input() returns a string.)

6. How to Run the Code

  1. Open your Python file (example: lesson_while.py) in your IDE.

  2. Copy and paste one of the examples.

  3. Save the file.

  4. Run:

    python lesson_while.py
    
  5. Watch the output and modify the code to test different conditions.