You can think about variables akin to containers or as buckets which can store/carry different type of data inside (in Python this is more like a label which points to a piece of data in memory, but this for later). Every variable is linked (connected to a value from right to left) which will be the information associated with that variable allowing the variable to represent or store the data within a program.
Identifiers names are following certain rules which are going to be explain (light) below (for the extended guidance you have to read the documentation).
e.g:
variable = "My first assigned value to the variable!"
print(variable)
>>> My first assigned value to the variable
As you can see from the picture, variable received a piece of text which in Python language, this is called a string and here you can find a classification of basic data types which are built into the Python language, and which can be used to work your magic:
An interesting aspect which you are going to find later would be that all the data types have methods build into them which allows you to perform an action on that particular piece of data. The methods are called by dropping a dot and calling that particular method from the data type.
e.g:
variable = "My first assigned value to the variable"
print(variable.upper())
>>> MY FIRST ASSIGNED VALUE TO THE VARIABLE Returning to variables, a very important rule of thumb if I can say is, when you are using variables, you need to adhere to a few rules and guidelines. Not following this rules will cause headache down the road and even your code will be prone to errors.
Based on my previous article lexical analysis, I have introduced you lightly to one of the most important guide on this matter and that its called PEP8 and if you don’t know, now you will know, this page contains all the guidance you have to follow to work your magic in Python.
Take time and read it, from time to time again check it until you follow as much as you can the information which is shared in it.
To highlight a few of the guidelines which I think are quite important ( the most directive ones you cant or should not avoid) are:
Visual guide to differentiate the above guidance when you have to define a variable:
SPECIAL variables usage, multi assignment and Constants:
What I don’t follow ad litteram the guidance, is the line length which in my setup is defined at 160 character per line, I have way to big screens to follow this guidance, currently nobody complained about what I do, but I'm a newbie, so you never know (here judge yourself what is it best)!
PEP 8 – Style Guide for Python Code its magic, each time I take a peak on it I realize I'm still not following some guidance from the documentation. I will not spend time to explain much of this details because, the PEP8 has so many example and so many goodies that would be a waste of time to recreate something which is terrible good. I just decided to cherry pic what later on will matter (with different components) and get only an excerpt from the document together with a need to highlight that you should spend some time in reading it from top to bottom.
One addition before I close this article and for which I wanted to write a separate phrase is commenting. Reading the guide you'll notice that there is quite a lot text invested in describing the comments. The main reason to drop comments in your code is to explain to you, or to another, what your code is suppose to achieve and how you envisioned to work.
Why I said to you, because we tend as humans to forget a lot of information and now when you work on your program, you are going to know all the aspects which needs to be known about it … later, in a few months or years, if you want to enhance your code or change portion of it, if you don’t have comments, … well its going to be cumbersome for you (for any) to do something (especially for someone who reads your code 1st time).
Happy PiPing folks!