
Understanding Global and Local Variables in Python Functions
Explore the concept of global and local variables in Python functions. Global variables are declared outside functions and can be accessed globally, while local variables are defined inside functions with limited scope. Learn how to work with global and local variables, as well as using the global keyword to modify global variables from within functions.
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 Functions Part II Part II 1. Global & Local Variables 2. The global Keyword 3. The nonlocal Keyword 1
Global and Local 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 (read & used) inside or outside of the function. Example 1: Create a Global Variable we created x as a global variable and defined a fun() to print the global variable x. Finally, we call fun() which will print the value of x. some global value >>> some global value >>> 2
Global and Local 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 some local value >>> If we are trying to access a local variable y in a global scope whereas the local variable only works inside fun() or local scope. Traceback (most recent call last): File test.py", line 5, in <module> print(y) NameError: name 'y' is not defined 3
Global and Local variables What if we want to change the value of a global variable inside a function? i.e. change x inside fun()? 4
Global Variables Example 3: Modifying/changing a global variable inside a function Traceback (most recent call last): File test.py", line 7, in <module> fun() File test.py", line 4, in fun x *= 2 UnboundLocalError: local variable 'x' referenced before assignment >>> Reason The output shows an error because Python treats x as a local variable and x is also not defined inside fun(). To make this work, we need to use the global keyword: "some global x""some global x" >>> What about global & local variables in the same code? 5
Global variables and local variables in the same code Example 4.1: Using Global and Local variables in same code global ? local ? >>> We declared x as a global and y as a local variable in the fun() function. 6
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. 7
Global variables and local variables in the same code Example 4.2: Using/Modifying Global and Local variables in same code global ?global ? local ? >>> We declare x as a global and y as a local variable in the fun() function. Then, we use multiplication operator * to modify the global variable x and we print both x and y. After calling fun(), 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 ? . 8
Global variable and Local variable with same name Example 5: Global variable and Local variable with same name inside fun(), x = 10 outside fun(), x = 5 >>> 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 fun() and global scope outside fun(). When we print the variable inside fun(),its value x = 10, this is called local scope of variable. Similarly, when we print the variable outside fun(), its value x = 5, this is called global scope of variable. 9
Global Keyword Global in Nested Functions Example 6: 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
Summary: global Keyword In Python, global keyword allows you to modify a global variables from within a function. It can be 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 be able to change a global variable inside a function. The use of global keyword outside of a function has no effect 11
Quiz 1 x is 50 Changed global x to 2 Value of x is 50 A x is 50 Changed global x to 2 Value of x is 2 B x is 50 Changed global x to 50 Value of x is 50 C D None of the mentioned 12
Quiz 2 x is 50 Changed global x to 2 Value of x is 50 A x is 50 Changed global x to 2 Value of x is 2 B x is 50 Changed global x to 50 Value of x is 50 C D None of the mentioned 13
Quiz 3 Changed global x to 2 Value of x is None A Changed global x to 2 Value of x is 2 B C None of the mentioned 14
Quiz 4 #1: global a = 1 Inside f(): a = 1 #2: global a = 1 Inside g(): a = 2 #3: global a = 1 Inside h(): a = 3 #4: global a = 3 >>> 15
Next Lecture: global, local & 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 in the global scope. Example 6: Create a nonlocal variable inside inner_fun(), x = nonlocal ? inside outer_fun(), x = nonlocal ? x = global ? >>> In the above code there is a nested function inner_fun(). We use nonlocalkeyword to create nonlocal variable. The inner_fun() function is defined in the scope of another function outer_fun(). 16
References Global, Local and nonlocal Variables: https://www.python-course.eu/python3_global_vs_local_variables.php Defining Functions: https://docs.python.org/3/tutorial/controlflow.html#defining-functions 17