search

Home  >  Q&A  >  body text

Python3的问题

>>> items = [1, 10, 7, 4, 5, 9]
>>> def sum(items):
... head, *tail = items
... return head + sum(tail) if tail else head
>>> sum(items)
36
>>>

谁能帮忙解释下为什么第四行的时候要用:
if tail else head 
这是什么意思呢?详细讲述下过程,多谢。

高洛峰高洛峰3024 days ago673

reply all(1)I'll reply

  • 三叔
  • Cancelreply