Reputation: 35
def fibonacci(n):
if n == 0 or n == 1: # base c a s e s
return 1
else:
return fibonacci(n-1) + fibonacci(n-2)
print(fibonacci(10))
When fibonacci(10) is called , is fibonacci(n-1) resolved first till the base case and after that,fibonacci(n-2) is resolved?
Upvotes: 0
Views: 79
Reputation: 1038
From the docs:
Python evaluates expressions from left to right.
So yes, the evaluation order for fibonacci(n-2) + fibonacci(n-1)
is as you describe: first fibonacci(n-2)
is evaluated to a value, then fibonacci(n-1)
is evaluated to a value, and the entire expression evaluates to the sum of these two values.
There’s nothing special about recursion in this case. Python evaluates left-to-right even if the operands aren’t recursive function calls.
Upvotes: 1