本系列将会陆续整理分享一些的Python内置函数。
-
通过百度网盘获取:
链接:https://pan.baidu.com/s/11x9_wCZ3yiYOe5nVcRk2CQ?pwd=mnsj
提取码:mnsj
-
前往GitHub获取:
https://github.com/returu/Python_built-in_functions
-
tuple() 函数:
tuple(iterable)
-
iterable:一个可以迭代的对象,比如列表、集合、字符串等。如果省略该参数,将返回一个空的元组。
返回值:
tuple(iterable)
-
iterable:一个可以迭代的对象,比如列表、集合、字符串等。如果省略该参数,将返回一个空的列表。
返回值:
下面是一些函数示例:
-
示例 1:tuple() 函数
# 将列表转换为元组
my_list = [1, 2, 3, 4]
my_tuple = tuple(my_list)
print(my_tuple)
# 输出: (1, 2, 3, 4)
# 将字符串转换为元组
my_string = "hello"
my_tuple = tuple(my_string)
print(my_tuple)
# 输出: ('h', 'e', 'l', 'l', 'o')
# 将集合转换为元组
my_set = {1, 2, 3, 4}
my_tuple = tuple(my_set)
print(my_tuple)
# 输出: (1, 2, 3, 4)
# 不传递参数,返回空元组
empty_tuple = tuple()
print(empty_tuple)
# 输出: ()
-
示例 2:list() 函数
# 将元祖转换为列表
my_tuple = (1, 2, 3, 4)
my_list = list(my_tuple)
print(my_list)
# 输出: [1, 2, 3, 4]
# 将字符串转换为列表
my_string = "hello"
my_list = list(my_string)
print(my_list)
# 输出: ['h', 'e', 'l', 'l', 'o']
# 将集合转换为列表
my_set = {1, 2, 3, 4}
my_list = list(my_set)
print(my_list)
# 输出: [1, 2, 3, 4]
# 不传递参数,返回空列表
empty_list = list()
print(empty_list)
# 输出: []


本篇文章来源于微信公众号: 码农设计师