点击运行
#!/usr/bin/python3 # 通过创建新元组将元素添加到元组 my_tuple = ('one', 'two') new_tuple = my_tuple + ('three', ) # 逗号很重要 # ('one', 'two', 'three') print(new_tuple) # -------------------- # 通过重新分配将元素添加到元组 my_tuple_2 = ('one', 'two') my_tuple_2 += ('three',) # 逗号很重要 # ('one', 'two', 'three') print(my_tuple_2) # ------------------------- # 通过解包将元素添加到元组 my_tuple_3 = ('one', 'two') my_str = 'three' new_tuple_3 = (*my_tuple_3, my_str) # ('one', 'two', 'three') print(new_tuple_3)
运行结果 :
正在执行...