# Please explain the logic behind this

Please explain the logic behind this
0

a=0
for j in range (1,8,7), similar for (1,9,7), similar for (1,8,2)
a=a+j
print(a)

thanks

In the above code, The range function in python takes three arguments,

• 1.Starting Value(included)
• 2.Ending Value (excluded)
• 3.Steps (incrementation)
and returns a list of possible values.

range(1,8,7) means, it starts from value 1 and ends with value 8 which is not included and takes 7 steps for every iteration (after 1st iteration the value of j increments to 1+7 =8 ). so, it returns a list of [1].

similarly, for range(1,8,2), it takes two steps and returns a list of possible values as [1,3,5,7].

By following the above mechanism, try to solve the given code.

All the best!!!

1 Like

thank you so much for the solution.