Python Advanced 01 Dictionary __python

Source: Internet
Author: User

Author: Vamei Source: Http://www.cnblogs.com/vamei Welcome to reprint, please also retain this paragraph of the statement. Thank you.

The Basics tutorial introduces basic concepts, especially objects and classes.

The Advanced tutorial further expands the basic tutorial to illustrate the details of Python. Hopefully, after the advanced tutorials, you have a more comprehensive understanding of python.

As we said before, 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 are going to introduce a new class, A dictionary (dictionary). Like a list, a dictionary can store multiple elements. This object that stores multiple elements is called a container (container).

Basic Concepts

Common ways to create dictionaries:

>>>dic = {' Tom ': One, ' Sam ': #, ' Lily ': 100}

>>>print type (DIC)

A dictionary and a table are similar places that contain multiple elements, each of which is separated by commas. But the dictionary element contains two parts, a key and a value, which are commonly used as a string to denote a key, or a number or a truth to represent a key (immutable objects can be keys). The value can be any object. Key and value one by one correspond.

For example, in the above example, ' Tom ' corresponds to one, ' Sam corresponds to, ' lily ' corresponds to 100

Unlike a table, the elements of a dictionary have no order. You cannot reference elements by subscript. Dictionaries are referenced by keys.

>>>print dic[' Tom '

>>>dic[' tom ' = 30

>>>print DIC

To build a new, empty dictionary:

>>>dic = {}

>>>print DIC

The method of adding a new element to the dictionary:

>>>dic[' Lilei '] = 99

>>>print DIC

Here, we refer to a new key and give it the corresponding value.

Circular invocation of dictionary elements

DiC = {' Lilei ': N, ' Lily ': M, ' Sam ': M, ' Tom ': ' For
key in dic:
    print Dic[key]

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

With the results of print, we can again confirm that the elements in DIC are not sequential.

common methods of dictionaries

>>>print Dic.keys () # Return all keys to DIC

>>>print dic.values () # Returns all values of DIC

>>>print dic.items () # Returns all elements of DIC (key-value pairs)

>>>dic.clear () # empty dic,dict changed to {}

There is also a very common usage:

>>>del dic[' Tom '] # delete dic's ' tom ' element

The del is a reserved keyword in python for deleting objects.

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

>>>print (Len (DIC))

Summary

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

DIC = {' Tom ': One, ' Sam ': #, ' Lily ': 100}

dic[' Tom ' = 99

For key in DIC: ...

Del, Len ()

Related Article

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.