Lesson 4. For loop with range
1/12. Loops? Why do I need them?
Quite often programs need to repeat some block several times. That's where loops come in handy. To sum first5
numbers, for sure you
can write something like what you see in the code section.
Instructions
Click "Run" to see what happens in output!
But it is inconvenient. Imagine you had to sum the first 100 numbers, or do another bunch of similar calculations. You surely wouldn't write the 100 lines!
sum = 0 sum = sum + 1 sum = sum + 2 sum = sum + 3 sum = sum + 4 sum = sum + 5 print(sum)