Python——各种数据类型之间的异同

2022-10-27 11:37:34

字符串,列表,元组,集合之间的相互转换及差别。

str1='helloworld'print(str1)#字符串转列表,list()
list1=list(str1)print(list1)#字符串转元组,tuple()
tup=tuple(str1)print(tup)#列表转元组
tup1=tuple(list1)print(tup1)#字符串转集合,set()
set1=set(str1)print(set1)#集合中元素不重复#列表转集合
set2=set(list1)print(set2)#元组转集合
set3=set(tup)print(set3)
helloworld['h','e','l','l','o','w','o','r','l','d']('h','e','l','l','o','w','o','r','l','d')('h','e','l','l','o','w','o','r','l','d'){'d','o','e','h','r','l','w'}{'d','o','e','h','r','l','w'}{'d','o','e','h','r','l','w'}
数据类型比较	 字符串  列表  元组  字典  集合
是否有序	     是	     是	   是	 否	   否
是否可修改	 否	     是	   不	 是	   是

可变类型与不可变类型
可变类型,值可以改变:

  1. 列表 list
  2. 字典 dict
  3. 集合 set

不可变类型,值不可以改变:

  1. 数值类型 int, long(Python3去除), bool, float
  2. 字符串 str
  3. 元组 tuple
  • 作者:南方有大雪
  • 原文链接:https://blog.csdn.net/weixin_45054387/article/details/105486771
    更新时间:2022-10-27 11:37:34