Hi, I am new here. Can someone please help me understand this code? I know the output is 6 but I don't understand what res and i are.
def func(x):
res=0
for i in range(x):
res+=1
return res
print(fund(4))
Hello and welcome to Python and the forums!
Next time please use
Python code tags, this time I have added them for you.
Have you ran the code you posted? The result I get is 4, and looking at the code, it seems right.
i is a variable that is assigned a value from
range(x) in each iteration. range(4) will give you 0, 1, 2 and 3. See more on
range in Python docs.
Res is a variable, which has an initial value of 0, but in each iteration of for loop, it gets incremented by 1. res += 1 is just shorthand for res = res + 1.