Function enclosure doubt

A.) def outer():
x=“local”
def inner():
print(x)
inner()
print(x)

B.)def outer():
x=10
def inner():
nonlocal x
x+=5
print(x)
inner()
print(x)

In case A we don’t require " nonlocal " keyword in it but in case B we require “nonlocal” keyword . they both are placed at same place in the code . is there any difference for using "nonlocal " keyword for string and inegers .

my question Is why in case A "nonlocal " keyword not used ?

Hey @Coder_sash2123, this is because of the fact that when you use the assignment operator (=) inside any function in python , so the variable associated becomes local to the function. So here in the second case if you don’t specify x as non local, it would give you an error in case of x+=5 , but it would not give the error in case you just print the value of x.

I hope it is clear. If it is not you can ask me again.
Happy coding :slightly_smiling_face:

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.