Variables in Python

This lesson deals with variables. Those who already know some programming must be familiar with the concept of variable and its importance. Variable is an important element of programming world, visually, you can consider variable as a box, that is capable of storing some value within it.

In more formal terms, variables are used to store information to be referenced and manipulated in a computer program.

They also provide a way of labelling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that hold information. Their sole purpose is to label and store data in the memory. This data can then be used throughout your program.

Let's see some examples. A variable is supposed to have a name. There are some rules to assign a name to the variable.

  • The variable name can consist of alphabet(s), number(s) and underscore(s) only.
  • The first character in variable's name cannot be a number. Hence i-am-variable, variable!, 1variable, #variable, are all invalid variable names. While i_am_variable, variable, variable1, _variable, are all valid names.

Storing value in a variable

Time to see how we can store a value in our variable. Consider a variable (or a box, if you like it casually) named x. We want this variable (or box) to store a number, say 11. Then in order to do that just go to IDLE and type:

>>> x = 11

And press Enter key. With that our variable gets created, named x, and with a default value 11 stored in it, using the equal to = operator. Remember, Equal to operator is always used to assign a specific value to any variable. Variable's name will always be on the left side and it's value will be on the right. Let's create another variable, say y and assign it value 25.

>>> y = 25

Now in the current IDLE session, python is dealing with two variables, x and y, which have values 11 and 25 respectively. If you want to check the value of any variable in IDLE, simply type the name of that variable in a new line and press the Enter key. The value stored in it will be printed on the IDLE screen in a new line.

>>> x
11
>>> y
25

Now try to look at the below code, what do you think this will do?

>>> x = y

As you can see on the LHS(Left Hand Side) we have x and y on the RHS(Right Hand Side), hence as we explained before, the value on the right will get assigned to the variable on the left. Since y has a value 25 stored in it, this statement will modify the value inside x from 11 to 25. And hence if you ask again for the value of x, it'll be 25 now. In this case, we just over-wrote the value inside x variable.

>>> x
25

Variables in Python

Once you are done with the above example, let's be more creative this time while naming our variables. Let's create a variable and assign your name as its value. So here we are having a box named name, which is capable of storing any word. The process is quite similar to what we did above but with just a tiny change. Watch carefully,

>>> name = "Sudytonight"

As you can see, we quoted our website's name within the double quotation marks. This is because we don't want python compiler to get confused. Since Studytonight is a word (or more precisely, string in the programming world), we will have to surround it with quotation marks. By doing so we tell python that it is a word. But, what is supposed to happen if we write Studytonight without the quotation marks? Like this,

>>> name = Studytonight

Since there is no quotation marks, python will consider Studytonight as another variable and will try to find the value stored within it so that it can further assign it to the variable name. But since we never declared any variable with the name Studytonight, python won't be able to find any value for it and in the end, it will throw an error saying that the variable with name Studytonight is not defined.

Also, you can use both single quotation as well as double quotation in order to represent a word (or string).

>>> name = "Studytonight.com"
>>> name = 'Studytonight'

Live Example →

Both are fine.

Variables in Python

Now, you can also try to use variables with math functions like we learned in the last tutorial. Try to use the variables as the arguments for the function. For example,

>>> x = 3
>>> y = 2
>>> pow(x, y)
9
>>> z = -7
>>> abs(z)
7

Next, you can try to save the answer of any mathematical function in a variable. Like,

>>> p = pow(5, 2)
>>> import math
>>> q = math.log(x)

Try using mathematical operators with these variables. Like,

>>> x+y
5
>>> x-y
1

Try using these variables in a mathematical expression including some mathematical function and operators, all together, like,

>>> x = pow(x**y, p+2)

In the above code, python will first calculate the expression on the RHS(Right Hand Side) first, and will use the old value for variable x, which is 3 and once the expression is solved the answer will be stored in the variable x, which will become its new value.

Variables in Python