Python2.7:使用python作为计算器

1、number
数字可以直接在python运算,使用圆括号分组
[cc lang=”python”]
In [1]: 2+2
Out[1]: 4
In [2]: 50-5*6
Out[2]: 20
In [3]: (50-5.0*6)/4
Out[3]: 5.0
In [4]: 8/5.0
Out[4]: 1.6
[/cc]
在进行除法/的时候,如果2个除数都是int型,则返回的值也为整型int:
如果有1个除数为浮点型float,则结果值为浮点型float;
使用运算符//做除法,则返回的值为除后取整
使用%做除法取余数;
[cc lang=”python”]
In [5]: 17/3
Out[5]: 5
In [6]: 17/3.0
Out[6]: 5.666666666666667
In [7]: 17//3.0
Out[7]: 5.0
In [8]: 17%3
Out[8]: 2
[/cc]
使用**来计算乘方:
[cc lang=”python”]
In [9]: 5**2
Out[9]: 25
In [10]: 2**7
Out[10]: 128
[/cc]
使用=号赋值:
[cc lang=”python”]
In [11]: width=20
In [12]: height=5*9
In [13]: width * height
Out[13]: 900
[/cc]
如果一个变量没有定义,那么直接使用它会出错:
[cc lang=”python”]
In [14]: n
—————————————————————————
NameError Traceback (most recent call last)
in ()
—-> 1 n
NameError: name ‘n’ is not defined
[/cc]
整型和浮点类型的转换:
[cc lang=”python”]
In [15]: 3*3.75/1.5
Out[15]: 7.5
In [16]: 7.0/2
Out[16]: 3.5
[/cc]
可以将最后打印的计算结果直接给“_”符号
[cc lang=”python”]
In [17]: tax=12.5/100
In [18]: price=100.50
In [19]: tax * price
Out[19]: 12.5625
In [20]: price+_
Out[20]: 113.0625
In [21]: round(_,2)
Out[21]: 113.06
[/cc]
2、String
可以使用单引号”和双引号来直接引用String,反斜杠可以转义引号:
[cc lang=”python”]
In [22]: ‘spam eggs’
Out[22]: ‘spam eggs’
In [23]: ‘doesn\’t’
Out[23]: “doesn’t”
In [24]: “doesn’t”
Out[24]: “doesn’t”
In [25]: ‘”Yes,” he said.’
Out[25]: ‘”Yes,” he said.’
In [26]: “\”Yes,\” he said.”
Out[26]: ‘”Yes,” he said.’
In [27]: ‘”Isn\’t,” she said.’
Out[27]: ‘”Isn\’t,” she said.’
[/cc]
使用print命令可以忽略字符引号,并且可以打印特殊意义符号:
[cc lang=”python”]
In [28]: ‘”Isn\’t,” she said.’
Out[28]: ‘”Isn\’t,” she said.’
In [29]: print ‘”Isn\’t,” she said.’
“Isn’t,” she said.
In [30]: s = ‘First line.\nSecond line.’
In [31]: s
Out[31]: ‘First line.\nSecond line.’
In [32]: print s
First line.
Second line.
[/cc]
如果不想打印特殊字符,可以在第一个引号前面加r:
[cc lang=”python”]
In [33]: print ‘C:\some\name’
C:\some
ame
In [34]: print r’C\some\name’
C\some\name
[/cc]
使用三元引号可以将多行放在一行里面,但在第一个引号后面加\符号则还是会使用多行模式:
[cc lang=”python”]
In [38]: print “””\
….: Usage: thingy [OPTIONS]
….: -h Display this usage message
….: -H hostname Hostname to connect to
….: “””
Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname Hostname to connect to
[/cc]
String可以使用+号连接或者*号运算
[cc lang=”python”]
In [40]: 3 * ‘un’ + ‘ium’
Out[40]: ‘unununium’
[/cc]
连接多个字符串可以使用以下方式:
[cc lang=”python”]
In [42]: ‘Py’ ‘thon’
Out[42]: ‘Python’
[/cc]
使用+符号可以将变量与字符串相连接:
[cc lang=”python”]
In [43]: prefix = ‘Py’
In [44]: prefix + ‘thon’
Out[44]: ‘Python’
[/cc]
当需要将比较长的字符串连接在一起的时候,使用引号的方式比较有用:
[cc lang=”python”]
In [45]: text = (‘Put several strings within parentheses ‘
….: ‘to have them joined together.’)
In [46]: text
Out[46]: ‘Put several strings within parentheses to have them joined together.’
[/cc]
字符串可以有索引:
[cc lang=”python”]
In [47]: word = ‘Python’
In [48]: word[0]
Out[48]: ‘P’
In [49]: word[5]
Out[49]: ‘n’
In [50]: word[-1]
Out[50]: ‘n’
In [51]: word[-2]
Out[51]: ‘o’
In [52]: word[-6]
Out[52]: ‘P’
[/cc]
字符串的切片:
[cc lang=”python”]
In [53]: word[0:2]
Out[53]: ‘Py’
In [54]: word[2:5]
Out[54]: ‘tho’
In [55]: word[:2] + word[2:]
Out[55]: ‘Python’
In [56]: word[:4] + word[4:]
Out[56]: ‘Python’
[/cc]
字符串切片索引示意:
[cc lang=”python”]
+—+—+—+—+—+—+
| P | y | t | h | o | n |
+—+—+—+—+—+—+
0 1 2 3 4 5 6
-6 -5 -4 -3 -2 -1
[/cc]
超过索引顺序的切片可以比较友好的处理:
[cc lang=”python”]
In [68]: word[4:42]
Out[68]: ‘on’
In [69]: word[42:]
[/cc]
String不能被改变:
[cc lang=”python”]
In [69]: word[0] = ‘J’
—————————————————————————
TypeError Traceback (most recent call last)
in ()
—-> 1 word[0] = ‘J’
TypeError: ‘str’ object does not support item assignment
[/cc]
如果需要不同的字符串,可以生成新的字符串:
[cc lang=”python”]
In [70]: ‘J’ + word[1:]
Out[70]: ‘Jython’
In [71]: word[:2] + ‘py’
Out[71]: ‘Pypy’
[/cc]
使用内置的len函数可以计算字符串的长度:
[cc lang=”python”]
In [72]: len(word)
Out[72]: 6
[/cc]
来源:http://linjohn.blog.51cto.com/1026193/1608530

Leave a Reply