Python for Loops
🐍 Python for Loops — Full Tutorial
The for loop in Python is used to iterate over a sequence (like a list, tuple, string, or range) and execute a block of code for each item.
🔹 1. Basic For Loop with List
✅ Output:
🔹 2. For Loop with Range
The range() function generates a sequence of numbers:
-
range(start, stop, step) -
start→ beginning number (inclusive) -
stop→ end number (exclusive) -
step→ increment (default is 1)
🔹 3. Loop Through String
✅ Output:
🔹 4. Nested For Loops
-
Useful for tables, patterns, matrices.
🔹 5. Using break in For Loop
break stops the loop immediately.
✅ Output:
🔹 6. Using continue in For Loop
continue skips the current iteration.
✅ Output:
🔹 7. Using else with For Loop
-
elseruns if the loop completes normally (not stopped bybreak).
✅ Output:
🔹 8. Looping Through Dictionary
🔹 9. Looping Through Tuple
🔹 10. Practical Examples
Multiplication Table
Sum of Numbers in a List
Pattern Printing
✅ Output:
🔹 11. For Loop Tips
-
Use
range()for number sequences. -
Use
breakto exit loop early. -
Use
continueto skip iteration. -
elseblock executes only if loop finishes naturally. -
Works with lists, tuples, sets, dictionaries, strings.
🧠 Practice Exercises
-
Print all even numbers from 1 to 50.
-
Print multiplication table for a user-entered number.
-
Calculate the factorial of a number using for loop.
-
Print a triangle pattern using
*. -
Loop through a dictionary of students and print their names.
