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

Python Local and Nonlocal Variables

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.

  1. Local Variables
  2. Global Variables
  3. Nonlocal Variables

1. Local Variables

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)

2. Global Variables

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

3. Nonlocal Variables

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.

  1. Scope of variables in Python:

    • Description: The scope of a variable defines where in the code it can be accessed or modified.
    • Example Code:
      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
      
  2. How to declare local variables in Python:

    • Description: Local variables are declared inside a function and are only accessible within that function.
    • Example Code:
      def my_function():
          local_variable = 30
          print(local_variable)
      
      my_function()
      
  3. Nonlocal variables in Python explained:

    • Description: Nonlocal variables are used in nested functions to access variables in the enclosing (non-global) scope.
    • Example Code:
      def outer_function():
          outer_variable = 40
      
          def inner_function():
              nonlocal outer_variable
              outer_variable += 5
              print(outer_variable)
      
          inner_function()
      
      outer_function()
      
  4. Using nonlocal keyword in Python:

    • Description: The nonlocal keyword is used to indicate that a variable is nonlocal, allowing modifications in the enclosing scope.
    • Example Code:
      def outer_function():
          outer_variable = 50
      
          def inner_function():
              nonlocal outer_variable
              outer_variable += 10
              print(outer_variable)
      
          inner_function()
      
      outer_function()
      
  5. Python local variable vs global variable:

    • Description: Local variables are limited to the function they are declared in, while global variables are accessible throughout the entire program.
    • Example Code:
      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
      
  6. Examples of local and nonlocal variables in Python:

    • Description: Demonstrating both local and nonlocal variables in different functions and scopes.
    • Example Code:
      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()
      
  7. Nested functions and variable scope in Python:

    • Description: Nested functions can access variables from their enclosing scopes, creating a hierarchy of variable scopes.
    • Example Code:
      def outer_function():
          outer_variable = 100
      
          def inner_function():
              inner_variable = 110
              print(outer_variable + inner_variable)
      
          inner_function()
      
      outer_function()