Home > Backend Development > Python Tutorial > python variable type-practical application and analysis of dictionary

python variable type-practical application and analysis of dictionary

乌拉乌拉~
Release: 2018-08-13 15:58:50
Original
1935 people have browsed it

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()    # 输出所有值
Copy after login

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']
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template