globals() and creating 100 global variables at once

21st May 2023 at 3:26pm

Ran into this scenario where I had to make 100 global variables programmatically. For example, x0 = 0, x1 =1, x2= 2 and so on till x100 = 100.

One way to do this in python using globals()

Example :

>>> globals()['x0'] = 0
>>> x0
0

And then loop through a list to make the rest of the variables.