Junior — Middle
What types of loops are implemented in Python?
sobes.tech AI
Answer from AI
Python implements two main types of loops:
forloop — used for iterating over elements of sequences (lists, strings, tuples, dictionaries, ranges, etc.).
for i in range(5):
print(i)
whileloop — executes a block of code as long as the condition is true.
count = 0
while count < 5:
print(count)
count += 1
Both loops support break (to exit the loop) and continue (to skip to the next iteration). Additionally, Python allows the use of generators and generator expressions for lazy iteration.
Thus, Python provides flexible constructs for executing code in a loop.