Python for Loop
The for loop in Python is an iteration structure.
If you have an ordered object like a list, you can use the for loop to sequentially operate on the items in that list.
The working logic of the for loop is very similar to the structure you see in many other programming languages.
In this article, we will examine the for loop in Python in detail.
We'll learn how to iterate over lists, tuples, and other data structures.
We will also discuss in detail the break and continue statements used to control the flow of the loop.
When to Use a For Loop?
The for loop is used when you need to repeat a particular block of code a fixed number of times.
If you don't know how many times the loop will run, then it is more convenient to use while loop.
For Loop Syntax in Python
The basic syntax of the for loop in Python is as follows:
for degisken in koleksiyon:
# Her yinelemede çalışacak kod bloğu
Details of Python For Loop Syntax
Let's examine the structure of a for cycle step by step:
forkeyword → Indicates the start of the cycle.- Iterator variable → It represents each element in the array sequentially and can be used inside the loop.
inkeyword → Tells Python to loop the iterator variable over the elements in the array (or iterable structure).- Array variable → This can be a list, tuple, string or any iterable.
- Expressions inside the loop → Here you can manipulate the iterator variable, call functions or perform calculations.
Printing the Letters of a String with a For Loop
In Python, string is an array of characters.
If you need to loop through letters one by one in a text, you can easily do this with the for loop.
The following example shows how it works:
metin = "Python"
for harf in metin:
print(harf)
Output
P
y
t
h
o
n
The reason this loop works is because Python sees a **string as an array of characters, rather than as a single entity.
That is, Python treats the word "Python" as a list of characters "P", "y", "t", "h", "o", "n".
In this way, we can process each character one by one with the for loop.
Using a For Loop on a List or Tuple in Python
Lists and tuples are iterable data structures in Python.
We can use the for loop to loop through the elements within these structures one by one.
meyveler = ["elma", "muz", "çilek", "kivi"]
for meyve in meyveler:
print(meyve)
Output
elma
muz
çilek
kivi
Now let's go one step further and see how we can loop over the elements in a tuple.
sayilar = (5, 10, 15, 20, 25)
toplam = 0
for sayi in sayilar:
toplam += sayi
print(f"Sayıların toplamı: {toplam}")
Output
0 + 5 = 5
5 + 10 = 15
15 + 15 = 30
30 + 20 = 50
50 + 25 = 75
Nested For Loops
If an for loop contains another for loop, it is called a nested for loop.
This structure is especially useful when working with multilayered data (e.g. lists within lists or letters of words).
For example, in the previous example, the for loop was printing the words in the list one by one.
What if we want to print the letters in each word one by one?
This is exactly where nested for loop comes into play.
The first loop (parent loop) → loops words one by one.
The second loop (inner loop) → goes through the letters of each word one by one.
Example:
kelimeler = ["Python", "Kod", "Döngü"]
for kelime in kelimeler: # dış döngü: kelimeleri geziyor
print(f"Kelime: {kelime}")
for harf in kelime: # iç döngü: her kelimedeki harfleri geziyor
print(f" Harf: {harf}")
Kelime: Python
Harf: P
Harf: y
Harf: t
Harf: h
Harf: o
Harf: n
Kelime: Kod
Harf: K
Harf: o
Harf: d
Kelime: Döngü
Harf: D
Harf: ö
Harf: n
Harf: g
Harf: ü
Structurally, nested for loops are very similar to nested if statements.
Just as you can define another if block inside a if block,
Likewise, you can define another for loop inside a for loop.
Thanks to this structure, the outer loop contains a group (for example, words),
The inner loop checks the children of that group (for example, letters).
So the logic is the same, only the intended use is different.
Python For Loop with ### range() Function
The range() function in Python is one of the built-in functions.
It is used when you want a for loop to run a certain number of times or when you need to produce numbers within a certain range.
So the range() function allows you to control how many times the loop will loop or what numbers it will work on.
When using the range() function you can pass integer arguments 1 to 3:
1- start → Specifies the value from which the series will start.
If not specified, it starts at 0 by default.
2- stop → Specifies the value at which the sequence will end (this value is not included).
So the expression range(5) produces 0, 1, 2, 3, 4 — except 5.
3-step → Determines how much the number will increase or decrease at each step.
If not specified, it defaults to 1.
If a negative value is given, the numbers progress decreasing.
In the example below, we want to print the numbers 1, 2 and 3.
for i in range(1, 4):
print("Ekrandaki Sayı:", i)
Output
Ekrandaki Sayı: 1
Ekrandaki Sayı: 2
Ekrandaki Sayı: 3
The range() function takes a step parameter in addition to the start and stop parameters.
This parameter specifies how many numbers to skip at each loop step.
That is, at each step, the value step is added to the previous number.
for i in range(0, 10, 3):
print(i)
Output
0
3
6
9
We can also run the loop in reverse (reverse) by making the value step negative.
In this case, start value should be large and stop value should be small.
for i in range(10, 0, -2):
print(i)
Here:
- 10 → initial value (
start) - 0 → end value (
stop, not included) - -2 → step value (
step)
That is, the loop starts from 100 and decreases by 2 each time until it goes to 0.
Output
10
8
6
4
2
When programming in Python, for loops are usually used for iteration operations.
Uses the range() function.
Thanks to range(), you can easily loop over numbers in a certain range,
You can use these numbers to operate on structures such as list, tuple or string.
In short, we can say that range() — for is the most frequently used “iteration engine” of loops in Python.
break Statement in For Loop
The break statement is used to early terminate a for loop.
If you want to exit the loop when a certain condition is met, that's exactly what break is for.
Let's say we have a list of numbers and we want to check if a number is in the list.
If the number we are looking for is found, there is no need to continue the loop — we can exit immediately with break.
In this case, we can use the for loop with the if-else condition.
sayilar = [3, 7, 12, 18, 25, 30]
aranan = 18
for sayi in sayilar:
if sayi == aranan:
print(f"{aranan} listede bulundu.")
break
else:
print(f"{aranan} listede bulunamadı.")
Output
18 listede bulundu.
continue Statement in a For Loop
The statement continue causes the for loop to skip the body part under a certain condition.
So, when the condition is met, the loop ends that round and moves on to the next round.
For example, let's say we have a list of numbers and we only want to get the sum of positive numbers.
We can use continue to skip negative numbers when they arrive.
sayilar = [10, -3, 7, -1, 5]
toplam = 0
for sayi in sayilar:
if sayi < 0:
continue # negatifleri atla
toplam += sayi
print(f"Pozitif sayıların toplamı: {toplam}")
Output
Pozitif sayıların toplamı: 22
Using the else Block with Python For Loop
In Python, a else block can be used at the end of the for loop.
This else block runs only when the loop is not terminated by the statement break.
So if the loop completes normally, the else block will run,
but if the loop ends early with break, the else part is skipped.
For example, let's say we have a function — this function prints the sum of numbers only if all numbers are even.
If there is an odd number, let's end the loop with break and the total will not be printed.
def cift_sayilari_topla(sayilar):
toplam = 0
for sayi in sayilar:
if sayi % 2 != 0:
print(f"Tek sayı bulundu: {sayi} ❌")
break
toplam += sayi
else:
print(f"Tüm sayılar çift ✅ Toplam: {toplam}")
Let's test
even_numbers_add(2, 4, 6, 8) # all even
Tüm sayılar çift ✅ Toplam: 20
even_numbers_add(2, 5, 8, 10) # odd in between
Tek sayı bulundu: 5 ❌
Using For Loop with Sequential Data Types
for loop not only with range() but also list, tuple, string etc.
can also work on sequential data types.
Such data structures can be given directly as parameters to the for loop.
In other words, we can define a list and perform operations on its elements one by one.
renkler = ["kırmızı", "yeşil", "mavi", "sarı"]
for renk in renkler:
print(f"Renk: {renk}")
In this example, we print each item in the list to the screen.
We used the variable name renk, but actually any valid variable name could have been used.
Output
Renk: kırmızı
Renk: yeşil
Renk: mavi
Renk: sarı
As you can see from the output above, the for loop went through the list
and printed each item in the list on a separate line.
Lists and other ordered data types — such as string and tuple —
for is frequently used with loops because these data types are iterable structures that can be looped over.
kuslar = ['kartal', 'şahin', 'serçe', 'güvercin', 'doğan', 'atmaca']
for i in range(len(kuslar)):
kuslar.append('kuş')
print(kuslar)
Output
['kartal', 'şahin', 'serçe', 'güvercin', 'doğan', 'atmaca', 'kuş', 'kuş', 'kuş', 'kuş', 'kuş', 'kuş']
Here we added 'kuş' in each loop equal to the length of the bugs list.
So for each element in the list, another 'kuş' value is added to the list.
You can also create a list from scratch using the same logic.**
For example, let's add numbers from 1 to 5 to a new list with a loop.
sayilar = []
for i in range(1, 6):
sayilar.append(i)
print(sayilar)
Output
[1, 2, 3, 4, 5]
When looping over a dictionary, it is very important to keep the **key : value) structure in mind.
This way you can be sure that you are accessing the correct item in the dictionary.
Here is an example that calls both the key and the value:
balik_bilgisi = {'isim': 'Maviş', 'tür': 'köpekbalığı', 'renk': 'mavi', 'konum': 'okyanus'}
for anahtar in balik_bilgisi:
print(anahtar + ': ' + balik_bilgisi[anahtar])
Output
isim : Maviş
tür : köpekbalığı
renk : mavi
konum : okyanus
When using a for loop with dictionaries, the variable in the loop represents the keys of the dictionary.
That is, the expression sozluk_degiskeni[dongu_degiskeni] returns the value corresponding to that key.
In the example above, the variable anahtar used in the loop represents the keys in the dictionary,
The expression balik_bilgisi[anahtar] shows the value of that key.
Generally, loops are used to loop through and operate on ordered data types (lists, arrays, etc.).
The for loop in Python is very similar to loops in other programming languages.
We can use the expressions break and continue to change the flow of the loop.
However, in Python, unlike other languages, there may also be an optional else block** in the **for loop.
I hope this guide has given you a few new ideas about loops.
If you have anything on your mind, don't hesitate to share it in the comments.
If you want to continue from here, you can also take a look at other lessons about while cycle and break, continue, pass expressions.

