0

Could someone explain how the output is 6?

def func(x): res = 0 for i in range(x): res += i return res print(func(4))

20th Jun 2020, 5:25 AM
Zaine Walker
Zaine Walker - avatar
2 odpowiedzi
+ 6
0+1+2+3
20th Jun 2020, 5:27 AM
Oma Falk
Oma Falk - avatar
+ 1
When range is used like this: range(x) it generates the interval of integers [0, x) {0 included, x excluded}. Therefore, calling func(4) adds the numbers in [0, 4) so 0+1+2+3=6.
20th Jun 2020, 6:05 AM
Nboumakis