python 创建tuple
时间: 2023-09-09 22:13:26 浏览: 109
48Python之创建单元素tuple共2页.pdf.zi
在Python中,我们可以使用圆括号 () 来创建元组。例如:
tuple1 = ('a', 'b', 'c')
print(tuple1) # ('a', 'b', 'c')
除了使用圆括号,我们还可以使用 tuple() 函数来创建元组。这个函数偏向于将其他类型转换为元组类型。例如:
str1 = 'Hello Python'
print(tuple(str1)) # ('H', 'e', 'l', 'l', 'o', ' ', 'P', 'y', 't', 'h', 'o', 'n')
list1 = ['Hello', 'Python']
print(tuple(list1)) # ('Hello', 'Python')
dict1 = {'Hello': 'Python', 'name': 'pink'}
print(tuple(dict1)) # ('Hello', 'name')
set1 = {'Hello', 'Python', 'name', 'pink'}
print(tuple(set1)) # ('Hello', 'name', 'Python', 'pink')
range1 = range(1, 6)
print(tuple(range1)) # (1, 2, 3, 4, 5)
所以,我们可以使用圆括号或者 tuple() 函数来创建元组。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [Python 元组tuple详解(超详细)](https://blog.csdn.net/m0_70885101/article/details/125999020)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
阅读全文