Python For Loop Tutorial With Examples To Practice
Loops are a fundamental concept in programming that allow you to execute a block of code repeatedly. In Python, the for loop is one of the most commonly used loops because it is simple, readable, and powerful.
Whether you are a beginner learning Python or preparing for coding interviews, understanding the Python for loop is essential. This tutorial explains the syntax, working, examples, and practice problems to help you master the for loop in Python.
What Is a For Loop in Python?
A for loop in Python is used to iterate over a sequence such as:
-
List
-
Tuple
-
String
-
Dictionary
-
Set
-
Range
In simple words:
A for loop repeats a block of code for each element in a sequence.
Syntax of Python For Loop
-
variable→ Takes the value of each element -
sequence→ Collection or range -
statements→ Code executed in each iteration
Basic Example of For Loop
Output
How Python For Loop Works
-
The loop picks the first element from the sequence
-
Executes the block of code
-
Moves to the next element
-
Repeats until the sequence ends
For Loop Using range()
The range() function is commonly used with for loops.
Example
Output
For Loop With List
Output
For Loop With String
Output
For Loop With Tuple
For Loop With Dictionary
Nested For Loop
A nested for loop means a loop inside another loop.
Example
Output
For Loop With break
The break statement stops the loop immediately.
For Loop With continue
The continue statement skips the current iteration.
For Loop With else
The else block executes after the loop finishes normally.
Practice Examples for Python For Loop
1. Print Numbers From 1 to 10
2. Find Sum of Numbers
3. Print Even Numbers
4. Multiplication Table
5. Count Characters in a String
Common Beginner Mistakes
-
Forgetting indentation
-
Using wrong range values
-
Confusing
forwithwhile -
Modifying the sequence inside loop
Time Complexity of For Loop
The time complexity depends on:
-
Number of iterations
-
Operations inside the loop
Usually:
-
O(n) for simple loops
For Loop Interview Questions
-
What is a for loop in Python?
-
Difference between for loop and while loop?
-
What is range()?
-
Can for loop be used with else?
-
Difference between break and continue?
When to Use For Loop
-
Iterating over sequences
-
Repeating tasks fixed number of times
-
Processing data collections
-
Writing clean and readable code
The Python for loop is one of the most powerful and beginner-friendly looping constructs. It helps you iterate over data efficiently, write clean code, and solve real-world programming problems.
By practicing the examples in this tutorial, you will gain confidence and build a strong foundation in Python programming. Mastering for loops is a crucial step toward learning advanced Python concepts and cracking coding interviews.

Comments
Post a Comment