{site_name}

{site_name}

🌜 搜索

在Python中,类型转换是将一个数据类型的值转换为另一个数据类型的过程

Python 𝄐 0
python类型转换int,Python类型转换代码,python类型转换函数有哪些,python类型转换astype,python类型转换函数的作用,python类型转换的作用
在Python中,类型转换是将一个数据类型的值转换为另一个数据类型的过程。Python中有多种类型转换函数可用。

以下是一些示例:

1. 将字符串转换为整数:

string_num = "123"
integer_num = int(string_num)
print(integer_num) # 输出:123


2. 将整数转换为浮点数:

integer_num = 123
float_num = float(integer_num)
print(float_num) # 输出:123.0


3. 将浮点数转换为字符串:

float_num = 3.14
string_num = str(float_num)
print(string_num) # 输出:"3.14"


4. 将列表转换为元组:

list_of_numbers = [1, 2, 3, 4, 5]
tuple_of_numbers = tuple(list_of_numbers)
print(tuple_of_numbers) # 输出:(1, 2, 3, 4, 5)


5. 将元组转换为列表:

tuple_of_numbers = (1, 2, 3, 4, 5)
list_of_numbers = list(tuple_of_numbers)
print(list_of_numbers) # 输出:[1, 2, 3, 4, 5]