Python Learning notes (06)

Source: Internet
Author: User

Dictionary {}

tuple () list [] Dictionary {}       

Dictionary is the only mapping type (hash table) in Python

The Dictionary object is mutable, but the key of the dictionary must use the immutable object, and a dictionary can use different key values

Keys () or values () returns a list of key or value

Items () returns a tuple containing key-value pairs


How to define:

dic={' name ': ' SQL ', ' age ': +, ' gender ': ' Male '}

Value:

>>>dic[' name ']

Sql

>>>dic[age]

30

Note: Key can be a variable, or it can be a string

Access updates directly with key values; The built-in update () method can copy the entire content into another dictionary

Add a value

dict1[' tel ']= ' 123456 ' * added after position unordered

Modify a value

dict1[' tel ']= ' 234569 '

Update and delete



Del dict1[' A '] removes the element with a key value of a in the dictionary

· Dict1.pop (' a ') deletes and returns the element with the key ' a '

· Dict1.clear () Delete all elements of a dictionary

· Del (dict1) Delete entire dictionary

Len (), hash () is used to determine whether an object can do a dictionary key, non-hash type report TypeError error

Dict.clear () Delete all elements in the dictionary

Create and return a dictionary with the elements in Seq  , Val                                        defaults to default value

Dict.get (Key,default=none) returns the value of key, if the key does not exist, returns the values specified by default

Example: ss={' a ': 123, ' B ': 456}

Ss.get (A,error)

>>>123

Ss.get (' C ', error)

>>>error

Dict.has_key (key) to determine if a key exists in the dictionary, it is recommended to use in or not

Dict.items () Returns a list of key-value pairs of tuples

Dict.keys () returns the list of keys in the dictionary

dict.iter* () iteriterms (), Iterkeys (), itervalues () returns the iteration instead of the list

same as Get (), the difference is if key exists, delete and return                               Dict[key], if there is no tangent default value not specified , throw Keyerror exception

with Set (), if key is present, return to its                                                    value, if it does not exist Dict[key] =default

add the key-value pairs in dict2 to the dictionary dict if                                       Add

Dict.values () Returns a list of all values in the dictionary




This article is from the "Ride a Pig to Travel" blog, please be sure to keep this source http://songqinglong.blog.51cto.com/7591177/1710006

Python Learning notes (06)

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.