Python学習シリーズ(二)


この記事では主にPython 3.1の計算オペレータとタイプを紹介します.以下は私が作ったSampleです.
'''
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