1.集合
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 # s=set(['alex','alex','sb']) 4 # print(s) 5 6 # s=set('hello') 7 # print(s) 8 9 # s={1,2,3,4,5,6} 10 #添加 add 不能添加重复元素 11 # s.add('s') 12 # s.add('3') 13 # s.add(3) 14 # print(s) 15 16 # s.clear() 17 # print(s) 18 19 # s1=s.copy() 20 # print(s1) 21 22 # s={'sb',1,2,3,4,5,6} 23 #随机删 24 # s.pop() 25 # print(s) 26 27 #指定删除 28 # s.remove('sb') 29 # s.remove('hellol') #删除元素不存在会报错 30 # s.discard('sbbbb')#删除元素不存在不会报错 31 # print(s) 32 33 # python_l=['lcg','szw','zjw','lcg'] 34 # linux_l=['lcg','szw','sb'] 35 # p_s=set(python_l) 36 # l_s=set(linux_l) 37 # #求交集 38 # print(p_s,l_s) 39 # print(p_s.intersection(l_s)) 40 # print(p_s&l_s) 41 # #求并集 42 # print(p_s.union(l_s)) 43 # print(p_s|l_s) 44 # #差集 45 # print('差集',p_s-l_s) 46 # print(p_s.difference(l_s)) 47 # print('差集',l_s-p_s) 48 # print(l_s.difference(p_s)) 49 50 #交叉补集 51 # print('交叉补集',p_s.symmetric_difference(l_s)) 52 # print('交叉补集',p_s^l_s) 53 54 # python_l=['lcg','szw','zjw','lcg'] 55 # linux_l=['lcg','szw','sb'] 56 # p_s=set(python_l) 57 # l_s=set(linux_l) 58 # print(p_s,l_s) 59 # print('差集',p_s-l_s) 60 # p_s=p_s-l_s 61 # p_s.difference_update(l_s) 62 # print(p_s) 63 64 # s1={1,2} 65 # s2={2,3,5} 66 # print(s1.isdisjoint(s2)) 67 68 # s1={1,2} 69 # s2={1,2,3} 70 # print(s1.issubset(s2))#s1 是s2 的子集 71 # print(s2.issubset(s1))#False 72 73 # print(s2.issuperset(s1))#s1 是s2 的父集 74 75 # s1={1,2} 76 # s2={1,2,3} 77 # s1.update(s2) #更新多个值 78 79 # s1.add(1,2,3,4) #更新一个值 80 # s1.union(s2) #不更新 81 82 # print(s1) 83 84 # s=frozenset('hello') 85 # print(s) 86 # names=['alex','alex','wupeiqi'] 87 # # 88 # names=list(set(names)) 89 # print(names)
2.字符串格式化
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 # msg='i am %s my hobby is %s' % ('lhf','alex') 4 # print(msg) 5 # 6 # msg='i am %s my hobby is %s' % ('lhf',1) 7 # msg='i am %s my hobby is %s' % ('lhf',[1,2]) 8 # print(msg) 9 # name='lhf' 10 # age=19 11 # msg='i am %s my hobby is %s' % (name,age) 12 # print(msg) 13 14 #打印浮点数 15 # tpl = "percent %.2f" % 99.976234444444444444 16 # print(tpl) 17 18 #打印百分比 19 # tpl = 'percent %.2f %%' % 99.976234444444444444 20 # print(tpl) 21 22 # tpl = "i am %(name)s age %(age)d" % {"name": "alex", "age": 18} 23 # print(tpl) 24 # 25 # msg='i am %(name)+60s my hobby is alex' %{'name':'lhf'} 26 # print(msg) 27 # 28 # msg='i am \033[43;1m%(name)+60s\033[0m my hobby is alex' %{'name':'lhf'} 29 # print(msg) 30 31 32 # print('root','x','0','0',sep=':') 33 # print('root'+':'+'x'+':'+'0','0')
3.format字符串格式化
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 # tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18) 4 5 # tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18}) 6 # 7 # tpl = "i am {:s}, age {:d}".format(*["seven", 18]) 8 # tpl = "i am {:s}, age {:d}".format("seven", 18) #["seven", 18] 9 10 l=["seven", 18] 11 # tpl = "i am {:s}, age {:d}".format('seven',18) 12 # print(tpl) 13 # 14 # tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%},{}".format(15, 15, 15, 15, 15, 15.87623, 2) 15 # print(tpl)
4.函数
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 ''' 4 y=2*x+1 5 x=3 6 y->7 7 x=3 8 y->7 9 ''' 10 # def test(x): 11 # ''' 12 # 2*x+1 13 # :param x:整形数字 14 # :return: 返回计算结果 15 # ''' 16 # y=2*x+1 17 # return y 18 # 19 # def test(): 20 # ''' 21 # 2*x+1 22 # :param x:整形数字 23 # :return: 返回计算结果 24 # ''' 25 # x=3 26 # y=2*x+1 27 # return y 28 # a=test() 29 # print(a) 30 31 #过程:就是没有返回值的函数 32 33 34 # def test01(): 35 # msg = 'test01' 36 # print(msg) 37 # # 38 # # 39 # def test02(): 40 # msg = 'test02' 41 # print(msg) 42 # return msg 43 # # 44 # def test03(): 45 # msg = 'test03' 46 # print(msg) 47 # return 1,2,3,4,'a',['alex'],{'name':'alex'},None 48 # # 49 # def test04(): 50 # msg = 'test03' 51 # print(msg) 52 # return {'name':'alex'} 53 # t1=test01() 54 # t2=test02() 55 # t3=test03() 56 # t4=test04() 57 # print(t1) 58 # print(t2) 59 # print(t3) 60 # print(t4) 61 62 # def calc(x,y): #x=2,y=3 63 # res=x**y 64 # return x 65 # return y 66 # res=calc(2,3) 67 # print(x) 68 # print(y) 69 # print(res) 70 # # a=10 71 # # b=10 72 # # calc(a,b) 73 74 75 # def test(x,y,z):#x=1,y=2,z=3 76 # print(x) 77 # print(y) 78 # print(z) 79 80 #位置参数,必须一一对应,缺一不行多一也不行 81 # test(1,2,3) 82 83 #关键字参数,无须一一对应,缺一不行多一也不行 84 # test(y=1,x=3,z=4) 85 86 #位置参数必须在关键字参数左边 87 # test(1,y=2,3)#报错 88 # test(1,3,y=2)#报错 89 # test(1,3,z=2) 90 # test(1,3,z=2,y=4)#报错 91 # test(z=2,1,3)#报错 92 93 # def handle(x,type='mysql'): 94 # print(x) 95 # print(type) 96 # handle('hello') 97 # handle('hello',type='sqlite') 98 # handle('hello','sqlite') 99 100 # def install(func1=False,func2=True,func3=True): 101 # pass 102 103 #参数组:**字典 *列表 104 # def test(x,*args): 105 # print(x) 106 # print(args) 107 108 109 # test(1) 110 # test(1,2,3,4,5) 111 # test(1,{'name':'alex'}) 112 # test(1,['x','y','z']) 113 # test(1,*['x','y','z']) 114 # test(1,*('x','y','z')) 115 116 # def test(x,**kwargs): 117 # print(x) 118 # print(kwargs) 119 # test(1,y=2,z=3) 120 # test(1,1,2,2,2,2,2,y=2,z=3) 121 # test(1,y=2,z=3,z=3)#会报错 :一个参数不能传两个值 122 123 def test(x,*args,**kwargs): 124 print(x) 125 print(args,args[-1]) 126 print(kwargs,kwargs.get('y')) 127 # test(1,1,2,1,1,11,1,x=1,y=2,z=3) #报错 128 # test(1,1,2,1,1,11,1,y=2,z=3) 129 # 130 # test(1,*[1,2,3],**{'y':1})