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)
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.
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.
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.
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 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.
Local, global, and nonlocal scopes in Python:
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()
Global variables and functions in Python:
global_var = 10 def my_function(): print(global_var) my_function() # Output: 10
Local variables and block scope in Python:
def my_function(): local_var = 5 print(local_var) my_function() # Output: 5
Variable shadowing in Python:
global_var = 10 def my_function(): global_var = 5 # This shadows the global variable print(global_var) my_function() # Output: 5
Dynamic behavior of variable scope in Python:
dynamic_var = 10 dynamic_var = 'hello'
Using global keyword to modify variables in Python:
global
keyword is used to indicate that a variable refers to a global variable, allowing modification within a function.global_var = 10 def my_function(): global global_var global_var = 20 my_function() print(global_var) # Output: 20
Scope resolution in nested functions in Python:
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