Python has two loop commands — while and for loops. The while loop executes as long as a condition is true.
i = 0
while i < 6:
i += 1
if i == 3:
continue
if i == 5:
break
print(i)
1
2
4
The for loop is used for iterating over a sequence (list, tuple, dictionary, set, or string).
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
continue
print(x)
apple
cherry
# range(2, 6) is 2, 3, 4, 5
# i.e. there are 6 - 2 = 4 numbers starting from 2 and excluding 6
for x in range(2, 6):
print(x)
else:
print("Finally finished!")
2
3
4
5
Finally finished!
Three statements — break, continue and else — are used to control the iterations. The break statement stops and exits the loop before it’s conclusion. The continue statement stops the current iteration to move to the next. The else statement runs a block of code once the condition no longer is true.
Use the Search Bar to find content on MarketingMind.
Contact | Privacy Statement | Disclaimer: Opinions and views expressed on www.ashokcharan.com are the author’s personal views, and do not represent the official views of the National University of Singapore (NUS) or the NUS Business School | © Copyright 2013-2025 www.ashokcharan.com. All Rights Reserved.