Python Tutorial: Variables

raghav-bhasin-c3sMNpS2-Dg-unsplash.jpg 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

message = "Houston we have a problem" the_answer = 6 * 7 response = "Apollo, we have a solution %d" % (the_answer,) response

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)

λ0 = 0.5 δ = λ0 / 10 δ

greek_letters_meme.jpg

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

the_answer -= 13 response += "!" response

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_answer = 13 (the_answer == 13, # True the_answer != 42) # True

the is operator is used to check if an object is exactly the same

message1 = ["I don't know who I am", 0] message2 = message1 message3 = ["I don't know who I am", 0] (message1 is message2, # True message1 is message3) # False

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

x = [1,2] y = x x += [3] (x == [1,2,3], # True y == [1,2,3], # True y is x) # True

Tuples are “immutable”, i.e. a new value is created after each operation

x = (1,2) y = x x += (3,) (x == (1,2,3), # True y == (1,2,3), # False y is x) # False

Deleting a variable

it is possible to erase a variable when it is not useful anymore

the_answer = 42 del the_answer the_answer #this will throw an exception

With the same syntax we can delete also dictionary keys…

user_info = {"username": "Marco", "password": "HAHAHAHA"} del user_info["password"] user_info

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

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

marco.p.v.vezzoli

Self taught assembler programming at 11 on my C64 (1983). Never stopped since then -- always looking up for curious things in the software development, data science and AI. Linux and FOSS user since 1994. MSc in physics in 1996. Working in large semiconductor companies since 1997 (STM, Micron) developing analytics and full stack web infrastructures, microservices, ML solutions

You may also like...