Home > Article > Backend Development > python variable type-practical application and analysis of dictionary
In this article, we will learn about the dictionary among Python variable types. This is a variable type that we often use in actual combat. I hope this article can provide some help to you who have just come into contact with python.
python dictionary:
Dictionary (dictionary) is the most flexible built-in data structure type in Python besides lists. A list is an ordered collection of objects, and a dictionary is an unordered collection of objects.
The difference between the two is that the elements in the dictionary are accessed through keys, not offsets.
Dictionaries are identified with "{ }". A dictionary consists of an index (key) and its corresponding value.
We can use an example to understand:
#!/usr/bin/python # -*- coding: UTF-8 -*- dict = {} dict['one'] = "This is one" dict[2] = "This is two" tinydict = {'name': 'john','code':6734, 'dept': 'sales'} print dict['one'] # 输出键为'one' 的值 print dict[2] # 输出键为 2 的值 print tinydict # 输出完整的字典 print tinydict.keys() # 输出所有键 print tinydict.values() # 输出所有值
The output result of the above example is:
This is one This is two {'dept': 'sales', 'code': 6734, 'name': 'john'} ['dept', 'code', 'name'] ['sales', 6734, 'john']
Through the above examples to understand the dictionary in python, I hope this article can provide some help to you who have just come into contact with python.
The above is the detailed content of python variable type-practical application and analysis of dictionary. For more information, please follow other related articles on the PHP Chinese website!