In nested for loop, program first encounters the outer loop (here its Loop Y ), executing its first iteration.This first iteration triggers the inner, nested loop (it's Z loop here), which then runs to completion.Then the program returns back to the top of the outer loop, completing the second iteration and again triggering the nested loop.
Here, at first iteration, Y=0 and it will execute next line that is another loop.
now, z will set value 0, complete the addition,loop ends..
next iteration, z=1, complete the code and z increments..
....this inner loop will continue to do the job until z reaches 4. when z=4 it will complete that code and terminates the loop (as z range from 0 to 5, i.e 0 <5)
Inner loop terminates, and now, execution go back to outer loop once again, and this time Y=1 and executes next line
we're once again in z loop, z=0...x=x+y+z
So ,this is how actually nested loop works..
You can check this intermediate value to understand how this loop running.. here is the modified code
for y in range(0,5):
for z in range(0,5):
print("value of z", z)
print ("Value of x", x)
print("Value of Y", y)
print("Both loop ends...........")
print ("Final Result:" , x)
Hope this helps.. :-)