Getting one value from a tuple

后端 未结 2 1768
时光说笑
时光说笑 2020-11-27 03:26

Is there a way to get one value from a tuple in Python using expressions?

def tup():
  return (3, \"hello\")

i = 5 + tup()  # I want to add just the three
<         


        
2条回答
  •  悲&欢浪女
    2020-11-27 03:40

    You can write

    i = 5 + tup()[0]
    

    Tuples can be indexed just like lists.

    The main difference between tuples and lists is that tuples are immutable - you can't set the elements of a tuple to different values, or add or remove elements like you can from a list. But other than that, in most situations, they work pretty much the same.

提交回复
热议问题