首页 > 编程语言 > 详细

python中字典的几种定义方式

时间:2018-12-01 22:47:51      阅读:190      评论:0      收藏:0      [点我收藏+]
# 方式1
>>> d = dict(name=Bob, age=20, score=88)
>>> print(d)
{name: Bob, age: 20, score: 88}

>>> d = dict("name"=Bob, "age"=20, "score"=88)
SyntaxError: keyword cant be an expression

>>>d = dict("name":Bob, "age":20, "score":88)
SyntaxError: invalid syntax

# 方式2

>>> d = {"name":Bob, "age":20, "score":88}
>>> print(d)
{name: Bob, age: 20, score: 88}

>>> d = {"name"=Bob, "age"=20, "score"=88}
SyntaxError: invalid syntax

>>> d = {name:Bob, age:20, score:88}
Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    d = {name:Bob, age:20, score:88}
NameError: name name is not defined

 

python中字典的几种定义方式

原文:https://www.cnblogs.com/david-lcw/p/10051275.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!