Local and Global Variables in Python. Topic – 33

Local and Global Variables in Python

Local and Global Variables in Python

In this topic we will learn about local and global variables in Python programming language. If this concept is not understood properly, then unexpected errors may occur in your program. You need to study this topic so that we know when and how to define a variable. So let’s see in the code.

Before moving forward in this concept, let’s first recall what a variable is in Python.

A variable is a named location in memory that stores a value. In Python, we can assign values to variables using the assignment operator =. For example:

x = 5
y = "Hello, World!"

Now let’s talk about local and global variables in Python.

Local Variable:

A local variable is a variable that is defined within a function and is only accessible within that function. It is created when the function is called and is destroyed when the function returns. We can’t access the variable outside of the function. For example:

def my_function():
  val = 5 # local variable
  print("Local variable", val) # access local variable 


If we define a value in the function and try to access this value outside of the function, it will throw an unexpected error. For example:

def local_variable():
  local_val = 5 # local variable

# NameError: name 'local_val' is not defined

Key Points:

  •     Local variables are variables declared inside a function.
  •     They are only accessible within the function in which they are defined.
  •     When a function is called, a new local scope is created, and any local variables declared within that function exist only within that scope.
  •     Once the function execution is complete, the local variables and their values are destroyed.

Global Variables:

The global keyword is used to declare that a variable is a global variable and should be accessed from the global scope. Here’s an example:

Global means the entire world, and in the context of code, it refers to the entire code file. A global variable can be accessed from anywhere within the code file, whether you’re inside or outside a function. For example:

global_var = 40 # global variable

def global_variable():
  print("Global variable", global_var)

# output: Global variable 40

Key Points:

  •     Global variables are variables declared outside of any function.
  •     They are accessible from anywhere in your program, including inside and outside of functions.
  •     Use global variables with caution, as they can lead to unintended side effects if modified within functions. Generally, it’s better practice to use local variables and pass necessary data as arguments to functions.

Remember, in most cases, it’s recommended to use local variables and pass arguments to functions to avoid unintended side effects and promote code clarity.

Working With OS Module in Python

Working With OS Module in Python

OS module is one of the most useful modules in a Python programming language. In this post, we will learn about …

I hope you understand local and global variables in Python. Need guidance or have questions? Drop a comment below. Share your email for a personalized touch—I’ll send exclusive resources and answer queries. Let’s code together, creating efficient code and memorable learning moments! 🚀

Finite number of  lines code and infinite possibilities

Programmer, Arpan Bera

Leave a Reply

Your email address will not be published. Required fields are marked *

Arpan Bera

Coding is like Love – everyone can start, but only a few finish without errors

Keep Updated to our News and Blog