【Python面试必看系列】之Python 中的字典是什么?

版权声明:本文为博主原创文章,转载请注明出处! https://blog.csdn.net/PoGeN1/article/details/82316680

Q 12. Python 中的字典是什么?

字典是 C++ 和 Java 等编程语言中所没有的东西,它具有键值对。

>>> roots={25:5,16:4,9:3,4:2,1:1}
>>> type(roots)
<class 'dict'>
>>> roots[9]

运行结果为:

3

字典是不可变的,我们也能用一个推导式来创建它。

>>> roots={x**2:x for x in range(5,0,-1)}
>>> roots

运行结果:

{25: 5, 16: 4, 9: 3, 4: 2, 1: 1}

猜你喜欢

转载自blog.csdn.net/PoGeN1/article/details/82316680
今日推荐