Python Tutorial: Variables
Photo by Raghav Bhasin on Unsplash
In the previous posts we learnt how to create values by typing into the python REPL, however the computation we made were somehow “volatile”.
Starting from this post I would suggest you to use an editor to type python code: we will execute our python files and there will be some exercise available
What are variables?
most of us learn about variables while studying basic equations; placeholders for a number.
In a python program a variable is a name we give to a value; a way to move values around in the program itself.
This is different from languages like C where a variable declaration is an instruction for the compiler to save some space in the program memory (the C programmers would say in the program “stack”)
Python is more similar to the Lisp programming language, where a value can be “bounded” to a name.
Assignment and update
an assignment is made with a variable name followed by the equal sign and an expression
Expressions can contain literals and variables too
Variable names must start with a letter and can contain letters, figures and underscore. This kind of names are called “identifiers”.
By the way – letters are defined in the “UNICODE” specification so you can use also greek letters (which is a beloved feature by academics) or letters from your favorite language (if your editor help you showing them)
once a variable value is set, a new assignment changes the binding.
For those values who support sum, multiplication or subtraction the following operators can be used to update the value from the previous one
Identity and equivalence
We call two values equivalent if their content has the same meaning even if they are stored in different python objects
The double equal is the operator to check value equivalence, it returns a boolean
the is
operator is used to check if an object is exactly the same
String literals are “optimized” by the compiler at compile time so that only a copy is in memory
Mutability of a value
Sometime the result of an operation is a new value, i.e. a different python object.
Other times the same object is altered to store the new “value content”
Try this example in a python REPL (command line): Lists are “mutable”, i.e. the object is “re-used” to contain new values
Tuples are “immutable”, i.e. a new value is created after each operation
Deleting a variable
it is possible to erase a variable when it is not useful anymore
With the same syntax we can delete also dictionary keys…
does this coincidence sound strange to you?
Yes, but what are variables actually?
what actually happens in Python is called “binding”.
This means we are “connecting” a name with a value.
What python actually does is to use some special “dictionaries” we call “environments” to actually create the connection between names (“keys”) and “values”
Garbage collection is a noble job
when a value is not bounded to any variable it is cleaned away from the process memory: this cleaning is called “garbage collection”.
The garbage collection also takes care to properly free resources which may be attached to the collected value (e.g. open files etc)