python基础---整型字符串列表元组字典内置方法和文件操作介绍
Posted 小曹
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python基础---整型字符串列表元组字典内置方法和文件操作介绍相关的知识,希望对你有一定的参考价值。
对于python而言,一切事物都是对象,对象是基于类创建的,对象继承了类的属性,方法等特性
1.int
首先,我们来查看下int包含了哪些函数
# python3.x dir(int) # [‘__abs__‘, ‘__add__‘, ‘__and__‘, ‘__bool__‘, ‘__ceil__‘, ‘__class__‘, ‘__delattr__‘, ‘__dir__‘, ‘__divmod__‘, ‘__doc__‘, ‘__eq__‘, ‘__float__‘, ‘__floor__‘, ‘__floordiv__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getnewargs__‘, ‘__gt__‘, ‘__hash__‘, ‘__index__‘, ‘__init__‘, ‘__int__‘, ‘__invert__‘, ‘__le__‘, ‘__lshift__‘, ‘__lt__‘, ‘__mod__‘, ‘__mul__‘, ‘__ne__‘, ‘__neg__‘, ‘__new__‘, ‘__or__‘, ‘__pos__‘, ‘__pow__‘, ‘__radd__‘, ‘__rand__‘, ‘__rdivmod__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__rfloordiv__‘, ‘__rlshift__‘, ‘__rmod__‘, ‘__rmul__‘, ‘__ror__‘, ‘__round__‘, ‘__rpow__‘, ‘__rrshift__‘, ‘__rshift__‘, ‘__rsub__‘, ‘__rtruediv__‘, ‘__rxor__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__sub__‘, ‘__subclasshook__‘, ‘__truediv__‘, ‘__trunc__‘, ‘__xor__‘, ‘bit_length‘, ‘conjugate‘, ‘denominator‘, ‘from_bytes‘, ‘imag‘, ‘numerator‘, ‘real‘, ‘to_bytes‘] # python 2.x dir(int) # [‘__abs__‘, ‘__add__‘, ‘__and__‘, ‘__class__‘, ‘__cmp__‘, ‘__coerce__‘, ‘__delattr__‘, ‘__div__‘, ‘__divmod__‘, ‘__doc__‘, ‘__float__‘, ‘__floordiv__‘, ‘__format__‘, ‘__getattribute__‘, ‘__getnewargs__‘, ‘__hash__‘, ‘__hex__‘, ‘__index__‘, ‘__init__‘, ‘__int__‘, ‘__invert__‘, ‘__long__‘, ‘__lshift__‘, ‘__mod__‘, ‘__mul__‘, ‘__neg__‘, ‘__new__‘, ‘__nonzero__‘, ‘__oct__‘, ‘__or__‘, ‘__pos__‘, ‘__pow__‘, ‘__radd__‘, ‘__rand__‘, ‘__rdiv__‘, ‘__rdivmod__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__rfloordiv__‘, ‘__rlshift__‘, ‘__rmod__‘, ‘__rmul__‘, ‘__ror__‘, ‘__rpow__‘, ‘__rrshift__‘, ‘__rshift__‘, ‘__rsub__‘, ‘__rtruediv__‘, ‘__rxor__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__sub__‘, ‘__subclasshook__‘, ‘__truediv__‘, ‘__trunc__‘, ‘__xor__‘, ‘bit_length‘, ‘conjugate‘, ‘denominator‘, ‘imag‘, ‘numerator‘, ‘real‘]
# __abs__() 绝对值输出 num = 1 result = num.__abs__() print(result) num = -1 result = num.__abs__() print(result)
1 num = -1 2 result = num.__add__(2) 3 4 print(result) 5 6 # 打印结果将输出 1
1 num = 5 2 result = num.__and__(2) 3 print(result) 4 5 #打印输出为0 6 # 0 0 0 0 0 1 0 1 5 7 # 0 0 0 0 0 0 1 0 2 8 #相同位为1则为1,由于没有相同位,所以5 & 2结果为0
1 # 以下结果输出都是True 2 num = 11 3 print(num.__bool__()) # True 4 5 num = -11 6 print(num.__bool__()) #True 7 8 # 以下结果输出都是 False 9 num = 0 10 print(num.__bool__()) #False 11 12 num = None 13 num = False 14 print (num.__bool__()) # False
#通过divmod函数可以实现将一个int类型对象除以另一个int对象得到一个两个元素的列表, #列表左边为除尽取整的值,第二个元素为取模的余数 num = 9 result = num.__divmod__(2) print(result) #输出(4,1)
num = 2 result = num.__eq__(3) print(result) #打印结果为False #2 == 3 结果为假 result = num.__eq__(2) print(result) #打印结果为True # 2 == 2 结果为真 __eq__ ==比较运算符
num = 9 print(num.__float__()) #打印结果为 9.0
num = int(181) result = num.__floordiv__(9) print(result) #打印输出20 #地板除 //取整 __floordiv__地板除//
num = int(181) result = num.__getattribute__("bit_length") print(result) #打印输出 <built-in method bit_length of int object at 0x100275020> #说明该数据类型num存在bit_length这个属性,可以用于判断对象是否拥有某种属性
num = int(181) print(num.__ge__(111)) #打印输出结果为True #因为181大于111,所以结果为真,该属性用于判断大于等于该属性自身的方法,结果将返回真,否则为假
num = 181 print(int.__invert__(num)) #打印输出-182 num = -180 print(int.__invert__(num)) #打印输出179 num = -181 print(int.__invert__(num)) #打印输出180
num = -181 result = num.__le__(111) print(result) #打印输出结果为True #当传人参数与对象本身相比较,只要对象小于或者等于传人的参数,则结果为真,否则为假
num = -181 result = num.__lshift__(1) print(result) #打印输出结果为-362 ,即-181 *( 2**1) result = num.__lshift__(2) print(result) #打印输出结果为-724 ,即-181*(2**2) #当传入参数大于等于0时且对象本身不能为0,首先参数本身为2的指数幂运算,然后再与对象本身相乘结果则为左移最终结果
num = -181 print(num.__lt__(11)) #打印输出结果为True #凡是对象比传入的参数小,则结果为真,否则结果为假
num = -181 print(num.__mod__(3)) #打印输出结果为2,因为-181除以3等于60,余数为2,所以结果为2
num = 181 print(num.__mul__(2)) #打印输出结果为362,即181*2的结果
num = -181 print(int.__neg__(num)) #打印结果为181,即-(-181),结果为181
num = 181 print(num.__ne__(181)) #打印结果为False print(num.__ne__(11)) #打印结果为True #凡是传入参数与对象本身不相等,则结果为真,否则为假
num = 18 print(num.__or__(7)) #打印输出结果为23 # 0 0 0 1 0 0 1 0 18 # 0 0 0 0 0 1 1 1 7 # 0 0 0 1 0 1 1 1 23 位的或运算,凡是相同位有一位为真,即为1,则结果为真,即1,然后所以最终结果为23
num = 9 print(num.__pow__(2)) #打印输出结果为81,即9**2
num = 6 print(num.__rdivmod__(3)) #返回结果(0,3) 左边为余数,右边为整除的结果
#python 2.7 num = 1 print(num.__sizeof__()) #打印输出结果为24个字节,说明一个int类型默认就在内存中占用了24个字节大小 #python3.5 num = 1 print(num.__sizeof__()) #打印输出结果为28个字节,说明一个int类型数据默认在内存中占用了24个字节大小
num = int(1111) result = num.__str__() print(type(result)) #打印输出结果为<class ‘str‘> #将int类型转换为str数据类型
num = int(9) print(num.__sub__(2)) #打印输出结果为7 #对象本身减去传入参数,得到最终的返回值
num = 11 print(num.__truediv__(3)) #打印输出结果为3.6666666666666665 #返回的数据类型为float,浮点型
num = 10 print(num.__xor__(6)) # 0 0 0 0 1 0 1 0 10 # 0 0 0 0 0 1 1 0 6 # 0 0 0 0 1 1 0 0 12 #同位比较,都是0则为假,都是1则为假,一真一假为真
num = 5 print(num.bit_length()) #打印输出结果为3 # 0 0 0 0 0 1 0 1 #长度为3位
num = 2.3 - 2.5j result = num.real #复数的实部 print(result) #打印输出2.3 result = num.imag #复数的虚部 print(result) #打印输出2.5j result = num.conjugate() #返回该复数的共轭复数 print(result) #打印输出(2.3+2.5j)
num = 5 print(num.__format__("20")) #表示5前面讲话有20个空格
print(int.from_bytes(bytes=b‘1‘, byteorder=‘little‘) #打印输出 49 ,即将字符1转换为十进制
num = 2 result = num.to_bytes(5,byteorder=‘little‘) print(result) #打印输出b‘\x02\x00\x00\x00\x00‘ for i in result: print(i) #打印输出2\n0\n0\n0\n0 #\n表示回车
以上是关于python基础---整型字符串列表元组字典内置方法和文件操作介绍的主要内容,如果未能解决你的问题,请参考以下文章