codegurukool.com · Free coding tutorials & online compiler

Home › Python Tutorial › Python for and while loops

Python for and while loops

Repeat code.

for walks through any sequence; range(a, b) gives numbers from a up to (not including) b. while repeats while a condition is True. break stops a loop and continue skips to the next round.

Example code

for i in range(1, 4):
    print(i)
n = 3
while n > 0:
    print("n =", n)
    n -= 1
for ch in "ab":
    print(ch)

Run this code in the online editor

← Python matchPython functions →