Python For Loop is one of the most important topics for GATE CSE 2026
In this post, we will cover for loop syntax, working, time complexity, and 3 GATE Previous Year Questions with detailed solutions.
What is For Loop in Python?
For loop in Python is used to iterate over a sequence like list, tuple, or range.
Syntax:
for variable in sequence:
# code block
Example:
for i in range(3):
print(i)
Output:
0
1
2
Why range(3) gives 0,1,2? Because range(n) starts from 0 and goes till n-1.
Time Complexity of For Loop
If loop runs n times, complexity is O(n). If nested for loop runs, complexity becomes O(n^2). This is frequently asked in GATE.
GATE Previous Year Questions on For Loop
Q1. GATE CSE 2020:
What will be output?
for i in range(0, 5, 2):
print(i)
Solution: range(0,5,2) means start 0, stop 5, step 2. Output is 0, 2, 4.
Q2. GATE CSE 2019:
How many times will loop execute?
for i in range(10):
for j in range(i):
print(i,j)
Solution: Outer loop 10 times. Inner loop i times. Total complexity O(n^2). Total iterations = 45.
Q3. Common Interview Question:
What is difference between for and while loop?
Ans: For loop is used when number of iterations is known. While loop is used when condition-based iteration is needed.
Conclusion
For loop is easy but GATE always asks tricky questions on range() function and nested loops. Practice 10 questions daily from GateStudy.in
Next Post: Python While Loop GATE Questions.
