Sobes.tech
Junior — Middle

What types of loops are implemented in Python?

sobes.tech AI

Answer from AI

Python implements two main types of loops:

  1. for loop — used for iterating over elements of sequences (lists, strings, tuples, dictionaries, ranges, etc.).
for i in range(5):
    print(i)
  1. while loop — 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.

What types of loops are implemented in Python? — QA /… - sobes.tech