← Learn to Code — Python for Beginners
ମଡ୍ୟୁଲ୍ · Repeating Yourself
for loops
A loop repeats a set of instructions without you writing them out again and again.
for i in range(5):
print(i)
This prints 0 1 2 3 4 — range(5) gives five numbers, starting at 0. Python counts from 0 by default, which trips up almost everyone at first.
You can also loop directly over a list of things:
branches = ["CSE", "ETC", "Mechanical", "Civil", "Electrical"]
for b in branches:
print(b)
Try it: the starter code prints the first 10 square numbers — run it, then change it to print cubes instead.