Learning Python 10.12
157页开始,是10.11晚开始的,既然是新的一章,索性放在第二天的内容里吧
复数:两种声明方式(怀疑大多数人永远都不会用到)
1 c1 = 5+2j 2 c2 = complex(5,2) 3 print(c1==c2) 4 #True
运算符:Python3与Python2对除号的定义有所不同,需要特别注意,受截图范围所限,省略了表中与数学运算关系不大或较为复杂的内容(如yield、lambda)

个人觉得Python与其他语言较为明显的不同有几个:
- 真除与floor除法(Python2.6与Python3定义又有所不同,但2.6可以from __future__ import division来保持与3一致,说实话我觉得这设定有点奇葩)
- 没有自增自减
- 比较运算符可以连用(如a>b>c)
- 运算符可以重载(by the way,刚看到的,java里String的+和=不是通过代码而是通过编译器实现的)。
数字的格式化显示:
1 num = 1/3.0 2 print(num) 3 #0.3333333333333333 4 print('%e' % num) 5 #3.333333e-01 6 print('{0:4.2f}'.format(num)) # 7 #0.33
其中第4行是老式的format风格,第6行是新式的format风格。关于老式format的说明如下:


新的format更加强大,不过现在我也没完全弄清,也没找到类似上表的资料,所以暂且放过,如果找到资料再补上
整数的精度:喜欢上Python完全因为简洁和整数处理的方便(可以参见Project Euler),Python里整数的精度是无限的。是不是无限我不清楚,但10086**10086这种计算是几乎瞬间出结果的。如果再大一些(类似65535**65535)可能会比较久(我没等到它出结果),但并不是算不出,而可能是转成可以显示的输出比较久,因为65536**65536%100也是几乎瞬间出结果的,以上为个人意见,如有不对还望指正。
进制转换:
1 print(0b11,0o77,0xFF) 2 #3 63 255 3 print(bin(9),oct(9),hex(9)) 4 #0b1001 0o11 0x9 5 print(int("0xff",16),eval("0xff")) 6 #255 255
其他内置数学工具:
1 print(pow(2,3),abs(-1),sum((1,2,3)),max(1,2,3),min(1,2,3),int(1.2),round(1.9),int(-1.2),round(-1.9)) 2 #8 1 6 3 1 1 2 -1 -2 3 4 import math 5 print(math.floor(1.2),math.trunc(1.2),math.floor(-1.2),math.trunc(-1.2),math.sqrt(144)) 6 #1 1 -2 -1 12.0 7 print(math.pi,math.sin(math.pi/2),math.pi/math.asin(1)) 8 #3.141592653589793 1.0 2.0 9 print(math.e,math.log(math.e),math.log10(100),math.log1p(math.e-1)) 10 #2.718281828459045 1.0 2.0 1.0 #最后一个是计算1+x的自然对数 11 12 import random 13 print(random.random()) 14 #0~1的数 15 print(random.randint(1,100)) 16 #1~100的整数 17 print(random.choice((1,2,3))) 18 #随机选择一个
需要注意的是,sum()和random.choice()中的参数都是tuple,而不是三个数字
Decimal:精确,并且可以控制精确的精度
1 from decimal import Decimal 2 print(0.1+0.1+0.1-0.3) 3 #5.551115123125783e-17 4 print(Decimal(0.1)+Decimal(0.1)+Decimal(0.1)-Decimal(0.3)) 5 #2.775557561565156540423631668E-17 6 print(Decimal('0.1')+Decimal('0.1')+Decimal('0.1')-Decimal('0.3')) 7 #0.0 8 print(Decimal(1)/Decimal(7)) 9 #0.1428571428571428571428571429 10 import decimal 11 decimal.getcontext().prec = 2 12 print(Decimal(1)/Decimal(7)) 13 #0.14 14 with decimal.localcontext() as ctx: 15 ctx.prec = 3 16 print(Decimal(1)/Decimal(7)) 17 #0.143 18 print(Decimal(1)/Decimal(7)) 19 #0.14 #书里说此处精度会被重置为默认值(Python 3.0),但Python3.2.3中实际重置为了之前的精度
Fraction:精确的分数
1 from fractions import Fraction 2 print(Fraction(1,3)+Fraction(0.25)+Fraction('0.1')) 3 #41/60 4 print((2.5).as_integer_ratio()) 5 #(5,2) #as_integer_ratio是float类的一个方法,返回tuple 6 print(Fraction(*2.5.as_integer_ratio())) 7 #5/2 #*为自动解包(将tuple化为多个参数),相当于Fraction(5,2) 8 print(Fraction.from_float(2.5)) 9 #5/2 10 print(Fraction(49,99).limit_denominator(50)) 11 #1/2
Set:
1 s1 = {'a','b','c'} 2 s2 = {'a','e','i','o','u'} 3 s3 = {'a'} 4 print(s3.copy()) 5 #{'a'} #shallow copy!!! 6 print(s1.isdisjoint(s3)) 7 #False #True if their intersection is empty 8 print(s3.issubset(s1)) #s1<=s3 9 #True 10 print(s1.issuperset(s3)) #s1>=s3 11 #True 12 print(s1.union(s2)) #s1|s2 #s1.update(s2) 13 #{'a', 'c', 'b', 'e', 'i', 'o', 'u'} 14 print(s1.intersection(s2)) #s1&s2 #s1.intersection_update(s2) 15 #{'a'} 16 print(s1.difference(s2)) #s1-s2 #s1.difference_update(s2) 17 #{'c', 'b'} 18 print(s1.symmetric_difference(s2)) #s1^s2 #s1.symmetric_difference_update(s2) 19 #{'c', 'b', 'e', 'i', 'o', 'u'} 20 21 s1.add('z') 22 s1.remove('z') #raise KeyError if not present 23 s1.discard('z') #remove if present 24 s1.pop() #return arbitrary element or raise KeyError when empty 25 s1.clear()
注意交并差异或的返回新set版本、操作符版本和自身更新版本(也有相应操作符版本为相应s1+=s2等)的区别。相比操作符版本,使用方法的版本可接受任何iterable类型的参数,可能导致未预料的结果。所以如需要避免类型错误应优先使用操作符版本。(set.intersection(str)会自动将str解包然后计算,set-str会失败)
set里不能包含可变类型(list和dictionary),但可以包含tuple。如果想让set本身不可变,可以使用frozenset
Boolean:True == 1但True is not 1。
如果需要更强大的数字相关操作,可以使用NumPy(号称强大到可以作为Matlab的免费替代品,可惜中国Matlab都是已经免费的,还有大量的熟练操作人员...)。
至195页。
周末不弄这个,一切顺利的话希望下周五可以进行到310页。
浙公网安备 33010602011771号