Python Tutorial

Python Variable

Python Operators

Python Sequence

Python String

Python Flow Control

Python Functions

Python Class and Object

Python Class Members (properties and methods)

Python Exception Handling

Python Modules

Python File Operations (I/O)

Python variable scope

In Python, variables have different scopes, which determine where they can be accessed within the code. Understanding variable scope is important for managing and organizing your code. In this tutorial, we will learn about variable scope in Python, covering local, nonlocal, and global variables.

  • Local scope:

A variable defined inside a function has a local scope, which means it can only be accessed within that function.

Example:

def local_example():
    local_var = "I'm a local variable"
    print(local_var)

local_example()  # Output: I'm a local variable
print(local_var)  # Error: NameError: name 'local_var' is not defined

In this example, local_var is defined within the local_example function and has a local scope. It can be accessed within the function but not outside it.

  • Global scope:

A variable defined outside any function has a global scope, which means it can be accessed from anywhere in the code, including inside functions.

Example:

global_var = "I'm a global variable"

def global_example():
    print(global_var)

global_example()  # Output: I'm a global variable
print(global_var)  # Output: I'm a global variable

In this example, global_var is defined outside any function and has a global scope. It can be accessed both inside the global_example function and outside it.

  • Using the global keyword:

If you want to modify the value of a global variable within a function, you need to use the global keyword before the variable.

Example:

counter = 0

def increment_counter():
    global counter
    counter += 1

increment_counter()
print(counter)  # Output: 1

In this example, we use the global keyword within the increment_counter function to indicate that we want to modify the global variable counter.

  • Nonlocal variables:

Nonlocal variables are used in nested functions when you want to access and modify a variable defined in an outer (enclosing) function.

Example:

def outer_function():
    outer_var = "I'm an outer variable"

    def inner_function():
        nonlocal outer_var
        outer_var = "I'm modified by inner_function"
        print(outer_var)

    inner_function()
    print(outer_var)

outer_function()
# Output:
# I'm modified by inner_function
# I'm modified by inner_function

In this example, we have a nested inner_function within the outer_function. We use the nonlocal keyword in inner_function to access and modify the outer_var variable defined in the enclosing outer_function.

In summary, variables in Python have different scopes depending on where they are defined. Local variables are defined within functions and can only be accessed there, while global variables are defined outside functions and can be accessed anywhere in the code. The global keyword is used to modify global variables within functions, and the nonlocal keyword is used to access and modify variables in enclosing functions when dealing with nested functions.

  1. Local, global, and nonlocal scopes in Python:

    • Description: Python has three types of scopes: local, global, and nonlocal. Local variables are defined inside a function, global variables outside any function, and nonlocal variables in nested functions.
    • Code:
      global_var = 10  # Global scope
      
      def my_function():
          local_var = 5  # Local scope
          
          def nested_function():
              nonlocal nonlocal_var
              nonlocal_var = 20  # Nonlocal scope
              
          nested_function()
      
      my_function()
      
  2. Global variables and functions in Python:

    • Description: Global variables are defined outside any function and can be accessed from anywhere in the program, including within functions.
    • Code:
      global_var = 10
      
      def my_function():
          print(global_var)
      
      my_function()  # Output: 10
      
  3. Local variables and block scope in Python:

    • Description: Local variables are defined within a block or function and have scope limited to that block or function.
    • Code:
      def my_function():
          local_var = 5
          print(local_var)
      
      my_function()  # Output: 5
      
  4. Variable shadowing in Python:

    • Description: Variable shadowing occurs when a local variable has the same name as a variable in an outer scope, causing the inner variable to "shadow" the outer one.
    • Code:
      global_var = 10
      
      def my_function():
          global_var = 5  # This shadows the global variable
          print(global_var)
      
      my_function()  # Output: 5
      
  5. Dynamic behavior of variable scope in Python:

    • Description: Python's dynamic typing allows variables to change type dynamically, but their scope remains constant once defined.
    • Code:
      dynamic_var = 10
      dynamic_var = 'hello'
      
  6. Using global keyword to modify variables in Python:

    • Description: The global keyword is used to indicate that a variable refers to a global variable, allowing modification within a function.
    • Code:
      global_var = 10
      
      def my_function():
          global global_var
          global_var = 20
      
      my_function()
      print(global_var)  # Output: 20
      
  7. Scope resolution in nested functions in Python:

    • Description: Nested functions have access to variables in their own scope, the scope of the outer function, and the global scope.
    • Code:
      global_var = 10
      
      def outer_function():
          outer_var = 5
      
          def inner_function():
              nonlocal outer_var
              print(outer_var + global_var)
      
          inner_function()
      
      outer_function()  # Output: 15