Loops in Python Explained : (Beginner’s Guide)

1. INTRODUCTION
Loops which are also called as iterative statements are useful when we want to repeat some of the tasks until a condition is satisfied. For example, if there are 10 students then we want to check the grade of every student, then we can use the concept of loops. The number of times the loop has to be executed are called as iterations.
2. TYPES OF LOOPS
There are three different types of loops that are available
1. for loop
2. while loop
3. nested loop
3. FOR LOOP
Syntax -
for variable_name in range (how many times to iterate):
# code to be executed for each iteration
EXAMPLE - 1 :
for i in range (6):
print(i)
'''
output - 0
1
2
3
4
5
'''
# The reason that the loop stopped printing at 5 is that when we mention a number n then
# range will iterate from 0 to n-1
EXAMPLE -2 :
i = 1
for num in range (i,10,3):
print(num)
'''
Output - 1
4
7
'''
# In the loop we have mentioned as i, 10, 3 so for each iteration it will add the i value to 3 and
# checks the condition.
EXAMPLE - 3 : We can use loops to iterate over the elements in a list, tuple, dictionary or even set.
colours = ["Blue", "Green", "Red", "Yellow"]
for element in colours:
print(element)
'''
Output - Blue
Green
Red
Yellow
'''
4. WHILE LOOP
It is very similar to for loop but the main difference is based on if we know how many times the loop has to iterate. In for loop we have to mention how many times the loop has to iterate but not many of the times we will know answer to this question so in that case we can use while loop.
A real life example for while loop is that if we open a website or an application we may use it just for a few seconds or for minutes or for hours so we can use while loop stating that the app has to work until the user is using the app.
Syntax -
while condition:
# statements to be executed
EXAMPLE - 1
num = 0
while num <= 5:
print(num)
num += 1 # num = num + 1
'''
Output - 0
1
2
3
4
5
'''
5. LOOP CONTROL STATEMENTS
These loop control statements are used while the loop is executing and will control a loop if a condition is satisfied. There are three types of loop control statements
break
continue
pass
1. BREAK - It will stop the loop if a condition is satisfied.
Example - In an e-commerce website we may want to cancel the whole order if an UPI payment is failed
i = 1
for num in range (i,10):
if num == 5:
break
print(num)
'''
Output - 1
2
3
4
'''
2. CONTINUE - continue is used when we want to skip an iteration if a condition is satisfied.
Example - Just like in the above example of e-commerce the the user selects COD then we may want to skip the UPI or any other online payment option.
i = 1
for num in range (i,6):
if num == 3:
continue
print(num)
'''
Output - 1
2
4
5
'''
3. PASS - Pass is used for checking a condition and it does not do anything and acts like a placeholder.
Example -
i = 1
for num in range (i,6):
if num == 3:
pass
print(num)
'''
Output - 1
2
3
4
5
'''
6. NESTED LOOPS
A nested loop is a loop that is present inside a loop.
Example -
for i in range (1,6):
for j in range (1,6):
print("*",end="")
# By default, print() moves the cursor to a new line after each call.
# Using end="" prevents that, so the stars are printed on the same line.
'''Output -
*****
*****
*****
*****
*****
'''
7. DIFFERENCE BETWEEN PASS AND CONTINUE
continue→Skips the current iteration and moves to the next one.pass→ Does nothing, just acts as a placeholder, and execution continues normally.
8. PRACTICE QUESTIONS
Try solving these on your own 👇
Write a
forloop to print the first 10 even numbers.Print all characters of the string
"Python"using aforloop.Use a
whileloop to print numbers from 10 down to 1.Print the multiplication table of 7 using a loop.
Write a loop that calculates the sum of numbers from 1 to 100.
Print only odd numbers from 1 to 20 using a loop.
Write a nested loop to print a 3×3 matrix of numbers.
Use
continueto skip printing the number5from a range of 1–10.Write a program that uses
passinside a loop.Create a
whileloop that keeps asking the user to input a number until they type"stop".
9. CONCLUSION
Loops help in executing repetitive tasks efficiently.
forloop → Best when the number of iterations is known.whileloop → Best when the number of iterations is unknown.Loop control statements (
break,continue,pass) give more flexibility while controlling iterations.Mastering loops is essential, as they are the backbone of problem-solving in Python.
QUICK REVISION
| Statement | Use Case |
for | When the number of iterations is known |
while | When the number of iterations is unknown |
break | Exit the loop completely |
continue | Skip the current iteration |
pass | Placeholder – does nothing |
10. BONUS PRACTICE
Number Guessing Game
Generate a random number.
Keep asking the user to guess until they get it right.
Provide hints like "Too High" / "Too Low".
To-Do List Program
Keep asking for tasks until the user types
"done".Store and print the final to-do list.




