I'm writing a function to calculate calendar dates. While cutting down on lines, I've found that I am unable to assign multiple variables to the same range.
Jan, Mar, May, Jul, Aug, Oct, Dec = range(1,32)
Would there be an efficient way to assign these values and why does python give a ValueError?
Use
The easiest way to do what you described would be to use the
x=y=z ... = VALUE
syntax, where x, y, z, and any other variables you include will all be assigned the value ofVALUE
.In your example, all the comma-separated variables on the left side of the equals sign are assigned to sequential values of a tuple on the right side. Hence, you could do something like this:
In your code, you have 7 values on the left, and 31 on the right, so you get an out of range error because the list on the left is longer than the number or variables on the left side to be assigned the values in it. I know the code above doesn't have much relevance to achieving your goal, but I thought I'd at least give some insight as to what it was trying to do. :D