Python Variables

Summary: in this tutorial, you will learn about Python variables, how to declare variables and understand about variable the scopes concept.

What are Python variables

By definition, a variable is something that may change often in your program. First, you use variables to refer to various kinds of data such string, integer, list, etc. Then you manipulate this data through variables.

Declaring Python variables

There are two phases of creating a variable:

  1. First, you need to initialize a variable by creating a container and assign it a name. This is called initialization.
  2. Second, you assign value to the the variable. This is known as assignment.

Because Python is dynamically typed language, you don’t have to pre-declare variable or its type. In Python, both initialization and assignment are done using a single assignment statement.

The type of the variable is initialized on assignment using the assignment operator (=).  Each variable is referred to as a name called identifier.

The following example illustrates how to declare a variable:

If you have been programming in other languages such as C/C++or Java, you will notice that a variable in Python does not require an explicit associated data type.

Python variables can be set to any object with different type. Let’s take a look at another example:

First, we defined a m variable and assign its value to 10. Then, we assigned a string to m, which is perfectly correct in Python. The rule is that the new assignment overrides the old assignment.

Python provides del statement to delete a variable. If you are trying to get the value of a variable after it is deleted, you will get an error.

Notice that variable name in Python is case sensitive, meaning  that variable  m is different from variable M.

Variable scopes

Scope of a variable is a context in the program in which variable name is valid and can be referred to. Variable scope is also known as variable visibility. In Python, variables can have either have local or global scope.

Global vs. Local variables

Let’s take a look at the following example before going into details of global and local variables:

Variables are defined inside a function have local scope whereas those are defined in a module have global scope.

In this example, variable  m  is a global variable. You can refer to it in any functions within the script. Its lifespan lasts during program execution.

Variable  n is a local variable because it is defined within the  f1() function Its lifespan begins when entering into the  f1() function and ends when the  f1() function completes. You cannot refer to the n variable outside the  f1() function.

Variable scope resolution

Let’s take a look at the following example:

What will display in the output? In this example, a global variable and local variable share the same name

When you call the  print(m) function, Python searches for the m variable within its scope therefore the global variable m is referred to. However, when you call  the  f1() functionPython will use local variable m inside the function.

The rule is that, when looking for a variable, Python searches the local scope first. If the variable is not found within the local scope, it continue to search in the global scope. If the variable still not found, it raises a NameError exception.

global statement

The global variables can be either used or abused. It is difficult to control what function changes the global variable. In order to make it clear, Python provides the global keyword to help you refer to global variables explicitly inside a function.

See the following example:

Inside the f1() function, we refer to the global variable m by using the global keyword.

In this tutorial, we have shown you how to use Python variables in your code.

  • Was this tutorial helpful ?
  • YesNo