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 timeswhile
loop โ repeats as long as a condition is true
๐ 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!
๐ 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!).
๐ 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
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!
๐ 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