I want to do multiplication using for loop. Here is the code.
a = 4
b = 6
for i in [a,b]:
i*=2
The values of a
and b
remain the same. How to make it work?
I want to do multiplication using for loop. Here is the code.
a = 4
b = 6
for i in [a,b]:
i*=2
The values of a
and b
remain the same. How to make it work?
int
are immutable, so you'll need to rebind a
and b
to fresh int objects
>>> a = 4
>>> b = 6
>>> a, b = (i*2 for i in [a,b])
>>> a
8
>>> b
12
Use dictionary:
z = {'i': a, 'j': b}
for k in z.keys():
z[k] *= 2
a = z['i']
b = z['j']