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 are nameplates that we put on empty boxes(memory locations) in which we can store any value(data) and once we are done using a variable the nameplate is removed(going out of scope) from the box(memory location) which then can have a new nameplate. Also, in python one box can have multiple nameplates.
In python everything is an object and variables are just names given to identify these objects.
x = 10
In the code above,
x is a variable or a label or a name for the object
If we have the following code:
x = 10
y = 10
Then for the object
10 we have two labels(references),
Hence, we can say that variables 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 nameplates for containers that hold information. Their sole purpose is to label the data stored 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
#variable, are all invalid variable names. While
_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 named
x, we want this variable to store a numeric value or 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
>>> y = 25
Now in the current IDLE session, python is dealing with two variables,
y, which have values
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.
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
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
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
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
>>> name = "Studytonight.com"
>>> name = 'Studytonight'
Live Example →
Both are fine.
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)
>>> 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,
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.