# 创建元组的示例代码
# 方法1:使用圆括号 ()
tuple1 = (1, 2, 3, 4, 5)
print("方法1创建的元组:", tuple1)
# 方法2:省略圆括号,直接用逗号分隔元素
tuple2 = 6, 7, 8, 9, 10
print("方法2创建的元组:", tuple2)
# 方法3:使用内置函数 tuple()
tuple3 = tuple([11, 12, 13, 14, 15])
print("方法3创建的元组:", tuple3)
# 方法4:创建空元组
empty_tuple = ()
print("空元组:", empty_tuple)
# 方法5:创建单元素元组(注意要加逗号)
single_element_tuple = (16,)
print("单元素元组:", single_element_tuple)
()
来创建元组,这是最常见的方法。tuple()
,可以将其他可迭代对象(如列表)转换为元组。上一篇:python fsolve
下一篇:python中[:]用法
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站