
Understanding Global, Local, and Nonlocal Variables in Python Functions
Explore the concepts of global, local, and nonlocal variables in Python functions, understanding how variables are scoped and accessed, with examples and explanations provided. Learn how to use the global and nonlocal keywords to modify variables within functions effectively.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Functions Part II Functions Part II 1. Global, Local and Nonlocal Variables 2. The global Keyword 3. The nonlocal Keyword 1
Global, Local and Nonlocal variables Global Variables In Python, a variable declared outside of the function or in global scope is known as global variable. This means, global variable can be accessed inside or outside of the function. Example 1: Create a Global Variable we created x as a global variable and defined a foo() to print the global variable x. Finally, we call the foo() which will print the value of x. What if you want to change value of x inside a function? 2
Global Variables Reason The output shows an error because Python treats x as a local variable and x is also not defined inside foo(). To make this work we use global keyword: 3
Global, Local and Nonlocal variables Local Variables A variable declared inside the function's body or in the local scope is known as local variable. Example 2: Accessing local variable outside the scope we are trying to access a local variable y in a global scope whereas the local variable only works inside foo() or local scope. 4
Global variables and local variables in the same code Example 3: Using Global and Local variables in same code We declare x as a global and y as a local variable in the foo(). Then, we use multiplication operator * to modify the global variable x and we print both x and y. After calling the foo(), the value of x becomes global global because we used the x * 2 to print two times global. After that, we print the value of local variable y i.e local. 5
Global variable and Local variable with same name Example 4: Global variable and Local variable with same name We used same name x for both global variable and local variable. We get different result when we print same variable because the variable is declared in both scopes, i.e. the local scope inside foo() and global scope outside foo(). When we print the variable inside the foo() it outputs local x: 10, this is called local scope of variable. Similarly, when we print the variable outside the foo(), it outputs global x: 5, this is called global scope of variable. 6
Global, Local and Nonlocal variables Nonlocal Variables Nonlocal variable are used in nested function whose local scope is not defined. This means, the variable can be neither in the local nor the global scope. Example 5: Create a nonlocal variable In the above code there is a nested function inner(). We use nonlocal keyword to create nonlocal variable. The inner() function is defined in the scope of another function outer(). 7
Global Keyword In Python, global keyword allows you to modify the variable outside of the current scope. It is used to create a global variable and make changes to the variable in a local context. Rules of global Keyword When we create a variable inside a function, it s local by default. When we define a variable outside of a function, it s global by default. You don t have to use global keyword. We use global keyword to read and write a global variable inside a function. Use of global keyword outside a function has no effect Example 6: Accessing global Variable From Inside a Function 8
Global Keyword However, we may have some scenarios where we need to modify the global variable from inside a function. Example 7: Modifying Global Variable From Inside the Function Reason we can only access the global variable but cannot modify it from inside the function. 9
Global Keyword Global in Nested Functions Example 8: Using a Global Variable in Nested Function In the above program, we declare global variable inside the nested function bar(). Inside foo() function, x has no effect of global keyword. Before and after calling bar(), the variable x takes the value of local variable i.e x = 20. Outside of the foo() function, the variable x will take value defined in the bar() function i.e x = 25. This is because we have used global keyword in x to create global variable inside the bar() function (local scope). If we make any changes inside the bar() function, the changes appears outside the local scope, i.e. foo(). 10
Quiz x is 50 Changed global x to 2 Value of x is 50 A x = 50 x is 50 Changed global x to 2 Value of x is 2 def func(): global x print('x is', x) x = 2 print('Changed global x to', x) B x is 50 Changed global x to 50 Value of x is 50 C func() print('Value of x is', x) D None of the mentioned 11
Quiz global : 1 Inside f() : 1 global : 1 Inside g() : 2 global : 1 Inside h() : 3 global : 3 12