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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值