python的数学操作符
时间:2007-09-15 来源:jcodeer
Python的操作符
测试常见的几种操作符,整型与浮点支持所有的操作符,高级数据类型部分支持或不支持
|
Plus(+) |
Minus(-) |
Multiply(*) |
Div(/) |
Mod(%) |
Pow(**) |
integer |
√ |
√ |
√ |
√ |
√ |
√ |
float |
√ |
√ |
√ |
√ |
√ |
√ |
complex |
√ |
√ |
√ |
√ |
× |
√ |
list |
√ |
× |
- |
× |
× |
× |
string |
√ |
× |
- |
× |
× |
× |
dictionary |
× |
× |
× |
× |
× |
× |
√ 支持 × 不支持 -不完全支持
测试代码
#plus(+)
nInt1 = 10
nInt2 = 20
print 'nInt1 + nInt2 = %d' % (nInt1 + nInt2)
print 'nInt1 - nInt2 = %d' % (nInt1 - nInt2)
print 'nInt1 * nInt2 = %d' % (nInt1 * nInt2)
print 'nInt1 / nInt2 = %d' % (nInt1 / nInt2)
#how to use escape %
print 'nInt1 %% nInt2 = %d' % (nInt1 % nInt2)
print 'nInt2 ** nInt2 = %d' % (nInt1 ** nInt2)
fFloat1 = 3.14
fFloat2 = 4.13
print 'fFloat1 + fFloat2 = %f' % (fFloat1 + fFloat2)
print 'fFloat1 - fFloat2 = %f' % (fFloat1 - fFloat2)
print 'fFloat1 * fFloat2 = %f' % (fFloat1 * fFloat2)
print 'fFloat1 / fFloat2 = %f' % (fFloat1 / fFloat2)
print 'fFloat1 %% fFloat2 = %f' % (fFloat1 % fFloat2)
print 'fFloat1 ** fFloat2 = %f' % (fFloat1 ** fFloat2)
cCom1 = 1 + 2j
cCom2 = 2 + 1j
print 'cCom1 + cCom2 = ',cCom1 + cCom2
print 'cCom1 - cCom2 = ',cCom1 - cCom2
print 'cCom1 * cCom2 = ',cCom1 * cCom2
print 'cCom1 / cCom2 = ',cCom1 / cCom2
#print 'cCom1 %% cCom2 = ',cCom1 % cCom2
print 'cCom1 ** cCom2 = ',cCom1 ** cCom2
lList1 = [1,2,3,4]
lList2 = [4,3,2,1]
print 'lList1 + lList2 = ',lList1 + lList2
#print 'lList1 - lList2 = ',lList1 - lList2
#
#print 'lList1 * lList2 = ',lList1 * lList2
print 'lList1 * 3 = ',lList1 * 3
#print 'lList /3 = ',lList1 / 3
#print lList1 % 2
#print lList1 ** 2
sStr1 = '1234'
sStr2 = '4321'
print 'sStr1 + sStr2 = ',sStr1 + sStr2
#print 'sStr1 1 sStr2 = ',sStr1 - sStr2
#print 'sStr1 * sStr2 = ',sStr1 * sStr2
print 'sStr1 * 3 = ',sStr1 * 3
#print 'sStr1 / 3 = ',sStr1 / 3
#print sStr1 % 3
#print sStr1 ** 1
#do not support plus of dictionary
#dDict1 = {1:'1',2:'2',3:'3',4:'4'}
#dDict2 = {100:'100',200:'200',300:'300',400:'400'}
#print dDict1 + dDict2