Python day 1, python get the first day of this week

Source: Internet
Author: User

Python day 1, python get the first day of this week

Dict key-value set.

1 d = {2     '1': 'a',3     '2': 'b',4     '3': 'c'5 }

(1) obtain the length of a set: len (d) = 3
(2) obtain the value:

Method 1: d [key]; example: d ['1'] = 'A'

If the key does not exist, the system reports a keyError. You can use

If '1' in d:

Print (d ['1'])

Method 2: d. get (key) method. If the key does not exist, None is returned.

Example: d. get ('1 ')

(3) dist features

The first feature of dist is that the search speed is fast. No matter whether dict has 10 or 0.1 million elements, the search speed is the same.. The search speed of list gradually decreases with the increase of elements.

However, there is no cost for fast dict search,The disadvantage of dict is that it occupies a large amount of memory and wastes a lot of content., The list is the opposite. The memory usage is small, but the query speed is slow. Because dict searches by key, keys in a dict cannot be repeated.

The second feature of dict is that the stored key-value pairs have no order.

The third feature of dict is that the key elements must be unchangeable. The basic types of Python, such as strings, integers, and floating-point numbers, can all be immutable. However, if the list is variable, it cannot be used as the key..

(4)

Dict is variable. You can assign a value through d ['4'] = 'D'. If the key already exists, update it.

(5) traverse dict

For key in d:

Print (key)

 

AM

 

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.