Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most efficient way to assign a value of zero to multiple variables at once

Tags:

python

I'm trying to initiate the variables at zero, so it currently looks like this

x1,y1,x2,y2=(0,0,0,0)

It works, but just seems a little redundant. Is there a cleaner way?

like image 916
Chris Avatar asked Jul 24 '12 15:07

Chris


People also ask

How do you assign the same value to multiple variables?

You can assign the same value to multiple variables by using = consecutively. This is useful, for example, when initializing multiple variables to the same value. It is also possible to assign another value into one after assigning the same value.

How will you assign values to multiple variables in a single line?

When assigning multiple variables in a single line, different variable names are provided to the left of the assignment operator separated by a comma. The same goes for their respective values except they should be to the right of the assignment operator.

Can we assign multiple values to multiple variables at a time?

We can assign values to multiple variables at once in a single statement in Swift. We need to wrap the variables inside a bracket and assign the values using the equal sign = . The values are also wrapped inside a bracket.

How do you assign a loop to multiple variables?

How do you put multiple variables in a for loop? And you, too, can now declare multiple variables, in a for-loop, as follows: Just separate the multiple variables in the initialization statement with commas.


2 Answers

That is effectively unpacking a tuple. You can do:

x1 = y1 = x2 = y2 = 0

Just don't do this with mutable objects!

like image 76
Jon Clements Avatar answered Nov 14 '22 03:11

Jon Clements


I'd usually do

x1 = y1 = x2 = y2 = 0

However, this hardly matters. Both versions are easy to grasp at a single glance.

like image 41
Sven Marnach Avatar answered Nov 14 '22 02:11

Sven Marnach