Python Forum
Understanding the code - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Understanding the code (/thread-13594.html)



Understanding the code - Eugine - Oct-22-2018

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))



RE: Understanding the code - j.crater - Oct-22-2018

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.