简体   繁体   中英

Assigning different values to multiple variables

I have around 15 variables to be assigned different values.

var1 = val1
var2 = val2
#.....and, so on 

Should I declare them one by one, or by using a for loop?

Is there an inefficiency induced through a for loop, considering that looping increases time complexity only if variables are too many. I have only 15.

Reason for asking - I need to do this on several places for different set of variables, on the same code file. Just want to know a preferable way.

you can just do this:

[var1, var2, var3, ...] = [val1, val2, val3, ...];

you can also do this without the lists but this could be used for things like:

[var1, var2, var3] = [x**2 for x in range(1,5)]

to assign for example to this new variables the squares of 2, 3, 4.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM