Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I multiply more the one variable by a number simultaneously

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
like image 239
james hughes Avatar asked Jan 26 '26 10:01

james hughes


2 Answers

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
like image 157
Bhargav Rao Avatar answered Jan 28 '26 00:01

Bhargav Rao


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
like image 39
Martin Evans Avatar answered Jan 28 '26 00:01

Martin Evans