''' @Modify Time @Author ------------ ------- python基本语法 2019/10/26 8:16 laoalo ''' import functools from filecmp import cmp print('#'*50,"字符串") dss = " hello pyqt.5 " s1 = dss.strip() s2 = dss.lstrip() s3 = dss.rstrip() s4 = dss.rsplit() print("原>>",dss) print("s1>>",s1) print("s2>>",s2) print("s3>>",s3) print("s4>>",s4) ''' strip : 返回的是字符串的副本,并删除前导和后缀字符。意思就是你想去掉字符串里面的哪些字符,那么你就把这些字符当参数传入。 此函数只会删除头和尾的字符,中间的不会删除。如果strip()的参数为空,那么会默认删除字符串头和尾的空白字符。 lstrip: 去除(字符串)开始的 rstrip: 去除(字符串)末尾的 rsplit: 通过指定分隔符对字符串进行分割并返回一个列表,默认分隔符为所有空字符. 类似于 split() 方法,只不过是从字符串最后面开始分割。 ''' print('*'*50) s2 = dss.join(['a','.','c']) print('s2>>',s2) s3='s3' s3+='xx' print('s3>>',s3) ''' join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串。 ''' print('*'*50) css="abc1c2c3" pi=css.find('c') print('pi>>',pi) print("*"*50) s1,s2='abc','ABC123efg' print(s1,':大写>>',s1.upper()) print(s2,':小写>>',s2.lower()) print(s2,":大小写互换",s2.swapcase()) print(s1,":首字母大写",s1.capitalize()) print('#'*50,"列表") import operator ''' python3移除了cmp()函数,但提供了六个丰富的比较运算符 import operator #首先要导入运算符模块 operator.gt(1,2) #意思是greater than(大于) operator.ge(1,2) #意思是greater and equal(大于等于) operator.eq(1,2) #意思是equal(等于) operator.le(1,2) #意思是less and equal(小于等于) operator.lt(1,2) #意思是less than(小于) ''' ls1=[1,2,3,4] ls2=[8,5,6] tp=(11,22,33,44) print("cmp>>",operator.gt(ls1,ls2)) print("len>>",len(ls1)) print("max>>",max(ls1)) print("min>>",min(ls1)) print("list>>",list(tp)) print("*"*30) ls1=[1,2,3,4] ls1.append('a') print("append>>",ls1) ls1.insert(1,'b') print("insert>>",ls1) print("pop>>",ls1.pop()) #默认最后一个 print("*"*30) ls1=[1,2,3,4,5,6,1,2,3,4,6,5,4] ls1.remove(2) print("remove>>",ls1) #只移除第一个匹配项 ls1.reverse() print('reverse>>',ls1) ls2.sort() print('sort>>',ls2) print('#'*50,"字典") dict1={'a':97,'b':98} dict2={'A':65,'B':66} # print("cmp>>",cmp(dict1,dict2)) print("len>>",len(dict1)) print("str>>",str(dict1)) print("type>>",type(dict1)) print("#"*50,"partial函数的使用") def add(a,b): return a+b print("add(4+2)=",add(4,2)) p3 = functools.partial(add,3) p5 = functools.partial(add,5) r2 = p3(4) print("p3(4)=",r2) r3 = p3(7) print("p(7)=",r3) r4 = p5(10) print('p5(7)=',r4) print('#'*50,"类的动态属性") class MyClass(object): def __init__(self): self.__param = None def getParam(self): print("get param:%s " % self.__param) return self.__param def setParam(self,value): print("set param:%s " % self.__param) self.__param = value def delParam(self): print("del param:%s " % self.__param) del self.__param param= property(getParam,setParam,delParam) class MyClass1(object): def __init__(self): self.__param = None @property def param(self): print("get param: %s " % self.__param) return self.__param @param.setter def param(self,value): print("set param: %s " % self.__param) self.__param = value @param.deleter def param(self): print("del param: %s " %self.__param) del self.__param if __name__ == '__main__': cls = MyClass() cls.param = 10 print("current param:%s " % cls.param) del cls.param print("*"*30) cls1 = MyClass1() cls1.param = 10 print("current param: %s " % cls1.param) del cls1.param