3.3 Dictionary: U disk with editable data + data label

Time:2021-11-24


(1) What is a dictionary

A dictionary (called dictionary in English) is also a data type that can store multiple data, which is very similar to a list. However, the elements in the dictionary store data in the form of “key: value”, and wrap the data with “{}”, as shown in the following figure.


(2) Dictionary output and assignment

Dictionary output

The dictionary outputs the value of a specified element by looking for its key. If the key of the element is not found, an error will occur.

Take a closer look at the following code and learn how to output dictionary elements.

A = {‘Apple‘: 1, ‘Banana‘: 5, ‘pear’: 10} print (a [‘Apple’]) print (a [‘Orange’])

Use the online cloud editor and enter the above code. The execution effect is as follows:

It can be found that when the key of the dictionary element has an “apple”, the output value of the element is 1; When there is no “orange” in the key of the element, the program will report an error.

Dictionary assignment

The assignment of dictionary elements can be completed in the form of “dictionary name [key] = value”, as shown in the following figure.

Carefully observe the following code to learn how to assign dictionary elements.

A = {‘Apple’: 1, ‘Banana’: 5, ‘pear’: 10} a [‘Apple’] = 10 a [‘Banana’] = 50 a [‘pear’] = 100 print (a)

Use the online cloud editor and enter the above code. The execution effect is as follows:


(3) Common operations of dictionary

Addition of dictionary elements

If you want to add elements to the dictionary, you can directly use the form of “dictionary name [key] = value”, as shown in the following figure.

Observe the following code carefully and learn how to add dictionary elements.

A = {‘Apple’: 1, ‘Banana’: 5, ‘pear’: 10} a [‘Orange’] = 30 print (a)

Use the online cloud editor and enter the above code. The execution effect is as follows:

Deletion of dictionary elements

If you want to delete an element in the dictionary, use the Del keyword, as shown in the following figure.

Carefully observe the following code to learn how to delete dictionary elements.

A = {‘Apple’: 1, ‘Banana’: 5, ‘pear’: 10} del a [‘Banana’] print (a)

Use the online cloud editor and enter the above code. The execution effect is as follows:


Challenge

If you have finished today’s knowledge, try the following challenging tasks!


Wechat campaign weekly (implemented by Dictionary)

The following figure shows the weekly report of wechat movement steps. Use the “dictionary” knowledge to create a movement step dictionary. The data are 8752, 4512, 14520, 1230, 3652, 7852 and 9852 respectively.

[source code: resource pack \ day 3 \ 3-3-challenge. Py]

3.3 Dictionary: U disk with editable data + data label


Here is the YuQue document. Click the link to view it:https://www.yuque.com/codeclub/gnec1v/ihgl72

Recommended Today

Seven solutions for distributed transactions

1、 What is distributed transaction Distributed transaction means that transaction participants, transaction supporting servers, resource servers and transaction managers are located on different nodes of different distributed systems. A large operation is completed by more than n small operations. These small operations are distributed on different services. For these operations, either all of them are […]