个中键是指向对应值的索引,我们须要应用键而拜访对应的元素值:
- dictionary_tk = {
- "name": "Leandro",
- "nickname": "Tk",
- "nationality": "Brazilian"
- }
- print("My name is %s" %(dictionary_tk["name"])) # My name is Leandro
- print("But you can call me %s" %(dictionary_tk["nickname"])) # But you can call me Tk
- print("And by the way I'm %s" %(dictionary_tk["nationality"])) # And by the way I'm Brazilian
以上创建了一个字典,个中定义了四个键与对应的值,print 函数内应用了字典的键以获取对应的值。此外,字典的值可以应用任何类型的数据,如下我们添加了一个键为字符型,值为数值型的键-值对。
- dictionary_tk = {
- "name": "Leandro",
- "nickname": "Tk",
- "nationality": "Brazilian",
- "age": 24
- }
- print("My name is %s" %(dictionary_tk["name"])) # My name is Leandro
- print("But you can call me %s" %(dictionary_tk["nickname"])) # But you can call me Tk
- print("And by the way I'm %i and %s" %(dictionary_tk["age"], dictionary_tk["nationality"])) # And by the way I'm Brazilian
下面我们须要懂得若何添加元素到字典中,扑晡仓典的本质就是指向特定值的关键字的集合。是以我们可以直接将某个值付与到字典某个关键字(可以不存在)中而修改或添加键值对。
- dictionary_tk = {
推荐阅读
完成这些后,启动 MultiBootUSB。 本教程介绍如安在一个 U 盘上安装多个 Linux 发行版。如许,你可以在单个 U 盘上享受多个现场版live Linux 发行版了。我爱好经由过程 U 盘测验测验不合的>>>详细阅读
本文标题:从变量到封装:一文带你为机器学习打下坚实的Python基础
地址:http://www.17bianji.com/lsqh/37927.html
1/2 1