Loops

Introduction

Loops are used to repeat code over a fixed number of cycles, or indefinitely based on logic. There are two main types of loops: for loops, and while loops.

For Loops

For loops run code iteratively, once per item, over each item in an iterable data type like a tuple or list.

my_list = [1,2,3,4,5]

for item in my_list:
    print(item)
1
2
3
4
5

While Loops

while loops, run code cyclically based on a conditional statement

ii = 0

while ii<5:
    print(ii)
    ii=ii+1
0
1
2
3
4