Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use setdefault () in python

Shulou Source: shulou.com Published: 2022-06-03 12:11:04 09月13日 Update

Editor to share with you how to use setdefault () in python, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

1. The parameter key represents the key name. If the key exists, the key value corresponding to the key will be returned, otherwise a new key value will be added.

2. The key value of the new key is specified by the parameter default. The return value is the key value corresponding to the key name. If no key value is specified, the return value is None.

Grammar

Dict.setdefault (key, default=None)

Example

Print ("add numerical values to the dictionary") scores = {'Mathematics': 95, 'English': 92, 'Chinese': 84, 'Chemistry': 90, 'Biology': 91, 'Physics': 80} scores ['History'] = 80print (scores) scores.setdefault ('Geography', 89) print (scores) above are all the contents of the article "how to use setdefault () in python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Articles content parameters not much chemistry history geography most dictionaries examples numerical values mathematics more physics biology knowledge industry Chinese grammar information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Redmi Shulou Technology Linux vpn