๐ฐ Introduction :

In programming, we often need to repeat the same code multiple times. For example:
Printing numbers from 1 to 10.
Checking elements in a list.
Repealing a game until the player loses.
Instead of manually repeating the code, we use for and while loops.
๐ First: The for loop
โ General form :
Python
for a variable in an array:
# Iterate code
๐ฆ The most famous example โ using range():
Python
for i in range(5):
print(i)
๐ Result :
0
1
2
3
4
๐ range(5) means: from 0 to 4 (5 not included).
๐ Printing numbers from 1 to 10 :
Python
for i in range(1, 11):
print(i)
๐ช Simple exercise :
Write code that prints the sentence “Hello!” Five times:
Python
for i in range(5):
print(“Hello!”)
๐งฎ Practical example: Multiplication table
Python
number = int(input(“Pick a number: “))
for i in range(1, 11):
print(number, “x”, i, “=”, number * i)
๐ Second: The while loop
โ General form :
Python
while condition:
# code that repeats
๐ The while is used when you don’t know the number of iterations in advance.
โณ Simple example :
Python
i = 1
while i <= 5:
print(“Repeat number”, i)
i += 1
โ ๏ธ Important warning :
Beware of infinite loops! If you forget to update the condition inside the while statement, it will run forever:
python
# This code will not stop!
while True:
print(“Always on”)
๐งช Exercise 1 :
Ask the user for a password, and repeat the request until they type it correctly:
python
password = “”
while password != “python123”:
password = input(“Enter password: “)
print(“Login successful!”)
๐ Exercise 2 :
Write code that asks the user for a number, then prints from 1 up to that number:
python
n = int(input(“Enter a number: “))
for i in range(1, n + 1):
print(i)
๐ The difference between for and while
The for loop property while loop
The number of iterations is known Yes No Always
Usage: Lists, range(), defined iterations, conditional conditions, loops of unknown duration
๐ Summary :
You have learned to use for and while to repeat commands.
You have understood the difference between loops.
I used range() and tried practical examples.
๐ What will we learn in Lesson 5 ?
In the next lesson, we’ll learn how to organize code using functions. You’ll be able to reuse code in a smarter, more professional way.
ููููุฉ ุชูุฒูู ุงูุชุทุจููุงุช
ุงููุฑ ูู ุงูุฃุณูู ุนูู ุชูุฒูู Dragon_Pro
ุณุชุธูุฑ ุตูุญุฉ ุงูุนุฏ ุงูุชูุงุฒูู ุจุนุฏ ุงูุงููุงุก ู
ู ุงูุนุฏ ุงููุฑ ุนูู ุชุญู
ูู ุงูุงู
ุซู
ูู ุงูุงุณูู ุงููุฑ ุนูู ุชุญู
ูู ุนุงุฏู
ุงูุชุถุฑ ุญุชู ููุชูู ุงูุนุฏ ู
ุฑุฉ ุงุฎุฑู
ุณุชุธูุฑ ุงููููุฉ ุงุจุฏุง ุชุญู
ูู ุงูุงู
ุงููุฑ ุนูููุง ุณูุชู
ุชุญู
ูู ุงูุชุทุจูู
How to download apps Click Download Dragon_Pro
at the bottom.
A countdown page will appear. After the countdown is complete, click Download Now. Then at the bottom, click Normal Download.
Wait for the countdown to finish again Then the Start Download Now icon will appear.
Click it to download the app.