Ryan Park
Ryan Park

Reputation: 65

Multiplying multiple variables by a scalar

I have four variables: x, y, height, and width (all integers or doubles). I want to multiple all four by some number, a (also an integer or double). It's possible to write

x *= a
y *= a
height *= a
width *= a

Is there a more concise, one-line version of this?

Upvotes: 0

Views: 1327

Answers (3)

Sayandip Dutta
Sayandip Dutta

Reputation: 15872

You can invoke __mul__ magic with map (this is an ugly, added it for sake of variation):

>>> x,y,height,width = map(a.__mul__,(x,y,height,width))

Upvotes: 0

shaik moeed
shaik moeed

Reputation: 5785

Try this one, no need to write a four times

>>> for i in [x, y, height, width]:
        print(a*i)

Upvotes: 0

Mert Köklü
Mert Köklü

Reputation: 2231

You can do with unpacking:

x, y, height, width = x*a, y*a, height*a, width*a

Upvotes: 4

Related Questions