如何在python中打印键值对中的值

如何在python中打印键值对中的值,python,Python,编译后我得到一个错误,说 class_roll = {'amit':1,'suresh:':2,'mahesh':23,'rajesh':13,'chandrashekar':21} print(class_roll) print('the roll no of amit is:{0}'.format(class_roll('amit')) ) 您可以稍微更改代码并获得预期的结果 TypeError: 'dict' object is not callable 输出为 class_roll

编译后我得到一个错误,说

class_roll = {'amit':1,'suresh:':2,'mahesh':23,'rajesh':13,'chandrashekar':21}
print(class_roll)
print('the roll no of amit is:{0}'.format(class_roll('amit')) )

您可以稍微更改代码并获得预期的结果

TypeError: 'dict' object is not callable
输出为

class_roll = {'amit':1,'suresh:':2,'mahesh':23,'rajesh':13,'chandrashekar':21}
print(class_roll)
print('the roll no of amit is:{0}'.format(class_roll['amit']) )

要访问dict元素,请使用
[]
,而不是
()
{'amit': 1, 'suresh:': 2, 'mahesh': 23, 'rajesh': 13, 'chandrashekar': 21}
the roll no of amit is:1