In the first snippet you make res==0 before the loop and then only add i in every iteration of the loop. So at the end it's the sum of all values in range(x)
In the second example you make res==0 in every iteration. So at the end res value is equal to the last value of i.
You can use http://www.pythontutor.com/visualize.html#mode=edit to visualise the execution of the code step by step for better understanding
In the second example you make res==0 in every iteration. So at the end res value is equal to the last value of i.
You can use http://www.pythontutor.com/visualize.html#mode=edit to visualise the execution of the code step by step for better understanding
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs