TGViewer
Python Learning Python Learning @python_bds Β· 5.75K subscribers
Post #1162 777
🐍 Python For Loops (Iteration) πŸ”„

For loops are used to iterate over a sequence (like a list, tuple, string, or range) or other iterable objects. They let you execute a block of code repeatedly for each item.

πŸ‘‰ They are essential for automating tasks and processing collections of data efficiently.

πŸ”Ή 1. What is a For Loop?

A for loop executes a set of statements, once for each item in a collection.

Example:
for character in "Python":
print(character)

Output:
P
y
t
h
o
n

πŸ”Ή 2. Looping Through a List

This is one of the most common uses: going through each item in a list.

Syntax:
for item in list_name:
# code to execute for each item

Example:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(f"I like {fruit}.")

Output:
I like apple.
I like banana.
I like cherry.

πŸ”Ή 3. The range() Function

The range() function generates a sequence of numbers, often used to loop a specific number of times.

β€’ range(stop): Numbers from 0 up to (but not including) stop.
β€’ range(start, stop): Numbers from start up to (not including) stop.
β€’ range(start, stop, step): Numbers from start up to stop, increasing by step.

Example:
for i in range(3): # Loop 3 times (0, 1, 2)
print(f"Iteration {i}")

Output:
Iteration 0
Iteration 1
Iteration 2

πŸ”Ή 4. break and continue Statements

β€’ break: Stops the loop completely, even if the iterable hasn't finished.
β€’ continue: Skips the rest of the current iteration and moves to the next.

Example (break):
for number in range(1, 6):
if number == 4:
break # Stop when number is 4
print(number)

Output:
1
2
3

Example (continue):
for item in ["A", "B", "C", "D"]:
if item == "C":
continue # Skip 'C'
print(item)

Output:
A
B
D

🎯 Today's Goal(What you should do)

βœ”οΈ Understand what for loops are
βœ”οΈ Iterate over lists, strings, and ranges (using your Python editor)
βœ”οΈ Use break and continue to control loop flow (using your Python editor)
  • ❀ 6
  • πŸ”₯ 2
More from @python_bds
  1. Oct 8, 2026πŸ› This Python Bug Looks Random, But It Isn't Look at this: functions = [] for i in range(…
  2. Oct 6, 2026🐍 Python Performance Optimization Python Performance Optimization: Make Your Code Faster…
  3. Oct 4, 2026Python Set Methods ✍️
  4. Oct 3, 2026Post #1310
  5. Oct 1, 2026🐍 Python Beginner Notes
  6. Sep 30, 2026🧠 return vs print() in Python These are not interchangeable. def add(a, b): print(a + b)…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook β†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 β†’