zhangbinmy 发表于 2017-4-25 06:45:10

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)
print('tempstr[:]:', tempStr[:3])
print('tempstr[:]:', tempStr)
tempStr1 = 'h'
print('tempStr+tempStr1:', tempStr+tempStr1)
print('tempStr1*2:', tempStr1*2)
#list and array
tempList = ['abcd','defg', 'mjnd']
print('tempList:',tempList)   
print('tempList[:2]', tempList[:2])
tempList = 'hjkm'
print('tempList:',tempList)   
tempArray = ('abcd','defg', 'mjnd')
print('tempArray:', tempArray)   
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:', tempDict['Hubei'])
for key in tempDict:
print('key:tempDict',key,tempDict)
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
: c
tempstr[:]: abc
tempstr[:]: cd
tempStr+tempStr1: abcdefgh
tempStr1*2: hh
tempList: mjnd
tempList[:2] ['abcd', 'defg']
tempList: hjkm
tempArray: mjnd
tempArray[:2] ('abcd', 'defg')
tempDict: {'Hubei': 'XianNing', 'chinese': 'jamson'}
tempDict.keys(): dict_keys(['Hubei', 'chinese'])
tempDict: XianNing
key:tempDict Hubei XianNing
key:tempDict chinese jamson
页: [1]
查看完整版本: Python 学习系列(二)