Topics

Python Variables

Welcome to another tutorial, here you will learn about variables in Python. A Variable is a nameplate that we put on empty boxes i.e. memory locations in which we can store any value or data and it can be removed once we are done using it, or going out of scope from the box or memory location which then can have a new nameplate. In Python, a single box can have multiple nameplates.

In python programming, everything is an object and variables are typically names given to identify these objects.

For example:

x = 10

In the above example, x is a variable or a label or a name for the objection 10.

This is another example:

x = 10
y = 10

From the example above, object 10 has two labels or references, and they are x and y.

Therefore, it can be said that variables provide a way of labeling data with a descriptive name, this is to make our code understandable by readers and us the developers. Thinking of variables as nameplates for containers that hold information, is quite helpful. The main purpose of variables is to label data stored in the memory and this store can be used all through the program.

 

Rules for naming variables

There are some rules observed when assigning a name to the variable.

All variable name can take alphabet(s), number(s) and underscore(s) only.

In any variable name, the first character in the variable's name cannot be a number. For instance, 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

Let’s consider a variable named x, and the variable will be made to store a numeric value or a number 11. Check the example below on how to do that, but first go to your IDLE and type: say 11. Then to do that just go to IDLE and type:

>>> x = 11

After that press, the enter key, and the variable will be created with a default value of 11 stored in it using the equal to '=' operator. Always have this at the back of your mind that the equal to operator is used to assign a specific value to any variable. Also, all variable names must be on the left side and their assigned value on the right. check out this example below:

>>> y = 25

In the example above, there are two variables, x, and y, with values of 11 and 25 respectively. If the value of a variable in an IDLE needs to be checked, just type the name of that variable in a new line and press the enter key, and the value stored in it will be printed on the IDLE screen in a new line.

>>> x
11
>>> y
25

Let’s check this example below: what do you think the code here will do?

>>> x = y

From the above example, we can observe that on the LHS (Left Hand Side) we have the variables x and on the RHS (Right Hand Side) we have y. Therefore, the value on the right will get assigned to the variable on the left. So, in the previous example, y has a value of 25 stored in it, as such this new statement will modify the value inside x from 11 to 25. If the value of x is requested again it will return 25 instead of 11. Hence, in this example, we over-wrote the value inside x variable.

>>> x
25

 

Now, let's try to be more creating when naming our variables. In the example below, you will create a variable and assign it your name as the value. Therefore, we are having a box named 'name' which is capable of storing any word. However, the process of doing this is similar to what we did in the above examples. Check out the example below:

>>> name = "PythonTutorial"

From the example above, we quoted our website's name within the double quotation marks, because we don't want the python compiler to get confused. Since (INSERT NAME), is a word meaning that is a string (programming language), we have to use quotation marks around it. As such, the python compiler will understand that is a word. 

Now, what will happen if we write this (INSERT NAME) without quotation marks. Check out below:

>>> name = PythonTutorial

Now, because of the absence of quotation marks, python will consider (INSERT NAME) as another variable and tries to find the value stored within it so that it can further assign it to the variable name. but, we did not declare any variable with the name (INSERT NAME), So, Python will not find any value for it and then it will throw an error saying that the variable name (INSERT NAME), is not defined.

In addition, a single quotation and double quotation can be used to represent a word or string.

>>> name = "Developerstutorail.com"
>>> name = 'PythonTutorial'

Both are fine: 

 

Let's use a variable with mathematical functions, that are the ones you learned in the previous tutorial. So, we will also use the variable as an argument for the function.

>>> 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

Let’s also use these variables in a mathematical expression but include some mathematical functions and operators, all together as shown below.

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

In the example above, the python compiler will first calculate the expression on the RHS (Right Hand Side), and then 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, and will become its new value.