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

1、number
数字可以直接在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
 

在进行除法/的时候,如果2个除数都是int型,则返回的值也为整型int:
如果有1个除数为浮点型float,则结果值为浮点型float;
使用运算符//做除法,则返回的值为除后取整
使用%做除法取余数;

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
 

使用**来计算乘方:

In [9]: 5**2
Out[9]: 25
In [10]: 2**7
Out[10]: 128
 

使用=号赋值:

In [11]: width=20
In [12]: height=5*9
In [13]: width * height
Out[13]: 900
 

如果一个变量没有定义,那么直接使用它会出错:

In [14]: n
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-14-fe13119fb084> in <module>()
----> 1 n
NameError: name 'n' is not defined
 

整型和浮点类型的转换:

In [15]: 3*3.75/1.5
Out[15]: 7.5
In [16]: 7.0/2
Out[16]: 3.5
 

可以将最后打印的计算结果直接给“_”符号

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
 

2、String
可以使用单引号”和双引号来直接引用String,反斜杠可以转义引号:

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.'
 

使用print命令可以忽略字符引号,并且可以打印特殊意义符号:

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.
 

如果不想打印特殊字符,可以在第一个引号前面加r:

In [33]: print 'C:\some\name'
C:\some
ame
In [34]: print r'C\some\name'
C\some\name
 

使用三元引号可以将多行放在一行里面,但在第一个引号后面加\符号则还是会使用多行模式:

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
 

String可以使用+号连接或者*号运算

In [40]: 3 * 'un' + 'ium'
Out[40]: 'unununium'
 

连接多个字符串可以使用以下方式:

In [42]: 'Py' 'thon'
Out[42]: 'Python'
 

使用+符号可以将变量与字符串相连接:

In [43]: prefix = 'Py'
In [44]:  prefix + 'thon'
Out[44]: '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.'
 

字符串可以有索引:

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'
 

字符串的切片:

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'
 

字符串切片索引示意:

 +---+---+---+---+---+---+
 | P | y | t | h | o | n |
 +---+---+---+---+---+---+
 0   1   2   3   4   5   6
-6  -5  -4  -3  -2  -1
 

超过索引顺序的切片可以比较友好的处理:

In [68]: word[4:42]
Out[68]: 'on'
In [69]: word[42:]
 

String不能被改变:

In [69]: word[0] = 'J'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-69-197b67ffdd83> in <module>()
----> 1 word[0] = 'J'
TypeError: 'str' object does not support item assignment
 

如果需要不同的字符串,可以生成新的字符串:

In [70]: 'J' + word[1:]
Out[70]: 'Jython'
In [71]: word[:2] + 'py'
Out[71]: 'Pypy'
 

使用内置的len函数可以计算字符串的长度:

In [72]: len(word)
Out[72]: 6
 

来源:http://linjohn.blog.51cto.com/1026193/1608530

You might also like:

Sidebar