Python入门01
目录
Python 里面万物皆对象(object),只要是对象,就有相应的属性 (attributes) 和方法(methods)。
当把布尔型变量用在数字运算中,用 1 和 0 代表 True 和 False。
学习链接:AI训练营Python-阿里云天池
身份运算符is,is not
is, is not 对比的是两个变量的内存地址
==, != 对比的是两个变量的值
比较的两个变量,指向的都是地址不可变的类型(str等),那么is,is not 和 ==,!= 是完全等价的。
对比的两个变量,指向的是地址可变的类型(list,dict,tuple等),则两者是有区别的。
Python 里面万物皆对象(object),只要是对象,就有相应的属性 (attributes) 和方法(methods)。
b = dir(int)
print(b)
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__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']
对它们有个大概印象就可以了,具体怎么用,需要哪些参数 (argument),还需要查文档。看个bit_length()
的例子。
找到一个整数的二进制表示,再返回其长度。
a = 1031
print(bin(a)) # 0b10000000111
print(a.bit_length()) # 11
0b10000000111
11
有时候我们想保留浮点型的小数点后 n 位。可以用 decimal 包里的 Decimal 对象和 getcontext()方法来实现。getcontext()显示了 Decimal 对象的默认精度值是 28 位 (prec=28)。
import decimal
from decimal import Decimal
a = decimal.getcontext()
print(a)
b = Decimal(1) / Decimal(3)
print(b)
Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999, capitals=1, clamp=0, flags=[], traps=[InvalidOperation, DivisionByZero, Overflow])
0.3333333333333333333333333333
使 1/3 保留 4 位,用 getcontext().prec
来调整精度。
import decimal
from decimal import Decimal
a = decimal.getcontext()
print(a)
b = Decimal(1) / Decimal(3)
print(b)
decimal.getcontext().prec = 4
c = Decimal(1) / Decimal(3)
print(c)
Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999, capitals=1, clamp=0, flags=[], traps=[InvalidOperation, DivisionByZero, Overflow])
0.3333333333333333333333333333
0.3333
当把布尔型变量用在数字运算中,用 1
和 0
代表 True
和 False
。
print(True + True)
print(True + False)
print(True * False)
2
1
0
print()函数
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
- 将对象以字符串表示的方式格式化输出到流文件对象file里。其中所有非关键字参数都按
str()
方式进行转换为字符串输出; - 关键字参数
sep
是实现分隔符,比如多个参数输出时想要输出中间的分隔字符; - 关键字参数
end
是输出结束时的字符,默认是换行符\n
; - 关键字参数
file
是定义流输出的文件,可以是标准的系统输出sys.stdout
,也可以重定义为别的文件; - 关键字参数
flush
是立即把内容输出到流文件,不作缓存。
利用位运算实现快速计算
通过 <<
,>>
快速计算2的倍数问题。
n << 1 #计算 n*2
n >> 1 #计算 n/2,负奇数的运算不可用
n << m #计算 n*(2^m),即乘以 2 的 m 次方
n >> m #计算 n/(2^m),即除以 2 的 m 次方
1 << n # 2^n
通过 ^
快速交换两个整数。 通过 ^
快速交换两个整数。
a,b=3,7
a^=b
b^=a
a^=b
print(a,b)
7 3
通过 a & (-a)
快速获取a
的最后为 1 位置的整数。
【例】
00 00 01 01 # 5
&
11 11 10 11 # -5
-----
00 00 00 01 # 1
00 00 11 10 #14
&
11 11 00 10 #-14
-----
00 00 00 10 # 2
print(14&-14)
print(5&-5)
2
1
利用位运算实现整数集合
一个数的二进制表示可以看作是一个集合(0 表示不在集合中,1 表示在集合中)。
比如集合 {1, 3, 4, 8}
,可以表示成 01 00 01 10 10
而对应的位运算也就可以看作是对集合进行的操作。
元素与集合的操作:
a | (1<<i) # 把 i 插入到集合中
a & ~(1<<i) # 把 i 从集合中删除
a & (1<<i) # 判断 i 是否属于该集合(零不属于,非零属于)
集合之间的操作:
a 补 # ~a
a 交 b # a & b
a 并 b # a | b
a 差 b # a & (~b)
注意:整数在内存中是以补码的形式存在的,输出自然也是按照补码输出。
【例】 Python 的bin()
输出。
print(bin(3)) # 0b11
print(bin(-3)) # -0b11
print(bin(-3 & 0xffffffff)) # 0b11111111111111111111111111111101
print(bin(0xfffffffd)) # 0b11111111111111111111111111111101
print(0xfffffffd) # 4294967293
0b11
-0b11
0b11111111111111111111111111111101
0b11111111111111111111111111111101
4294967293
从结果可以看出:
Python中bin
一个负数(十进制表示),输出的是它的原码的二进制表示加上个负号。
Python中的整型是补码形式存储的。