Using range function with for loop

Using range function with for loop:
The range() function can be combined with the ‘for loop’ to supply the numbers required by the loop.

In the following example, the range(1,x+1) provided the numbers 1 to 50 needed by the ‘for loop’ to add the sum of 1 until 50:

x=50
total=0
total=total+number
print("Sum of 1 until %d:%d"%(x,total))

OUTPUT:
Sum of 1 until 50:1275
Translate »