8-13. Python Hash Review

Source: Internet
Author: User

1. {} input is curly brace

Mycat = {' Size ': ' fat ', ' color ': ' Gray ', ' disposition ': ' Loud '} Key: Value

mycat[' size ' = fat

2, 3 Methods of the dictionary:

Keys (), values (), items (), the values returned by these methods are not real lists, they cannot be modified, but can be traversed with a for loop

For V in Mycat.values ():

Print (v)

For I in Mycat.keys ():

Print (i)

For k,v in Mycat.items ():

Print (k ': ' V)

3. Check if there is a key-value in the dictionary:

' Name ' in Spam.keys ()

' Value ' in Spam.values ()

4. Get () Method:

Before accessing the value of a key, it is troublesome to check that the key exists in the dictionary, and the Get () method is a good solution to the problem: if the key is present, the value is returned, otherwise the alternate value is returned

Spam.get (' CPUs ', 0)

4, SetDefault () method:

The SetDefault () method provides a way to join the key if a key does not exist in the hash and set it to the passed value, and if the key exists in the hash, the value of the key is returned. This method ensures that a key exists. Very practical

Spam = {' name ': ' Pooka ', ' Age ', 5}

Spam.setdefault (' Color ', ' write ')

8-13. Python Hash Review

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.