Python Learning: Dictionaries

Source: Internet
Author: User

Dictionary

1. Querying memory Addresses

A = 10

Print (ID (a))

b = A

Print (ID (b))

b = 15

Print (ID (b))

2. Data type

Immutable types: Integer, string, tuple

Mutable type: list, dictionary

The value of a dictionary can be any type, and the key can only be an immutable type.

The dictionary store is unordered. For example:

DIC = {' name ': ' YSW ', ' age ': $, ' hobby ': ' Game ', ' Is_handsome ': True}

Print (DIC)

Print (dic[' name '])

#dic = {1: ' ysw ', ' age ': $, ' hobby ': ' Game ', ' Is_handsome ': True}

#print (DIC)

#print (Dic[1])

#dic = {[up]: ' YSW ', ' age ': $, ' hobby ': ' Game ', ' Is_handsome ': True}

#print (DIC)

#print (dic[[1,2]])

#dic = {' name ': ' YSW ', ' age ': $, ' hobby ': {' game_name ': ' wow ', ' level ': ' + '}, ' Is_handsome ': True}

#print (dic[' hobby ')

#dic = {' 1 ': ' 123 '}, ' age ': $, ' hobby ': {' game_name ': ' wow ', ' level ': ' + '}, ' Is_handsome ': True}

#print (DIC)

#字典两大特点: unordered; key unique

#dic = {' age ': ' YSW ', ' age ': $, ' hobby ': ' Game ', ' Is_handsome ': True}

#print (DIC)

3. Two ways to create a dictionary:

#dic1 = {' name ': ' YSW '}

#dic2 = Dict ((' Name ', ' YSW '),)

4. Corresponding operation

(1) Increase

Dic1 = {' name ': ' YSW '}

Dic1[' age '] = 18

Print (DIC1)

dic1[' name '] = 18

Print (DIC1)

#没有重复的键就添加, replace the value with a duplicate key

------------------------------

Dic1 = {' name ': ' YSW '}

Dic1[' age '] = 18

Print (DIC1)

Dic1.setdefault (' age ', 36)

Print (DIC1)

#dic1. SetDefault (' hobby ', ' game ')

#print (DIC1)

#setdefault的特点: No duplicate keys are added, there is no change to duplicate keys

Python Learning: Dictionaries

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.