Why do people sometimes write code like this?

``````var = float(str(alist[::-1][0]).split()[1:4])/3+float(alist[4:])
``````

The answer: to save computational time. When the code could be instead expanded with only three lines more…

``````var = alist[::-1][0]
var = str(var).split()[1:4]
var = float(var)/3
var += float(alist[4:]
``````

…the computational cost budgeteers shake their head and opt for the former.

Repeated variable assignment takes up computational space, they say, so much that with the number of iterations in their code it makes a significant difference.

In this article, I’ll explore what the true computational cost is of writing code cleanly with multiple variable assignment with multiple tests.

## First — The Benefits of Multiple Variable Assignment

Especially in a language like Python, where there are at least ten ways to write anything, developers will often cram several operations into one line.

Multiple variable assignment allows the reader to take in the functions applied in smaller batches. Additionally, it makes it easier to pick through the layers of parenthesis present when more than three Python functions are applied:

``````list(str(int(x)+1)+'1') #the scope is incredible difficult to read.
``````

More so, it is difficult to track the state of the variable when it is all crammed into one line. It’s like teaching maths — the teacher starts with arithmetic first, then calculus later, instead of teaching both simultaneously.

Multiple variable assignment means the reader can track what happens to the variable and what status the variable is in, much more easily in four lines than one line.

In many cases, multi-variable assignment _saves _computational time. Take, for example:

``````a = (b+5)*5 + (b+5)/4
``````

which could alternatively be written as

``````c = b+5
a = c*5 + c/4
``````

By assigning `(b+5)`to one variable, it is calculated only once instead of multiple times.

#testing #data-analysis #programming #python #pandas

1.40 GEEK