PYTHON - VARIABLES
Variables in Python are storage location where the data(Be it a number or a Name) is stored.It is like a container where something is stored.
Example :
Let us go back to our school days. You are in the Maths class and your Maths teacher had asked you to add two numbers. i.e. She asked you to add the numbers 5 and 6 and tell her the result.
And you have written the numbers 5 and 6 in your notebook, added them and and told the result 11 to your teacher.
Well ! Pyton takes the same step to add two numbers. The only difference is, you have used one notebook to write and add the numbers.
But Python uses different locations to store each number. And they are called as 'Variables'.
Let us see how 'Variables' are used in Python for addition of two numbers.
Python Code to add two numbers
x = 5
y = 6
z = x + y
print("The added value is :",z)
Output :
The added value is : 11
Now, if you look at the above code, 'x', 'y' and 'z' are 'Variables'.
And below are the steps involved in addition of the numbers.
-
'x' is holding the first number '5'.
x = 5
The above line says, create an empty location named 'x'. And assign it with the value '5'. And 'x' is called as 'variable'.
Just note that the '=' in the above code is called 'assignment operator'. It actually means the value '5' at the right should be placed in the storage location/variable 'x'.
-
'y' is holding the second number '6'.
y = 6
Again, the above line says, create an empty location/variable named 'y'. And assign it with the value '6'.
-
And 'z' is holding the added value of (5 + 6).
z = x + y
Similarly, the above line says, create an empty location/variable named 'z'. And store the added value to it.
-
Finally, we are printing the added value of (5 + 6) on the screen. Which is stored in 'z'.
print("The added value is :",z)
The above print(...) statement has two sections.
First section has the text inside double quotes ' " '.
"The added value is :"
And the second section does not have double quotes. And is usually a 'variable'.
z
And they are separated by a comma ','.
"The added value is :",z
The comma ',' in the above statement is a concatenate operator. i.e. It joins the first and the second section to form a meaningful sentence.
And we get the below output :
The added value is : 11
Note : The programmers who already know other languages like Java, C, C++, C# might be confused by not seeing the main(...) method or semicolon ';' or even the Curly Braces '{}'. Well! That's the beauty of Python. It doesn't use unwanted lines of code to give a wonderful and easy coding experience.
So far we have seen, how to store a number (i.e. x = 5) in a 'Variable'.
Now, let us see how to store a String(i.e. It could be a Name or a City) in a Variable.
Storing a String in a Variable
Let us say, we want to store the String 'Hello World' in a Variable(Say 'x').
And the process of storing a String is same as storing a number, but with a mild difference.
Below is the Python Code :
Example :
x = "Hello World"
print(x)
Output :
Hello World
So, in the above code, we have initialised the String 'Hello World' to the variable 'x'.
x = "Hello World"
The only thing to note here is, if you are initialising a String (i.e. Hello World), it should be either in Double Quotes ("") or Single Quotes (''). i.e. "Hello World" or 'Hello World'.
x = "Hello World"
x = 'Hello World'
Dealing with multiple variable and data
Python also gives us the flexibility to assign one value to multiple variables.
Below is the Python Code :
Example :
x = y = z = "Hello"
print(x)
print(y)
print(z)
Output :
Hello
Hello
Hello
So, in the above code, we have three variables 'x', 'y' and 'z'. And we have assigned the string 'Hello' to all the three variables in a single line.
x = y = z = "Hello"
And what happens is, three variables, 'x', 'y' and 'z' are created and assigned with the value 'Hello'.
Next, let us say we have three variables and also three different values.
Interestingly, Python provides us the flexibility to assign three values to all three variables in a single line.
Below is the Python Code :
Example :
x, y, z = "Hello", "Beautiful", "World"
print(x)
print(y)
print(z)
Output :
Hello
Beautiful
World
So, in the above code, we have three variables 'x', 'y' and 'z'. And we have assigned the string 'Hello', 'Beautiful' and 'World' to all the three variables in a single line.
x, y, z = "Hello", "Beautiful", "World"
Python Variable Naming Convention
Just like any other language, there is a rule to declare the 'Variables'.
Although, the 'Variables' can be of any names with some restriction.
-
A lower case Variable is different from an upper case Variable.
i.e. The lower case variable 'a' and upper case variable 'A' are different.
-
A Variable should never start with a number.
i.e. The Variable with name '2y' is never allowed.
-
A Variable can contain alphabets (i.e. A to Z or a to z), numbers (i.e. 0 to 9) or an underscore (i.e. _).
So, if you have two numbers and need to assign them to two 'Variables'. You can either declare the variables as 'x' and 'y'.
x = 5
y = 7
But the preferred way to name the 'Variables' would be 'first_number' and 'second_number'.
first_number = 5
second_number = 7
But you can never use 'Variable' names with spaces like 'first number' or variables with a - like 'first-number'.