cool hit counter Python programming for beginners from introduction to practice - using dictionaries_Intefrankly

Python programming for beginners from introduction to practice - using dictionaries


01

Dictionary Introduction

We learned about lists earlier and know how to add, modify and delete items in the shopping cart. However, the reality is that we don't just store just the name of the item, but in the vast majority of cases we also need to store the model number, specifications, unit price, and other information for each item as well, so that when we look at the purchase history, we can see the details of the item.

This is where a data structure called a "dictionary" in Python can help us. A dictionary is likewise one of Python's many data types, and it is defined using a pair of braces {}.

02

Practice Answers

6-1 persons. Use a dictionary to store information about an acquaintance, including first name, last name, age, and city of residence. The dictionary should contain the keys first_name, last_name, age, and city. Print out each item of information stored in that dictionary.

The 3 topics in the original textbook mainly practiced defining, initializing, and accessing the values in the dictionary, as above. Below, in order to fully practice to what you have learned in this subsection, you will need to complete a little extra practice.

Now, this person moved to live in another city. Please change the city of residence and print.

According to the relevant regulations, we have to store the ID number of each person for verification of identity. Please add a key "ID" to store the ID number and print it.

The age information is already included in the ID number and it is no longer necessary to store the age separately. Please delete the age key "age" and print out the dictionary to confirm that the deletion was successful.

03

Content Analysis

Theoretically, every individual in the real world can be defined by a dictionary variable. A key corresponds to an attribute in the entity object, and the value of the attribute can be of various types, including lists and dictionaries. We will learn, later in this chapter, how to store lists and dictionary type data in a dictionary.

When using dictionaries, be careful to use quotation marks for the "keys" in the dictionary, both when defining the dictionary and when accessing, modifying or adding values to the dictionary.

Also, similar to lists, the program throws an exception when deleting a key-value pair that does not exist. The conditional test statement to determine if a key exists is: "key_name" in dictionary (similar to determining if a value exists in a list).

That's it for today, if you have any suggestions or questions, you can leave them directly on the public page, or if the questions are more informative, you can send them to my email at

Feel free to shoot the breeze, thanks!

Long press the QR code below to follow the public number and get the latest information at the first time


Recommended>>
1、The Science Island driverless car passed the final acceptance
2、Do you know all the hot majors this year
3、The original has a variety of perspective spell three Zhang auxiliary software
4、Has anyone from Han Xues Wit and Magic noticed how she looks after aging Netizen Its so magical
5、Mission 2031 from 0 to 1 for driverless

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号