What are
variables?
Variables
are like a container that will contain the valuable piece of information in it.
In programming, variables are the name associated with memory locations that
keep a particular type of data. The data inside the memory location can also
be changed. Let’s understand this concept with an example; You have juice in a jug
but jug can also contain water, soda or any other kind of liquid. So, here the variable
is a jug, the data is juice and its type is liquid.
You can
change the value of a variable in your program at any time, and Python will
always keep track of its current value.
Rules
and guidelines to use variables:
Whenever we
use a variable, we need to consider some rules and guidelines to avoid errors
and to make things easier for us. As, in our last example, if the liquid is hot,
we will not put it in a glass jug that can’t bear it or else it will get
cracked Moreover if we want to pour something hot in that jug, we will
always put a spoon inside it. Likewise, we do for our python variables. Let’s
discuss those rules and guidelines called naming conventions:
- Variable name can only have letters (both upper or lower case), numbers and underscore. Be mindful of their arrangement as a variable can start with a letter or underscore and not with a number. For example, jug_1 and _jug1 are valid variable names but 1_jug is an invalid name.
- Using spaces in a variable name are not allowed and will generate an error. Instead, you can use underscore to separate multiple words. For example, glass jug is invalid variable name but glass_jug is a valid one.
- Avoid to use Python reserved words, keywords and function name as your variable name. For example, print, True, False etc
- Variable names should be precise yet descriptive that clearly show the purpose and data that could possibly be in it. For example, jug is better than j, glass_jug is better than g_j and jug_capacity is better than capacity_of_glass_jug.
- Be careful while using a letter that can easily be confused. For example, l and capital O are mostly confused with number 1 and 0.
It will definitely
need some practice before you become an expert in it.
Note:
Try
using lowercase letters for now although python interpreter will not cause any
error if you use uppercase letter too it’s a good practice to remember the
variable name.
Always
check the spellings of the variable name while using the variable for some
operation otherwise, it will cause an error. Don’t worry You can always solve errors by
using trackback. Consider the code below:
-------------------------------------------------------------------------------------------------------------------------
>>
message = “Hello programers!”
>>
print(mesage)
------------------------------------------------------------------------------------------------------------------------
NameError: name 'mesage' is not defined
Python interpreter
helps us to find the error by giving the line number and type of error in this case
error is on line two and the type of error is name error.
Variable Assignment:
In python,
the syntax to assign a value to a variable is easy than any other programming
language.
>> name
= “John Doe”
This will
be read as “name is assigned a value, John Doe”. The assignment is done using equal
(=) sign on the left of it is the variable name and on the right side is the value.
Exercise:
The best
way to understand new programming concepts is to try using them in your programs.
Here I have an exercise for you so, you can practice what you have learnt
so far. Write a program to print a message using a message variable. Try changing
the message and then print it again. If you get stuck while working on an exercise
feel free to share your issues.
If you
found this article helpful share it with others. Let me know your queries and
suggestions in the comment section below. Do subscribe my site and channel as
I’m going to upload a series of articles and videos on programming in python
and I don’t want you to miss any update.
Moreover,
you can also contact me and share your queries on Instagram, Facebook or
Twitter. I would love to provide a solution to your issues.
Stay tuned
and strive to learn. Keep learning, keep growing!
0 Comments