coding180 icon
Coding180
A Beginner's Guide to Understanding Variable Scope in Python Functions

Understanding Variable Scope in Python Functions

Home > Tutorials > Python for beginners > Functions


Python is a popular programming language used in many industries today. However, understanding how Python handles variable scope in functions can be challenging for beginners.

What is variable scope?

Variable scope refers to where a variable can be accessed within a program. Variables have two primary scopes in Python: global and local.

  • Global variables: These variables are defined outside any function and can be accessed from anywhere in the program.

  • Local variables: These variables are defined inside a function and can only be accessed within that function.

Understanding variable lifetime

The lifetime of a variable refers to how long it exists in memory. Once a variable goes out of scope (either because the block of code containing the variable has completed execution or the function that contained the variable has returned), the memory allocated to that variable is freed up for other use.

Global vs. Local variables

It's important to understand the difference between global and local variables when working with Python functions.

Global Variables

Global variables are defined outside any function and can be accessed from anywhere in the program. Here's an example:

x = 10

def my_function():
  print(x)

my_function()

In this example, x is a global variable. The function my_function() prints the value of x, which is 10.

Local Variables

Local variables, on the other hand, are defined within a function and can only be accessed from within that function. Here's an example:

def my_function():
  x = 10
  print(x)

my_function()

In this example, x is a local variable. The function my_function() prints the value of x, which is 10.

Using the global keyword

To access a global variable from within a function, you can use the global keyword. Here's an example:

x = 10

def my_function():
  global x
  x = 20

my_function()

print(x)

In this example, the global keyword tells Python to use the global variable x instead of creating a new local variable with the same name. The output of this code will be 20 because the value of the global variable has been changed within the function.

Examples and Exercises

Now that you understand variable scope in Python functions, let's look at some examples and exercises to help solidify your understanding.

Example 1

y = 5

def my_function():
  y = 10
  print(y)

my_function()

print(y)

In this example, y is a global variable set to 5. The function my_function() defines a local variable also named y with a value of 10. When the function is called, it prints out 10. After the function call, the global variable y is printed, which still has a value of 5.

Exercise 1

z = 15

def my_function():
  global z
  z = 20

my_function()

print(z)

In this exercise, z is a global variable set to 15. The function my_function() uses the global keyword to change the value of the global variable to 20. After the function call, the new value of z is printed which should be 20.

Conclusion

Understanding variable scope in Python functions is essential for writing efficient and effective code. Global variables can be accessed from anywhere in the program, while local variables are only accessible within the function in which they were defined. Using the global keyword allows you to modify global variables from within a function. With practice and experimentation, you'll quickly get the hang of variable scope in Python functions.


user

Robort Gabriel

Lagos, Nigeria

Freelance Web Developer, Native Android Developer, and Coding Tutor.

Skills
  • UI / UX
  • JavaScript
  • Python
  • PHP
  • Kotlin
  • Java
  • Bootrap
  • Android
  • Laravel