How does Python create variable variables
Most people don't understand the knowledge points of this article "how Python creates variable variables", so Xiaobian summarizes the following contents for everyone. The contents are detailed, the steps are clear, and they have certain reference value. I hope everyone can gain something after reading this article. Let's take a look at this article "how Python creates variable variables".
Dictionaries can be used to accomplish this task. A dictionary is a store of keys and values.
>>> 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 effects of variable variables without security risks.
>>> x = "spam">>> z = {x: "eggs"}>>> z["spam"]'eggs'
I'm not sure if you want to do something like that
var1 = 'foo'var2 = 'bar'var3 = 'baz'...
Lists may be more appropriate than dictation. A list represents an ordered sequence of objects with integer exponents:
lst = ['foo', 'bar', 'baz']print(lst[1]) # prints bar, because indices start at 0lst.append('potatoes') # lst is now ['foo', 'bar', 'baz', 'potatoes']
For ordered sequences, lists are more convenient than dictionaries with integer keys because lists support iteration, slicing, appending, and other operations in index order that may require clumsy key management for dictionaries.
The above is the content of this article about "Python how to create variable variables". I believe everyone has a certain understanding. I hope the content shared by Xiaobian will be helpful to everyone. If you want to know more relevant knowledge content, please pay attention to the industry information channel.