Want to create a variable number of variables in Python? You can use dictionaries to accomplish this. Dictionaries are stores of keys and values.
1 2 3 4 5 | >>> dct = {'x': 1, 'y': 2, 'z': 3} >>> dct {'y': 2, 'x': 1, 'z': 3} >>> dct["y"] 2 |
You can use variable key names to achieve the effect of variable variables without the security risk.
1 2 3 4 | >>> x = "spam" >>> z = {x: "eggs"} >>> z["spam"] 'eggs' |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.