CodeLab Academy 📞 9936 6696
← Lessons 🐍 Python for Beginners

Loops: for and while

## for ```python for i in range(5): print(i) # 0 1 2 3 4 ``` `range(a, b)` — from a up to b-1, `range(a, b, step)` — with a step. ```python total = 0 for i in range(1, 11): total += i print(total) # 55 ``` ## while Repeats as long as the condition is true: ```python n = 12345 digits = 0 while n > 0: n //= 10 digits += 1 print(digits) # 5 ``` > ⚠️ Don't forget to change the condition inside a `while` loop, or you'll get an **infinite loop**!
← Previous Next →

💡 Want to track which lessons you've read and solve problems for points? Sign up for free or log in.