若 a = (1, 2, 3),下列哪些操作是合法的?
A a[1:-1]
B a*3
C a[2] = 4
D list(a)

题解

In [1]: a = (1, 2, 3)

In [2]: a[1:-1]

Out[2]: (2,)

In [3]: a*3
Out[3]: (1, 2, 3, 1, 2, 3, 1, 2, 3)

In [5]: a[2] = 4    # 元组为不可变对象,不可以赋值
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-fdddcd2211ce> in <module>()
----> 1 a[2] = 4
TypeError: 'tuple' object does not support item assignment

In [6]: list(a)
Out[6]: [1, 2, 3]

答案:A B D

相关文章:

  • 2021-10-30
  • 2021-11-08
  • 2022-12-23
  • 2022-12-23
  • 2021-06-04
  • 2021-12-18
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-09-12
  • 2021-04-03
  • 2021-06-14
  • 2021-11-29
  • 2021-05-22
  • 2022-01-21
相关资源
相似解决方案