0

someone know if is possible to create a function in python that create variables with different names, something like this idea:

def makeVariable(variableName, n): 
    variableName = pow(n, 5)

so that later on I can use the variables with the name I specified in the def function. Ex:

makeVariable(one, 1)
makeVariable(two, 2)

print(one)
print(two)
  • Its possible, but a bad idea. Populate a dictionary instead, and have the names as keys. – Carcigenicate Feb 09 '21 at 02:36
  • The globals() function returns the dictionary that holds all of python's current global variables. Hence, you can do "globals()['name'] = 5" and now the variable "name" exists as the value of 5. – Bobby Ocean Feb 09 '21 at 02:53

0 Answers0