I have three variables, I would like to know how to multiply all of these variables by another variable number simultaneously.
For example
number = 2
var1 = 0
var2 = 1
var3 = 2
The output should be:
0
2
4
Use a list comprehension
>>> number = 2
>>>
>>> var1 = 0
>>> var2 = 1
>>> var3 = 2
>>>
>>> [i*number for i in (var1,var2,var3)]
[0, 2, 4]
And to print it
>>> for i in output:
... print(i)
...
0
2
4
You can use map and lambda also
>>> for i in map(lambda x:x*number,(var1,var2,var3)):
... print(i)
...
0
2
4
You could just use a simple for loop as follows:
number = 2
var1 = 0
var2 = 1
var3 = 2
for output in (var1, var2, var3):
print output * number
This would display:
0
2
4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With