python day 07

1、集合{}:可变数据类型,他里边的元素必须是不可变的,无序,不重复,

set1 = set({1,2,3})

print(set1)

# 集合的表现
'''set1 = set({1,2,3})
#set2 = {1,2,3,[2,3],{'name':'liang'}}  # 报错 不可变就是可哈希的
print(set1)
print(set2)  
'''
  # 集合的增删查,不能改
# set1 = {'jinzha','muzha','nezha','xiaoliongnv'}
# 增  随机增加
'''set1.add('小猪熊')
print(set1)
# update  加一个列表,列表中的每一个元素都在里边,随机
set1.update('abc')
print(set1)'''
 # 删 pop 随机删除
'''set1.pop()
print(set1)
print(set1.pop())
# remove 按元素删
set1.remove('nezha')
print(set1)
# clear 清空集合 set()表示空集合
set1.clear()
print(set1)
# del 删除整个集合 报错
del set1
print(set1)'''
 # 查 用for循环去查
'''for i in set1:
    print(i)
'''
     # 交集
'''set1 = {1,2,3,4,5}
set2 = {4,5,6,7,8}
print(set1 & set2)  # {4,5}
print(set1.intersection(set2))  # {4,5}'''
    # 并集
'''set1 = {1,2,3,4,5}
set2 = {4,5,6,7,8}
print(set1 | set2)  # {1,2,3,4,5,6,7,8}
print(set1.union(set2))  # {1,2,3,4,5,6,7,8}'''
   # 差集 set1 独有的 重复的部分删去
'''set1 = {1,2,3,4,5}
set2 = {4,5,6,7,8}
print(set1 - set2)  # {1,2,3}
print(set1.difference(set2))  # {1,2,3}'''
   # 子集与超集
'''set1 = {1,2,3}
set2 = {1,2,3,4,5,6}
print(set1 < set2)  # 说明set1是set2的子集 True
print(set1.issubset(set2))  # True
print(set2 > set1)  # 说明set2是set1的超集 True
print(set2.issuperset(set1))  # True'''

 # 面试题 列表中去除重复的,set比算法快
'''li = [1,2,33,33,2,1,4,5,6,6]
set1 = set(li)
print(set1)
li = list(set1)
print(li)'''
# 可变数据类型转换为不可变数据类型
# 创建不可变集合
'''b = frozenset('runoob')
print(b,type(b))
for i in b:  #可查无序的 不可增删改查
    print(i)'''
# 生成一个新的不可变集合
'''
a = frozenset(range(10))
print(a)'''

2、基础数据类型汇总补充

str   int   list   bool   dict   tupe 

Ctrl点击 str 或 int 可转到使用界面,可自己了解

#循环列表和字典的时候不要删他的东西,删就会很麻烦容易报错
dic = {'k1':'v1','k2':'v2','a3':'v3'}
# 1、循环字典当中你不能删除他的键
'''
dic1 = {}
for i in dic:
    if 'k' not in i:
        dic1.setdefault(i,dic[i])
dic = dic1
print(dic)
'''
 # 2、把含有k的键放在一个列表里,循环列表删除字典所要删除的东西
'''l = []
for i in dic:
    if 'k' in i :
        l.append(i)
for i in l:
    del dic[i]
print(dic)
'''
# 什么数据类型转化为bool是Forst
int = 0
str = ''
list = []
tup = ()
dict = {}
set = ()
# 元组
# 如果元组里边只有一个元素且不加','此元素是什么类型,print结果就是什么类型
'''tup1 = (1)
tup2 = (1,)
tup3 = ([1])
tup4 = ([1],)
print(tup1,type(tup1))
print(tup2,type(tup2))
print(tup3,type(tup3))
print(tup4,type(tup4))
'''

 

上一篇:python之路2:数据类型


下一篇:003_python的str切片,str常用操作方法,for循环,集合,深浅copy