How do I define it while changing the variable name in the for statement?
for i in range(5):
exec('var%d = %d' % (i, i**2))
>>>var0, var1, var2, var3, var4
>>>(0, 1, 4, 9, 16)
There are other ways to put it in an array, but if you want to create multiple variables at once, it is smart to use exec.
Recommended Posts