Python Variables
Let us explain it.
You can read or watch the explanation
If you follow this blog by this time you know how to do arithmetic operations with the given numbers. You also know the process of encoding literals. But, Where do you store the results of all those numbers to use them in other operations?
How do you save the intermediate results to get subsequent ones?
For example, you are adding 2 numbers and you will get some result and now you want the result of the previous calculation to add with the new number. From where will you get that number? In general you know the result and you will do it manually. But how will you say to the computer that this is the result and you have to add this with a new number?
Python will help you with that. It provides you with some special memories or containers or boxes and these boxes are called variables. The name itself says that it varies from time to time. As you can in this example where you are adding 2 numbers and you are storing the result in some variable. Here you can use the same variable to update the value by adding a new number.
Hence the variable itself determines that it varies from time to time. What does every Python variable have? Every variable will basically have 2 things.
- Name - where name means you are giving some name to the container which you are using for storing the results.
What if you didn’t give any name and try to access it. There is no way to do that. It is important to give some names to the container. And the second thing is value?
2. Value - Value means the result that you store in the container or variable.
But there are some conditions to specify the name of the variables. As a developer, You must decide how many variables you should use and which variables you use. If you want to give a name to a variable, Some strict rules must be followed.
The name of the variable must:
- Composed with uppercase or lowercase letters. You can also use characters like underscores and digits. Lowercase variables and Uppercase variables are treated differently in python. There is a lot of difference between the variables “add” and “ADD” though they have the same meaning they are accepted as different variables in python.
- The name of the variable must begin with a letter.
- Also you are not allowed to use the names of keywords or reserved words where keywords or reserved words are the words which already exist in python. We will get to know about it in a bit.
Rules to name a variable :
Same rules are applied if you want to create your own functions. Here are some examples which follows the rules:
MyValue - you can give a combination of upper and lowercase letters
a - and you can just give lowercase letters for the name.
t20 - you can even use a combination of alphabets and numbers.
ThereIsNoLengthForTheNameOfTheVariable
AddedResult-Incase you are using more than one word
separate it by using uppercase letters.
so that you will be aware of the name
easily.
You can also use latin letters and underscores to create your own variables.
Adiós_Señora
переменная
Now let us look at some examples of incorrect variable names which are against the rules of programming.
20t - it must not begin with a number.
Added Result - It must not contain any space
Here you can see some of the keywords listed in python. These are also called reserved words. These words are predefined. They already have some meaning and you cannot change them on your own. You have to treat them as they are built. Fortunately, Python is a case sensitive language, you can modify these keywords so that it will not be a keyword, instead it becomes your own created word.
For example, Let us take a keyword called “break”. You cannot use the same word for your variable name or function name but you can use the word “Break” for your function name or variable name. Why is python accepting it because python is a case sensitive language which means it will take “break” as a keyword only if b is in lowercase but you are giving uppercase B in your name so python will take it as a normal variable name or function name.
Let us see, what we will keep inside a variable and what it actually contains. Well you can store anything inside a variable. You can store any kind of stuff inside a variable and the value of the variable depends on the content that you put inside the variable.
Let’s talk about 2 important things related to a variable which are:
- How do we create a variable?
- How do we store anything inside a variable?
These 2 things are interrelated and both are dependent on each other. It means. A variable is created only when you assign some value to it. By assigning the value the variable will be created automatically.
For example, let us take a variable called “a” and give some value to it using equal. so, a = 2 variable is created named “a” and the value of that variable is 2. Creation is extremely simple. If you want to create some variable you will just create some significant name and assign some value to it. You don’t need to do anything else in particular. Simple,
Variable name, equal sign, some value.
Let’s write a simple code where I’m creating a variable called var and I’m assigning some value in the first line and in the second line I’m trying to print that variable.
Code :
Output :
2
By this we can understand that print functions can handle variables as well. Let’s try to create 2 more variables called
Output :
1 Girl Coder 92.5
So you can create as many variables as you want based on your code. One more important thing, you are not allowed to use the variable which does not exist.
For example you’ve created a variable called name where n is lowercase and you are trying to print the variable Name where n is uppercase.Python generates an error called name is not defined and it’s true because there is a lot of difference between this name and this Name and python treats them completely different.
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
print(Name)
NameError: name 'Name' is not defined
How do we update the value of a variable in python ?
You just need to use equal sign where equal sign is an assignment operator where it assigns the given value to the specified variable.So, it assigns its right argument to its left argument.
Let us update the value of the variable percentage from 92.5 to 96.5 and print as you’ve done before. You can see the updated value of the percentage in the output. Let us do little maths using a python program.
Find the square root of the number :
First create a variable called number and assign some value to it like some integer. In maths, to find the square root we will keep the given number inside the root. What would be the next step? You will convert the square root to an exponent where the square root becomes ½ . You’ve done all these in maths.
But in python how do we get the square root? Here we use the exponentiation operator.
Use the exponentiation operator here i.e., number ** 0.5 where we get the square root of the given number. Now store it in some other variable called result. Whenever you declare the name of the variable try to give a significant one so that everyone can easily understand the code. Now, after declaring it, print the variable result and you will get the output of the square root of the given integer or number.
If you wanna reduce the lines of the code. you can directly print the calculation of square root.
Importance Of Comments :
Imagine After you’ve written your code and Now you have to send your code to your friend and your friend is not getting what you’re doing. How can he understand your code? Not only your friend, how can any human understand any code.
That is why we put comments in programs. Comments help humans to understand what’s going on in code. Whenever python encounters comments it just takes it as a blank space no matter how long it is.
It is just a piece of text which starts with # sign. This hash extends till the end of the line.
If you want to write a comment which is more than one line then you can use a set of 3 quotes at the beginning and at the end. Good and Responsible developers always describe each important piece of code. They always explain the role of the variables in the code. The best way to comment is to declare clearly and significantly.
For example, if a particular variable is designed to find the square root of the given number. The name would be like square_root which itself explains why it is created. This is called self commenting.
Comments are also used in another way as well. If you don’t wanna include a piece of code while running then you can eliminate that piece using comments in case you have errors and you have to find the location of it. You will understand this if you write a larger code.
Shortcut of operators in python :
You can also write operators in a shortened way. Look at the code where we’ve already created and updated var i.e., var = var + 1, You can also alter this code to var += 1 which also has the same meaning. Not only + you can use almost all operators
Take a look at these examples.
a = a + 3 * b a += 3 * b
num = num / 2 num /= 2
result = result % 10 result %= 10
So far you learned :
- Variables
- Keywords
- Comments
- Shortcuts of operators

Post a Comment
0Comments