字符串格式化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=19msg=‘i am %s my hobby is %s‘ % (name,age)print(msg) 打印浮点数tpl = "percent %.2f" % 99.976234444444444444print(tpl) 打印百分比tpl = ‘percent %.2f %%‘ % 99.976234444444444444print(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‘)集合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_sp_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))#Falseprint(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)函数‘‘‘y=2*x+1x=3y->7x=3y->7‘‘‘def test(x): ‘‘‘ 2*x+1 :param x:整形数字 :return: 返回计算结果 ‘‘‘ y=2*x+1 return ydef test(): ‘‘‘ 2*x+1 :param x:整形数字 :return: 返回计算结果 ‘‘‘ x=3 y=2*x+1 return ya=test()print(a)过程:就是没有返回值的函数def test01(): msg = ‘test01‘ print(msg)def test02(): msg = ‘test02‘ print(msg) return msgdef test03(): msg = ‘test03‘ print(msg) return 1,2,3,4,‘a‘,[‘alex‘],{‘name‘:‘alex‘},Nonedef 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 yres=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})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)
原文地址:https://www.cnblogs.com/xuegaobuliang/p/8783111.html
时间: 2024-10-10 21:20:52