Python 中連線元組的方法


當需要連線多個元組時,可以使用 '+' 運算子。元組是一種不可變資料型別。這就意味著,一旦定義了值,就不能透過訪問其索引元素來更改這些值。如果我們嘗試更改這些元素,就會導致錯誤。這些重要的包含是非常重要的,因為它們確保了只讀訪問。

可以用來新增數值或連線字串。'+' 運算子

以下是演示 −

示例

Live Demo

my_tuple_1 = (11, 14, 0, 78, 33, 11)
my_tuple_2 = (10, 78, 0, 56, 8, 34)

print("The first tuple is : ")
print(my_tuple_1)
print("The second tuple is : ")
print(my_tuple_2)

my_result = my_tuple_1 + my_tuple_2

print("The tuple after concatenation is : " )
print(my_result)

輸出

The first tuple is :
(11, 14, 0, 78, 33, 11)
The second tuple is :
(10, 78, 0, 56, 8, 34)
The tuple after concatenation is :
(11, 14, 0, 78, 33, 11, 10, 78, 0, 56, 8, 34)

說明

  • 定義了兩個元組並將其顯示在控制檯上。
  • 使用 '+' 運算子將它們連線起來。
  • 這將分配給一個值。
  • 它顯示在控制檯上。

更新時間:2021 年 3 月 12 日

5 千次以上觀看

開始您的 職業生涯

透過完成課程獲得認證

入門
廣告
© . All rights reserved.