Python learning 2: basic explanation of the dictionary and basic explanation of the python dictionary

Source: Internet
Author: User

Python learning 2: basic explanation of the dictionary and basic explanation of the python dictionary

By NiceCui

  • This document is not reposted. If you need to reposted, you must obtain the author's consent. Thank you.
  • Link: http://www.cnblogs.com/NiceCui/p/7862377.html
  • Email: moyi@moyibolg.com
  • Date:

Python learning 2: basic dictionary explanation

(You don't need to learn more. You can learn to have fun with small projects)

I. Introduction

In fact, the Python dictionary is similar to the HashMap in java. It is implemented through hash tables and stores key-value pairs. That is to say, the dictionary is an array, the index of the array is obtained after the key is processed by the hash function. In fact, you may encounter many similar storage methods when learning programming languages. For example, the cache system Memcached stores data using key-value pairs, which are also available in Redis, therefore, key-value pairs are widely used in programming languages.

A dictionary is a container that can contain multiple elements, but its location is not used as an index. A dictionary allows you to create data indexes in a custom manner.

Ii. dictionary Analysis

A dictionary can contain multiple elements, each of which is well separated;

The dictionary element consists of two parts: Key and Value. The Key is the data index, the Value is the data itself, and the Key and Value are one-to-one correspondence.

 

1 #! /Usr/bin/python 2 # coding: UTF-8 3 ''' 4 Created on 5 @ author: NiceCui 6 ''' 7 8 example_dict = {"a": 1, "B": 2, "c": 3} 9 10 print (type (example_dict) # output <type 'dict '> 11 12 print (example_dict ["a"]) # output 113 14 print (example_dict ["B"]) # output 215 16 print (example_dict ["c"]) # output 3

 

3. modify or add an element value in the dictionary.

1 #! /Usr/bin/python 2 # coding: UTF-8 3 ''' 4 Created on 5 @ author: NiceCui 6 ''' 7 8 example_dict = {"a": 1, "B": 2, "c ": 3} 9 10 example_dict ["a"] = 10 # modify the value of key "a" to 1011 12 example_dict ["B"] = 20 # change the value of key "B" 2013 14 example_dict ["d"] = 4 # increase the key to "d" and the value to 415 16 print (example_dict) # output: {'A': 10, 'C': 3, 'B': 20, 'D': 4}

Create a new empty dictionary

#! /Usr/bin/python # coding: UTF-8 ''' Created on @ author: NiceCui ''' example _ dict ={}# empty dictionary print (example_dict) # output {}

The dictionary does not have sequential continuous order, so it is suitable for storing a group of data with a loose structure.

For example, the down payment ratio and tax rate can be in the same dictionary:

1 #!/usr/bin/python2 #coding:utf-83 '''4 Created on 2017-11-195 @author: NiceCui6 '''7 8 9 example_dict ={"premium":0.2,"tax":0.15}

Iv. Summary

There is not much content in this study. I only learned one knowledge point. This will make my learning more relaxed. I don't have to worry about it. It's just a large size, so that my heart is full of pressure, this article write python language is used in eclipse installed python plug-in, installation Tutorial: http://www.cnblogs.com/NiceCui/p/7858107.html

In dictionary examples and most application scenarios, we use strings as Dictionary keys. However, other types of data, such as arrays and boolean values, can also be used as Dictionary keys for simple learning.

 

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.