python开发学习 day14-python集合/函数
set集合
# s=set('hello')
# print(s)
#
# s=set(['alex','alex','sb'])
# print(s)
# s={1,2,3,4,5,6}
#添加
# s.add('s')
# s.add('3')
# s.add(3)
# print(s)
# s.clear()
# print(s)
# s1=s.copy()
s={'sb',1,2,3,4,5,6}
#随机删
# s.pop()
#指定删除
# s.remove('sb')
# s.remove('hellol') #删除元素不存在会报错
# s.discard('sbbbb')#删除元素不存在不会报错
# print(s)
# python_l=['lcg','szw','zjw','lcg']
# linux_l=['lcg','szw','sb']
# p_s=set(python_l)
# l_s=set(linux_l)
# #求交集
# print(p_s,l_s)
# print(p_s.intersection(l_s))
# print(p_s&l_s)
# #求并集
# print(p_s.union(l_s))
# print(p_s|l_s)
# #差集
# print('差集',p_s-l_s)
# print(p_s.difference(l_s))
# print('差集',l_s-p_s)
# print(l_s.difference(p_s))
#交叉补集
# print('交叉补集',p_s.symmetric_difference(l_s))
# print('交叉补集',p_s^l_s)
python_l=['lcg','szw','zjw','lcg']
linux_l=['lcg','szw','sb']
p_s=set(python_l)
l_s=set(linux_l)
print(p_s,l_s)
# print('差集',p_s-l_s)
# p_s=p_s-l_s
p_s.difference_update(l_s)
print(p_s)
# s1={1,2}
# s2={2,3,5}
# print(s1.isdisjoint(s2))
s1={1,2}
s2={1,2,3}
print(s1.issubset(s2))#s1 是s2 的子集
print(s2.issubset(s1))#False
print(s2.issuperset(s1))#s1 是s2 的父集
s1={1,2}
s2={1,2,3}
# s1.update(s2) #更新多个值
# s1.add(1,2,3,4) #更新一个值
# s1.union(s2) #不更新
print(s1)
s=frozenset('hello')
print(s)
names=['alex','alex','wupeiqi']
names=list(set(names))
print(names)
字符串格式化
# msg='i am %s my hobby is %s' % ('lhf','alex')
# print(msg)
#
# msg='i am %s my hobby is %s' % ('lhf',1)
# msg='i am %s my hobby is %s' % ('lhf',[1,2])
# print(msg)
# name='lhf'
# age=19
# msg='i am %s my hobby is %s' % (name,age)
# print(msg)
#打印浮点数
tpl = "percent %.2f" % 99.976234444444444444
print(tpl)
#打印百分比
tpl = 'percent %.2f %%' % 99.976234444444444444
print(tpl)
tpl = "i am %(name)s age %(age)d" % {"name": "alex", "age": 18}
print(tpl)
msg='i am %(name)+60s my hobby is alex' %{'name':'lhf'}
print(msg)
msg='i am \033[43;1m%(name)+60s\033[0m my hobby is alex' %{'name':'lhf'}
print(msg)
print('root','x','0','0',sep=':')
# print('root'+':'+'x'+':'+'0','0')
format
# tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18)
#
# tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18})
# tpl = "i am {:s}, age {:d}".format(*["seven", 18])
# tpl = "i am {:s}, age {:d}".format("seven", 18) #["seven", 18]
#
# l=["seven", 18]
# tpl = "i am {:s}, age {:d}".format('seven',18)
# print(tpl)
tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%},{}".format(15, 15, 15, 15, 15, 15.87623, 2)
print(tpl)
函数
'''
y=2*x+1
x=3
y->7
x=3
y->7
'''
# def test(x):
# '''
# 2*x+1
# :param x:整形数字
# :return: 返回计算结果
# '''
# y=2*x+1
# return y
#
# def test():
# '''
# 2*x+1
# :param x:整形数字
# :return: 返回计算结果
# '''
# x=3
# y=2*x+1
# return y
# a=test()
# print(a)
#过程:就是没有返回值的函数
# def test01():
# msg = 'test01'
# print(msg)
#
#
# def test02():
# msg = 'test02'
# print(msg)
# return msg
#
# def test03():
# msg = 'test03'
# print(msg)
# return 1,2,3,4,'a',['alex'],{'name':'alex'},None
#
# def test04():
# msg = 'test03'
# print(msg)
# return {'name':'alex'}
# t1=test01()
# t2=test02()
# t3=test03()
# t4=test04()
# print(t1)
# print(t2)
# print(t3)
# print(t4)
# def calc(x,y): #x=2,y=3
# res=x**y
# return x
# return y
# res=calc(2,3)
# # print(x)
# # print(y)
# print(res)
# # a=10
# # b=10
# # calc(a,b)
# def test(x,y,z):#x=1,y=2,z=3
# print(x)
# print(y)
# print(z)
#位置参数,必须一一对应,缺一不行多一也不行
# test(1,2,3)
#关键字参数,无须一一对应,缺一不行多一也不行
# test(y=1,x=3,z=4)
#位置参数必须在关键字参数左边
# test(1,y=2,3)#报错
# test(1,3,y=2)#报错
# test(1,3,z=2)
# test(1,3,z=2,y=4)#报错
# test(z=2,1,3)#报错
# def handle(x,type='mysql'):
# print(x)
# print(type)
# handle('hello')
# handle('hello',type='sqlite')
# handle('hello','sqlite')
# def install(func1=False,func2=True,func3=True):
# pass
#参数组:**字典 *列表
def test(x,*args):
print(x)
print(args)
# test(1)
# test(1,2,3,4,5)
# test(1,{'name':'alex'})
# test(1,['x','y','z'])
# test(1,*['x','y','z'])
# test(1,*('x','y','z'))
# def test(x,**kwargs):
# print(x)
# print(kwargs)
# test(1,y=2,z=3)
# test(1,1,2,2,2,2,2,y=2,z=3)
# test(1,y=2,z=3,z=3)#会报错 :一个参数不能传两个值
def test(x,*args,**kwargs):
print(x)
print(args,args[-1])
print(kwargs,kwargs.get('y'))
# test(1,1,2,1,1,11,1,x=1,y=2,z=3) #报错
# test(1,1,2,1,1,11,1,y=2,z=3)
# test(1,*[1,2,3],**{'y':1})

浙公网安备 33010602011771号