Variables in Python

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Video Tutorial

Variables

Overview

A variable in Python is a reserved memory location to store values. In other words, a variable in a python program gives data to the computer for processing. Declaring and re-declaring a variable in Python is as easy as writing names and assigning values to it.

Scope of article

  • In this topic, we will get familiar with variables and use them in Python.
  • Then, we will learn how to declare, re-declare and delete a variable.
  • We will also learn about different types of variables in Python.

Introduction to Python Variables

As kids, we all got excited (sadly, only initially) when we saw alphabets being used in Mathematics.

Yes, you thought it was right! I am talking about Algebra here! In the beginning, we all were curious about the x’s and the y’s and soon started enjoying the process of finding their values.

python variables Well, guess what? The x and y in Algebra were nothing but variables!

The world of programming, just like Algebra, has its own sets of variables too. And today, in this article, we will deep dive specifically into Python variables.

To put it simply, variables in Python are named memory locations. This named memory location stores data. Hence, a variable is used to retrieve this data, and its value may change during the program.

variables in python are named memory locations

Variable Naming Convention

Variables in Python are a type of identifier. Identifiers are widely used to identify literals in a program.

Rules for naming a variable are as follows –

  • The variable name should either begin with an Uppercase(A to Z) or Lowercase(a to z) character or an underscore(_).
  • One should always remember to use a meaningful name for variables in Python. For example – no_of_chocolates makes more sense than noc.
  • Which brings us to the next point. If a variable has multiple words, it is advised to separate them with an underscore.
  • One should ensure that a variable name should not be similar to keywords of the programming language.
  • One should also remember that even variable names are case-sensitive.
  • A variable should not begin with a digit or contain any white spaces or special characters such as #,@,&.
  • Example of good variable names – my_name, my_dob.
  • Example of bad variable names – #n, 22dob.

How to Declare a Variable in Python?

There is no binding in Python to declare variables before we use it. We also need not explicitly declare variables with their data type. When we assign a value to Python variables, they are automatically declared.

We use the “=” operator for value assignment.

For example

If we want to declare a variable that should hold the total number of chocolates we have, which is 10, we can do it in the following way –

How to re-declare a Variable in Python?

Variables in Python can also be redeclared with ease. We assign a new value to the variable before it is used again.

Let’s look at an example to understand this better –

Output

The output shows that the variable was redeclared, and the updated value was printed to the console.

Assigning a single value to variables in Python

Instead of declaring Python variables with the same values one after the other, we can use the following technique to assign a single value to multiple variables –

The above code's output should obviously be the same value printed thrice to the console.

Let’s see what it looks like –

Multiple assignments

Another ease offered by Python variables is multiple assignments!

What do multiple assignments mean? Well, to put it simply, we can declare multiple variables in Python in just one line!

Take a look at the following code snippet to understand this better –

The output of the above snippet will be the same as the single-line assignment –

So clearly, we can easily perform multiple assignments for Python variables just with the , operator.

Types of Variables in Python

Consider a hypothetical scenario: Variables are like you and me, too, like to travel. If I wish to travel to Canada, I would need a VISA and my passport. But if I wish to travel to any place within India, I do not require a VISA. Hence, these travels are global and local, respectively.

Now, back to the beginning, we considered the variables to be like us. So even variables have two scopes- global and local. And thus, their utility and mode of access are determined by their nature-i.e, whether local or global.

Hence, through this example, it is illustrated that variables in Python can be of two types –

global and local scope in python variable

1. Local Variable in Python

The variables declared inside a function and used only within the scope of that particular function are known as local variables.

Let’s look at an example to understand this better –

Output –

The local variables num1 and num2 are defined within the function sum and can be referenced only within the scope of sum.

If we try to refer, say num1, outside the scope –

We would get the following error –

2. Global Variable in Python

The variables that can be used throughout the program and its scope are global, i.e. available within the entire codebase are known as global variables.

Any variable declared outside any particular function is considered a global variable.

Let’s modify the local variable example to illustrate a global variable and its scope.

Output

From the output, we can make out that num has acted as a global variable as we have used it inside and outside the sum() function and we were able to print its value to the console as well.

Learn More About Types of Variables in Python

How to Delete a Variable in Python?

Just like declaring and using variables in Python is so easy, even deleting a Python variable is not a difficult task either.

Python automatically deletes variables and functions when they can no longer be used, freeing the memory. The user can also manually delete variables and functions. It can be useful when large data structures are no longer needed since deleting them will free memory for other uses. It is why Python offers its users the delete functionality.

You can delete a variable using the del command in Python.

Let’s take a look at how we do it practically and what is the output that we get –

Here, we have re-used our total number of chocolates example. We declared it initially and now we are deleting it.

Output –

Using the del command, we have successfully deleted the variable no_of_chocolates.

Object References

The topic of variables in Python is incomplete without understanding how the Python interpreter works when we declare a variable. It is slightly different when compared to other programming languages.

We all know that Python is an object-oriented language. Hence, every object belongs to a specific class.

Consider the following example –

We have declared a variable and assigned a string in the above example. Now, we print it to the console first. In the last line of the code, you will notice that we have used the type function.

What is the type function doing? Let’s take a look –

python variables have their unique ids We can see that a string object is created of the class str! Thus, we can say that variables in Python are symbolic names that point to an object, just like the example we looked at.

Object Identity

Now that we have understood that variables in Python point to their respective objects let’s try to understand how objects are uniquely identified.

Python ensures that no two variables will have the same identifier or id. The built-in Python function id() is used to identify the object id or identifier.

Let’s consider the same example that we used to understand object references –

As we can see, we have two different variables- message and final_message. Using the id() function, we are trying to see whether these identifiers are unique or not.

Let’s see what we found –

Just like we discussed, the two Python variables have their unique ids.

string object python variable

Conclusion

Now that we have reached the end of the article, I am sure you have learnt that a simple concept learnt in school has its own upgraded version in the programming world!

  • Variables in Python are named memory locations used to store data.
  • A variable is used to retrieve the stored data, and its value may change during the program.
  • We use the “=” operator for value assignment.
  • There are 2 types of variables in Python -
    • Local Variables
    • Globbal Variables
  • Python automatically deletes variables and functions when they can no longer be used, freeing the memory.
  • You can manually delete a variable using the del command in Python.

That’s all for today, folks! Happy coding!

See Also: