这篇文章主要介绍下Python 3.1的计算操作符和类型,以下是我所作的一个Sample:
run python,Console输出:
'''
Created on 2010-3-31
@author: Jamson Huang
'''
#calcu flag
#definiton function
#def testCalcuFunction():
if __name__ == '__main__':
#这里,/和//与Python 2.6相反
# 传统除法/
a = 3.2
b = 1.8
print('a/b:', a/b)
#浮点除//
a=3.2
b=1.8
print('a//b:', a//b)
#地板除/
a=5
b=2
print('a//b:', a//b)
#求余
print('a%b:', a%b)
#**乘方操作符
a=4.1
b=2
print('a**b:', a**b)
print('b**a:', b**a)
print(-3**2+4%3+5//3)
#字符串的处理=>索引操作符[]和切片操作符[:],+,^
tempStr = 'abcdefg'
print('[tempStr]:', tempStr[2])
print('tempstr[:]:', tempStr[:3])
print('tempstr[:]:', tempStr[2:4])
tempStr1 = 'h'
print('tempStr+tempStr1:', tempStr+tempStr1)
print('tempStr1*2:', tempStr1*2)
#list and array
tempList = ['abcd','defg', 'mjnd']
print('tempList[2]:',tempList[2])
print('tempList[:2]', tempList[:2])
tempList[2] = 'hjkm'
print('tempList[2]:',tempList[2])
tempArray = ('abcd','defg', 'mjnd')
print('tempArray[2]:', tempArray[2])
print('tempArray[:2]', tempArray[:2])
#dictionary use = hashmap(java)
tempDict = {'chinese':'jamson'}
tempDict['Hubei']='XianNing'
print('tempDict:',tempDict)
print('tempDict.keys():', tempDict.keys())
print('tempDict[Hubei]:', tempDict['Hubei'])
for key in tempDict:
print('key:tempDict[Key]',key,tempDict[key])
run python,Console输出:
a/b: 1.77777777778
a//b: 1.0
a//b: 2
a%b: 1
a**b: 16.81
b**a: 17.1483754006
-7
[tempStr]: c
tempstr[:]: abc
tempstr[:]: cd
tempStr+tempStr1: abcdefgh
tempStr1*2: hh
tempList[2]: mjnd
tempList[:2] ['abcd', 'defg']
tempList[2]: hjkm
tempArray[2]: mjnd
tempArray[:2] ('abcd', 'defg')
tempDict: {'Hubei': 'XianNing', 'chinese': 'jamson'}
tempDict.keys(): dict_keys(['Hubei', 'chinese'])
tempDict[Hubei]: XianNing
key:tempDict[Key] Hubei XianNing
key:tempDict[Key] chinese jamson