Before we delve into this topic, we need to understand the technical terms surrounding variables.
Variables are created using an assignment operator =, as indicated in the previous blog post under “Common Operators Used in Python“.
The word before the equal sign is the variable name.
Anything after the equal sign is the value.
As seen in this image, left of the equal sign is the variable name, and right of the equal sign is the value.
We can store all Data Types within a variable.
The process of retrieving the values from within a variable is called “call”
In Python or any programming language, variables are meant to store information to be retrieved at a later time. In order to create a better visual of what a variable really is, and to give you a clear understanding of the subject, I’d like to describe variables in many ways using metaphors; as a “Treasure box”, “Jewelry Box”, “Storage Unit”, “Container”, “Kitchen Cabinet”, “Toy Box”, “Safe”, “Wardrobe/Closet” etc; In general, these are all forms of safe/secure places where we store our possessions (things that are of value to us, or things that are of value to others that we babysit on their behalf, so they can come back and claim them later). Within these places, our possessions can be retrieved when needed, in order to use, sell, gift, discard, or do anything we want with them.
Same goes with variables, they are treasure boxes, and the values that we store in them, represents our priceless possessions. You can call on them later, and manipulate the data within them.
Some Dos and Don’ts
Do not use reserved keywords, as they are meant for specific functions within Python.
Keywords
As you can see, attempting to use a reserved keyword in Python would result in an error message.
Do not start Variable names with numbers.
As you can see, starting with numbers, resulted in an error message, because it is the wrong syntax.
We can do it this way instead;
Notice that there was no error message this time, that’s because a variable name starting with a word is acceptable.
Note that Python is case sensitive, so variables are to be called upon the way they were created.
Now if we want to call on the variable we created, we cannot call it as Numbers, because it would result in an error message, letting us know that there is no such variable, as shown in the image below.
If you also noticed, we didn’t have to print our variable as a string, print(“numbers”), because if we did, it would just be a print statement. For instance;
We want to retrieve the value that our variable is holding onto, so we say print(numbers) that way it gives us an output of the value that has been stored in it. If we went by the former it would only output a print statement, numbers instead of the value 123.
for instance;
Do not start a variable with a dash or place a dash in the middle of a variable, because it is an operator within Python called subtraction, and cannot be used while assigning a variable. For instance;
As you’ve noticed trying to assign a variable this way resulted in an error message.
Instead, we could start our variable with an underscore, or place it in the middle of two words to create a distinction. (shift key + – gives you an underscore _ ). For instance;
Now, when we try to call on either one of these two variables, they would give us an output of road and person.
Next we would be looking at, manipulating variables and much more.