分片:分片操作的实现需要提供两个索引作为边界,第一个包含在分片内,第二个不包含 number =[1,2,3,4,5,6,7,8,9,10] number [3:6] -->[4,5,6] number [0,1] -->[1] number [-3,-1] -->[8,9] number [-3,0] -->[ ] (当第一个索引比第二个晚出现在序列中,则是空序列) number [-3 :] -->如果分片所得部分包含头或者尾,则可以把索引置空 number [ :…
1. 如果想实现与某个内置类型具有类似行为的类时,最好的方法就是将这个内置类型子类化. 2. 内置类型子类化,其实就是自定义一个新类,使其继承有类似行为的内置类,通过重定义这个新类实现指定的功能. class newDictError(ValueError): '''如果向newDict添加重复值,则引发此异常''' class newDict(dict): '''不接受重复值的字典''' def __setitem__(self, key, value): if value in self.v…
python中判断某一个元素是否在一个列表中,可以使用关键字in 和 not in. 示例如下: 如果需要输出相应的信息,可以搭配使用if语句,这里不赘述. ------------------------------------------------------------------------------------------分割线------------------------------------------------------------------------------…
1. reversed() a = [1, 2, 3, 4] for i in reversed(a): print(i) 2. range(len(a)-1, -1, -1) a = [1, 2, 3, 4] for i in range(len(a)-1, -1, -1): print(a[i]) 3. range(len(a)) + ~操作符 ~按位取反运算符:对数据的每个二进制位取反,即把1变为0,把0变为1 .~x 类似于 -x-1 a = [1, 2, 3, 4] for i in…
By default, an object is considered true unless its class defines either a __bool__() method that returns False or a __len__() method that returns zero, when called with the object. Here are most of the built-in objects considered false: constants de…