'Some one can explain to me how this code executes ? I still mess up with recursion

That output is supposed to be 4,3,2,1 right?

def test(x): 
 if x > 0 :
  test(x - 1)
  print(x)
test(4)
#output => 1
#output => 2
#output => 3
#output => 4


Solution 1:[1]

This is what a call stack is. Code is being pushed to the call stack and poping as we go. This is where recursive calls are done. see this for reference https://www.freecodecamp.org/news/how-recursion-works-explained-with-flowcharts-and-a-video-de61f40cb7f9/

test(4) is generating test(3), test(2), test(1), and pushing to the call stack. As we know Stack is FILO (First In Last Out). Last pushed recursion instruction will pop and execute first resulting print of 1. then 2,3 and lastly 4.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tipu Sultan Eiko