码迷,mamicode.com
首页 > 编程语言 > 详细

python中字典的几种定义方式

时间:2018-12-01 23:39:22      阅读:356      评论:0      收藏:0      [点我收藏+]

标签:express   ack   lin   class   invalid   call   name   shell   back   

# 方式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中字典的几种定义方式

标签:express   ack   lin   class   invalid   call   name   shell   back   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!