Skip to content

Python! Loops For and While | Lectures 6

Lecture 6 Loops For and While

Lets Start Python Programming For Beginners to advanced. Loops โ€“ For and While, Lectures 6

๐Ÿ Lecture 6: Loops โ€“ For and While

๐ŸŽฏ Goal

By the end of this lecture, you will:

  • Understand what loops are and why they’re useful.
  • Be able to use for loops to repeat actions a set number of times.
  • Use while loops to repeat actions while a condition is true.
  • Build fun programs like counters, timers, and simple games.

๐Ÿง  What Are Loops?

A loop lets you repeat code many times โ€” just like doing something over and over again!

๐Ÿ” Imagine you’re jumping rope:

  • You jump once โ†’ one line of code.
  • You jump 10 times โ†’ one loop that runs 10 times.

In Python, there are two main types of loops:

  • for loop โ†’ repeats a specific number of times
  • while loop โ†’ repeats as long as a condition is true

[adrotate banner=”6″]

๐Ÿ” for Loop โ€“ Repeat a Certain Number of Times

Syntax:

for i in range(5):
    print("Jump!")

Output:

Jump!
Jump!
Jump!
Jump!
Jump!

๐Ÿ‘‰ range(5) means “do it 5 times”. You can change the number inside to do it more or less.

Example: Counting from 1 to 5
for i in range(1, 6):
    print(i)

๐Ÿ‘‰ Output:

1
2
3
4
5

๐Ÿง’ Kids Corner: Think of a for loop like eating cookies โ€” one by one until theyโ€™re all gone!

[adrotate banner=”6″]

๐Ÿ”„ while Loop โ€“ Repeat While Something Is True

A while loop keeps going until something changes.

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

๐Ÿ‘‰ Output:

Count: 1
Count: 2
Count: 3
Count: 4
Count: 5

This loop says:

  • As long as count is less than or equal to 5 โ†’ keep looping.
  • Each time, increase count by 1 (count += 1).

โš ๏ธ Be careful not to make an infinite loop (a loop that never stops!).

Lecture 6 Loops For and While
Lecture 6 Loops For and While

[adrotate banner=”6″]

๐Ÿ›‘ Breaking Out of a Loop Early โ€“ break

You can stop a loop anytime using break.

Example:
for i in range(10):
    if i == 5:
        break
    print(i)

๐Ÿ‘‰ This will print numbers from 0 to 4, then stop.

๐Ÿงช Try It Yourself!

Write a program that counts from 1 to 10 using both:

  • A for loop
  • A while loop

๐Ÿ’ก Sample Code:

# Using for loop
print("Using for loop:")
for i in range(1, 11):
    print(i)

# Using while loop
print("\nUsing while loop:")
count = 1
while count <= 10:
    print(count)
    count += 1

๐ŸŽฎ Mini Project: Countdown Timer

[adrotate banner=”6″]

Letโ€™s build a countdown timer from 10 to 1, then say “Blast off!” at the end.

print("๐Ÿš€ Countdown Timer")
for i in range(10, 0, -1):
    print(i)
print("Blast off! ๐ŸŒŒ")

๐Ÿ‘‰ Output:

10
9
8
7
6
5
4
3
2
1
Blast off! ๐ŸŒŒ

๐Ÿ“ Note: range(10, 0, -1) means start at 10, go down to 1.

๐Ÿง’ Kids Corner

๐Ÿง  Imagine you’re on a swing โ€” you go back and forth again and again.
Thatโ€™s what a loop does โ€” it makes your code swing through the same steps until it’s done.


Back and forth, again and again โ€” thatโ€™s a loop!

[adrotate banner=”6″]

๐Ÿš€ Challenge (Optional)

Make a password guessing game where the user has to guess a secret password.

Example:

password = ""
while password != "secret":
    password = input("Enter the password: ")
    if password == "secret":
        print("Access granted! ๐ŸŽ‰")
    else:
        print("Wrong password. Try again.")

๐Ÿ“Œ Summary

  • Use for loops when you know how many times to repeat.
  • Use while loops when you want to repeat until something changes.
  • Avoid infinite loops unless you have a way to break out using break.
  • Loops help you write short code that does big things!

Python Programming For Beginners To Advanced

Python Programming For Beginners! Lecture 1

Python Programming! Variables and Data Types | Lectures 2

Python Programming! Input And Output | Lectures 3

Python Programming! Operators in Python Lectures 4

Python Programming! Conditional Statements Lectures 5

Python! Loops For and While | Lectures 6

Python! Building A Mini Game | Lectures 7

[adrotate banner=”6″]

Najeeb Alam

Najeeb Alam

Technical writer specializes in developer, Blogging and Online Journalism. I have been working in this field for the last 20 years.

Leave a Reply

Your email address will not be published. Required fields are marked *