Advanced python tutorials: dictionaries, dictionaries, and dict

Source: Internet
Author: User
This article mainly introduces the dictionary, dictionary, and dict of the python advanced tutorial. the most official words are the dictionary and the english dict. This article focuses on the dictionary, you can refer to the basic tutorial to introduce basic concepts, especially objects and classes.

The advanced tutorial further expands the basic tutorial to illustrate the details of Python. I hope you will have a more comprehensive understanding of Python after the advanced tutorial.

As we mentioned earlier, the list is a class in Python. A specific table, such as nl = [1, 3, 8], is an object of this class. We can call some methods of this object, such as nl. append (15 ).

We will introduce a new class, dictionary ). Similar to the list, a dictionary can store multiple elements. The object that stores multiple elements is called a container ).

Basic concepts

Common ways to create a dictionary:

The code is as follows:


>>> Dic = {'Tom ': 11, 'Sam': 57, 'Lily ': 100}
>>> Print type (dic)


Similar to a table, a dictionary contains multiple elements separated by commas. However, the dictionary element contains two parts: keys and values. Commonly, keys are represented by strings. you can also use numbers or true values to represent keys (immutable objects can be used as keys ). The value can be any object. Keys and values correspond to each other.

For example, in the above example, 'Tom 'corresponds to 11, 'Sam corresponds to 57, and 'Lily' corresponds to 100

Different from a table, the dictionary elements have no order. You cannot reference an element by subscript. A dictionary is referenced by a key.

The code is as follows:


>>> Print dic ['Tom ']
>>> Dic ['Tom '] = 30
>>> Print dic

Create a new empty dictionary:

The code is as follows:


>>> Dic = {}
>>> Print dic


Add a new element to the dictionary:

The code is as follows:


>>> Dic ['lilei'] = 99
>>> Print dic


Here, we reference a new key and assign it the corresponding value.

Cyclic call of dictionary elements

The code is as follows:


Dic = {'lilei': 90, 'Lily': 100, 'Sam ': 57, 'Tom': 90}
For key in dic:
Print dic [key]

In a loop, each key of dict is extracted and assigned to the key variable.

With the print result, we can confirm again that the elements in dic are unordered.

Common dictionary methods

The code is as follows:


>>> Print dic. keys () # return all dic keys
>>> Print dic. values () # return all dic values
>>> Print dic. items () # return all dic elements (key-value pairs)
>>> Dic. clear () # clear dic and change dict {}


There is also a common usage:

The code is as follows:


>>> Del dic ['Tom '] # Delete the 'Tom' element of dic.


Del is a reserved keyword in Python and is used to delete objects.

Similar to a table, you can use len () to query the total number of elements in the dictionary.

The code is as follows:


>>> Print (len (dic ))


Summary

Each element in the dictionary is a key-value pair. The element has no order.

The code is as follows:


Dic = {'Tom ': 11, 'Sam': 57, 'Lily ': 100}
Dic ['Tom '] = 99
For key in dic :...
Del, len ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.