python空集合如何表示
python中,用set来表示一个无序不重复元素的序列。set的主要作用就是用来给数据去重。
可以使用大括号{}或者set()函数创建集合,但是注意如果创建一个空集合必须用set()而不是{},因为{}是用来表示空字典类型的。
set的集合的创建与使用
#1.用{}创建set集合
person={"student","teacher","babe",123,321,123}#同样各种类型嵌套,可以赋值重复数据,但是存储会去重
print(len(person))#存放了6个数据,长度显示是5,存储是自动去重.
print(person)#但是显示出来则是去重的
'''
5
{321,'teacher','student','babe',123}
'''
#空set集合用set()函数表示
person1=set()#表示空set,不能用person1={}
print(len(person1))
print(person1)
'''
0
set()
'''
#3.用set()函数创建set集合
person2=set(("hello","jerry",133,11,133,"jerru"))#只能传入一个参数,可以是list,tuple等类型
print(len(person2))
print(person2)
'''
5
{133,'jerry',11,'jerru','hello'}
'''
以上内容为大家介绍了python培训之空集合如何表示,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注IT培训机构:千锋教育。

猜你喜欢LIKE
相关推荐HOT
更多>>
python中的filter函数功能是什么?
python中的filter函数功能是什么?在python中,面对众多的数据,我们要过滤筛选出我们需要的数据。python中的filter函数就是起到了过滤筛选的作...详情>>
2023-11-10 20:37:27
pythontime模块是什么
pythontime模块是什么在python中使用时间,就免不了和time模块打交道,另外两个模块这个暂时先不做介绍。做time模块的使用上,我们可以用它来对...详情>>
2023-11-10 15:53:16
python是什么编程语言
python是什么编程语言1、说明是一种面向对象、解释型计算机程序设计语言,由GuidovanRossum于1989年底发明,第一个公开发行版发行于1991年。Pyt...详情>>
2023-11-10 15:21:05
python异常处理的两种技巧
python异常处理的两种技巧1、传递异常有时我们会在捕捉到一个异常后重新引发它(传递异常),实现起来很简单,使用不带参数的raise语句即可。deff...详情>>
2023-11-10 14:49:39热门推荐
python中的filter函数功能是什么?
沸python delattr函数如何使用?
热python中pdb模块怎么用?
热Python如何截图保存?
新python中缺少module怎么办?
python strftime和strptime的不同分析
python time.strptime的格式化
python中@contextmanager是什么?
python对象的三要素是什么
pythonGIL在Python多线程的应用
python如何对多个CSV文件进行读取
pythonif嵌套命令如何理解?
python对列表进行永久性或临时排序的方法
python生成器调用方法引发异常
技术干货






