python之 tuple元组

tuple使用(), list最大区别:元组中元素不能修改。   元组: 序列类型。

元组中只包含一个元素时,需要在元素后面添加逗号,否则括号会被当作运算符使用

  • >>>tup1 = (50) >>> type(tup1)→<class 'int'>           # 不加逗号,类型为整型
  • >>>tup1 = (50,) >>> type(tup1)→<class 'tuple'>    # 加逗号,类型为元组

元组中元素不可删除,但是可以删除整个元组del tuple

运算符:重复,迭代,判断元素是否存在,连接,计算元素个数(与list一致)

元组满足索引和切片截取。

元组内置函数:

  • len(tuple)       计算元组元素个数。
  • max(tuple)    返回元组中元素最大值。
  • min(tuple)     返回元组中元素最小值。
  • tuple(list)       将列表转换为元组。

猜你喜欢

转载自blog.csdn.net/qq_42422981/article/details/83871241