Python Tutorial
Python Flow Control
Python Functions
Python Data Types
Python Date and Time
Python Files
Python String
Python List
Python Dictionary
Python Variable
Python Input/Output
Python Exceptions
Python Advanced
In this Python Local and Nonlocal Variables tutorial, we'll cover the concepts of local, nonlocal, and global variables in Python, and how to use them in functions.
Local variables are variables that are defined within a function and are accessible only within that function. They are created when the function is called and destroyed when the function returns.
Example:
def my_function(): local_var = 10 print("Inside the function, local_var =", local_var) my_function() # Output: Inside the function, local_var = 10 # The following line will raise a NameError, as the variable is not defined outside the function print(local_var)
Global variables are defined outside a function and can be accessed and modified by any function in the program.
Example:
global_var = 20 def my_function(): print("Inside the function, global_var =", global_var) my_function() # Output: Inside the function, global_var = 20 print("Outside the function, global_var =", global_var) # Output: Outside the function, global_var = 20
To modify a global variable from within a function, you need to use the global
keyword:
global_var = 20 def my_function(): global global_var global_var = 30 print("Inside the function, global_var =", global_var) my_function() # Output: Inside the function, global_var = 30 print("Outside the function, global_var =", global_var) # Output: Outside the function, global_var = 30
Nonlocal variables are used in nested functions, where a variable is defined in the outer function and is accessed by the inner function. The nonlocal
keyword is used to indicate that a variable is not local to the inner function and should be used from the enclosing scope.
Example:
def outer_function(): outer_var = 40 def inner_function(): nonlocal outer_var outer_var = 50 print("Inside inner_function, outer_var =", outer_var) inner_function() print("Inside outer_function, outer_var =", outer_var) outer_function() # Output: # Inside inner_function, outer_var = 50 # Inside outer_function, outer_var = 50
In this tutorial, you learned about local, global, and nonlocal variables in Python and how to use them in functions. Local variables are defined within a function and can only be accessed within that function. Global variables are defined outside any function and can be accessed by any function in the program. Nonlocal variables are used in nested functions to access a variable from an enclosing scope.
Scope of variables in Python:
global_variable = 10 def my_function(): local_variable = 20 print(global_variable) # Accessing global variable print(local_variable) # Accessing local variable my_function() print(global_variable) # Accessing global variable outside the function
How to declare local variables in Python:
def my_function(): local_variable = 30 print(local_variable) my_function()
Nonlocal variables in Python explained:
def outer_function(): outer_variable = 40 def inner_function(): nonlocal outer_variable outer_variable += 5 print(outer_variable) inner_function() outer_function()
Using nonlocal keyword in Python:
nonlocal
keyword is used to indicate that a variable is nonlocal, allowing modifications in the enclosing scope.def outer_function(): outer_variable = 50 def inner_function(): nonlocal outer_variable outer_variable += 10 print(outer_variable) inner_function() outer_function()
Python local variable vs global variable:
global_variable = 60 def my_function(): local_variable = 70 print(global_variable) # Accessing global variable print(local_variable) # Accessing local variable my_function() print(global_variable) # Accessing global variable outside the function
Examples of local and nonlocal variables in Python:
def outer_function(): outer_variable = 80 def inner_function(): nonlocal outer_variable inner_variable = 90 outer_variable += inner_variable print(outer_variable) inner_function() outer_function()
Nested functions and variable scope in Python:
def outer_function(): outer_variable = 100 def inner_function(): inner_variable = 110 print(outer_variable + inner_variable) inner_function() outer_function()