Python global , local and nonlocal variables declaration in functions and scope of the variables

preview_player
Показать описание
Global variable scopes are available within the main script. Inside the function the local variables are used and any variable declared inside the function is a local variable and it is not available outside the function.
Similarly if we change the variable inside the function, then the value won’t be affected outside the function as variable is declared as global.
Nonlocal
In nested function we can declare a variable as nonlocal so the value will change the value in parent function.
Рекомендации по теме
Комментарии
Автор

Sir. Its really very helpful for me. Thank you sir

anirbanrana
Автор

How come the variable n can be read from within the function in the first example? (00:45)
I dont understand the relation between global and local variables.
Is it so that IF the variable n is edited within the function, THEN a local instance of n is created? Otherwise the global instance is used (and can be read)?

hakonhove