python中字典的幾種定義方式
阿新 • • 發佈:2018-12-02
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 can‘t 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中字典的幾種定義方式