题解 | #遍历字典#
遍历字典
https://www.nowcoder.com/practice/0315639767824323a2cdb9ee3f998314
#!/usr/local/bin/python3 #遍历字典 operator_dict = {'<':'less than' ,'==':'equal'} print('Here is the original dict:') (operator_dict) for i in sorted(operator_dict): print(f"Operator {i} means {operator_dict[i]}.") operator_dict['>'] = 'greater than' print() print('The dict was changed to:') for i in sorted(operator_dict): print(f"Operator {i} means {operator_dict[i]}.")新增一个知识点,输出的时候使用f""里面能直接使用索引输出字典的键和值。