判断题 偏易
#16024

在Python中,下面这段代码的输出结果是{1: 0, 2: 0, 3: 0, 4: 0, 5: 0}。( ) student_scores = dict.fr…

在Python中,下面这段代码的输出结果是{1: 0, 2: 0, 3: 0, 4: 0, 5: 0}。( )

student_scores = dict.fromkeys(range(1, 6), 0)

print(student_scores)

未登录也能判分,登录后才会记录
答案与解析
正确答案:对
解析

range(1, 6)生成一个从1到5的整数序列。dict.fromkeys()方法会将这个序列的每个元素作为键,将指定的值(这里是0)作为每个键对应的值,生成一个字典。因此输出为{1: 0, 2: 0, 3: 0, 4: 0, 5: 0}。