a = 10
def foo(x = a):
   print x

a = 5

Can someone explain why it prints 10 ?

There are 3 things going on with your script.
1. a is global variable assigned the value of 10.
2. You function is a function that has a default value set. that is global a which got the value of 10.
due to that


will print the default value which is

def foo(x=10)

because a=10. So when you call


it will return the default value.

Try and call foo(a) on line 6. This will print the current value of a which is 5.

3.Variable scope and function definations are very simple once you master them.
As you can see.


took no value. As long as you dont give a value to


.... It will keep printing the value of first a which is 10.

Try and change the value of first a and call


without a value to see.

Hope you get the idea. happy codding :)

Thanks for explaining :)