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
The output shows an error because
Python treats x as a local variable
and x is also not defined inside foo().
Reason
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
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.
 Global variables and local variables in the same code
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
we can only access the global variable but cannot
modify it from inside the function.
Reason
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 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
x = 50
def func():
global x
print('x is', x)
x = 2
print('Changed global x to', x)
func()
print('Value of x is', x)
None of the mentioned
D
11
Quiz
global : 1
Inside f() : 1
global : 1
Inside g() : 2
global : 1
Inside h() : 3
global : 3
12

Functions-(Global-Local-Nonlocal) (1).pptx

  • 1.
    Functions Part II 1.Global, Local and Nonlocal Variables 2. The global Keyword 3. The nonlocal Keyword 1
  • 2.
    Global, Local andNonlocal 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
  • 3.
     Global Variables Theoutput shows an error because Python treats x as a local variable and x is also not defined inside foo(). Reason To make this work we use global keyword: 3
  • 4.
    Global, Local andNonlocal 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
  • 5.
    Example 3: UsingGlobal 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.  Global variables and local variables in the same code 5
  • 6.
     Global variableand 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
  • 7.
    Global, Local andNonlocal 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
  • 8.
    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
  • 9.
    Global Keyword However, wemay have some scenarios where we need to modify the global variable from inside a function. Example 7: Modifying Global Variable From Inside the Function we can only access the global variable but cannot modify it from inside the function. Reason 9
  • 10.
    Global Keyword  Globalin 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
  • 11.
    Quiz x is 50 Changedglobal 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 x = 50 def func(): global x print('x is', x) x = 2 print('Changed global x to', x) func() print('Value of x is', x) None of the mentioned D 11
  • 12.
    Quiz global : 1 Insidef() : 1 global : 1 Inside g() : 2 global : 1 Inside h() : 3 global : 3 12