1. 程式人生 > >python中字典的幾種定義方式

python中字典的幾種定義方式

def trace syn syntax invalid last ict print class

# 方式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中字典的幾種定義方式