Variables in Python
by Num3Ilia 07 Feb 2024

Variables in Python

Overview

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
Variables in Python

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:

  • variable names can contain only letters, numbers and underscores. They can start with a letter or an underscore but NOT with a number.
  • spaces are not allowed in the names of the variable, only underscores can be used to separate words in variable names ( don’t abuse this to make a variable name in length of a book wrote by George RR Martin)
  • try to be creative and avoid using python keywords and function names as variable names. Python has reserved keywords for a particular internal mechanism here and a number of functions and types built into the language that are always available to brew your "potions" check it here.
  • variable names should be short but descriptive ( you can avoid, but readability will be affected, the joke from above wanted to highlight to avoid making LONG words as variables, don’t be a JAVA guy!).
  • class names should use the CapitalWordsConvention
  • function names should be lowercase, with words separated by underscores as necessary to improve readability.
  • constants are usually defined on a module level (placed at the top fo your python file) and written in all capital letters with underscores separating words.
  • there are more but I think this are the most important, please read  PEP8
  • no seriously, please read PEP8!

Visual guide to differentiate the above guidance when you have to define a variable:

Variables in Python

SPECIAL variables usage, multi assignment and Constants:

Variables in Python
Note:  Try to avoid defining your variables with letter l from LIMA or I from INDIA are confusing OR a mixture with uppercase letter O from ORAGE, as this can be interpreted wrong by other readers with 1 and 0 link.

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).

Note:  Writing good comments will save you tremendous time and will aid also in collaboration with other programmers, so make a habit and write meaningful comments always!

References

  1. PEP 8 – Style Guide for Python Code
  2. Link - Python Documentation Memory Management
  3. Real Python - Memory Management in Python
  4. musingsofagator - memory layout of a C program
  5. stackoverflow - environment variable memory address
  6. opensourceforu.com - memory management in list and tuples
  7. datadriveninvestor (medium blog) - python memory management
  8. tenthousandmeters.com - how variables are implemented in CPython

Happy PiPing folks!