+ 1

# Can anyone make an explanation about this code?

def func(x): res = 0 for i in range(x): res += i return redprint (func(4))Result: 6This was one of the questions I had during the quizzes. Well I saw the answer because of the comments but I didn't find a good reason to believe that's really truth.

3 Answers

+ 1

range(4) will create a range of 0,1,2,3 since it is zero based.
Expanding we will do:
res += 0 #res is 0
res += 1 #res is 1
res += 2 #res is 3
res += 3 #res is 6

+ 1

iam not 100% sure and i have asked that question before
you can replace i by x and gives the same answer then res+=i is mean you multiply the var. x by 2

- 1

that is the answer