所谓操作运算符,其实就是加减乘除之类的,可以执行某种运算或者是某项操作的符号,例如:4 + 5这个式子中的4和5就是操作数,它们中间的 + 就是加法运算符,加法运算符可以将左右两个操作数相加,并得到相加的结果...
[email protected]:~/Pytest/ch5$ python Python 2.7.8 (default, Dec 10 2014, 22:05:07) [GCC 4.5.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> 4+5 9 [40594 refs] >>> quit() [18355 refs] [email protected]:~/Pytest/ch5$ |
[email protected]:~/Downloads$ cd Python-2.7.8 [email protected]:~/Downloads/Python-2.7.8$ make clean [email protected]:~/Downloads/Python-2.7.8$ ./configure --with-pydebug [email protected]:~/Downloads/Python-2.7.8$ make [email protected]:~/Downloads/Python-2.7.8$ sudo make install [sudo] password for zengl: [email protected]:~/Downloads/Python-2.7.8$ cd /home/zengl/Pytest/ch5/ [email protected]:~/Pytest/ch5$ gdb python ....................................... Reading symbols from /usr/local/bin/python...done. (gdb) set args test.py (gdb) show args Argument list to give program being debugged when it is started is "test.py". (gdb) b main Breakpoint 1 at 0x8058e5d: file ./Modules/python.c, line 23. (gdb) r Starting program: /usr/local/bin/python test.py [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/i386-linux-gnu/libthread_db.so.1". Breakpoint 1, main (argc=2, argv=0xbffff284) at ./Modules/python.c:23 23 return Py_Main(argc, argv); (gdb) p argv[1] $1 = 0xbffff445 "test.py" (gdb) p argv[0] $2 = 0xbffff42f "/usr/local/bin/python" (gdb) c Continuing. Line 1 - Value of c is 12 Line 2 - Value of c is 61 Line 3 - Value of c is 49 Line 4 - Value of c is -61 Line 5 - Value of c is 240 Line 6 - Value of c is 15 [18354 refs] [Inferior 1 (process 2597) exited normally] (gdb) q [email protected]:~/Pytest/ch5$ |
Operator 运算符 |
Description 描述 |
---|---|
+ | 加法运算符,将左右两个操作数相加,并返回相加的结果。 |
- | 减法运算符,用左侧操作数减去右侧操作数,并返回相减的结果。 |
* | 乘法运算符,将左右两个操作数相乘,并返回乘法运算的结果。 |
/ |
除法运算符,用左侧的操作数除以右侧的操作数。 需要注意的是:Python 2.x与Python 3.x中的除法运算的结果,在某些情况下是不一样的, 尤其是当两个整数相除时,下面会进行介绍。 |
% | 取余运算符,用左侧操作数除以右侧操作数,并返回除运算后的余数,例如:3%4 得到的结果就会是余数3 。 |
** | 指数运算符,对操作数进行指数运算,例如:2**4 就是计算2的4次方,那么结果就是16 。 |
// |
舍入式除法运算符,它与上面提到的除法运算符的区别在于:无论除运算的结果如何,它始终会将小数部分给舍去掉, 只保留整数部分的值。例如5.0/2会得到2.5,但是5.0//2得到的就会是2.0,小数部分会被舍弃掉。 |
[email protected]:~/Pytest/ch5$ python Python 2.7.8 (default, Dec 10 2014, 22:05:07) [GCC 4.5.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> 1+2 3 >>> 3-1 2 >>> 2*3 6 >>> quit() [email protected]:~/Pytest/ch5$ |
[email protected]:~/Pytest/ch5$ python Python 2.7.8 (default, Dec 10 2014, 22:05:07) [GCC 4.5.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> 5/2 2 >>> 5.0/2 2.5 >>> quit() [email protected]:~/Pytest/ch5$ |
[email protected]:~/Pytest/ch5$ python3.2 Python 3.2.3 (default, Apr 10 2013, 05:29:11) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> 5/2 2.5 >>> 5.0/2 2.5 >>> 4/2 2.0 >>> quit() [email protected]:~/Pytest/ch5$ |
[email protected]:~/Pytest/ch5$ python Python 2.7.8 (default, Dec 10 2014, 22:05:07) [GCC 4.5.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> 5%2 1 >>> 6%7 6 >>> 2**3 8 >>> 2**4 16 >>> 3**2 9 >>> quit() [email protected]:~/Pytest/ch5$ |
[email protected]:~/Pytest/ch5$ python Python 2.7.8 (default, Dec 10 2014, 22:05:07) [GCC 4.5.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> 5//2 2 >>> 5.0//2 2.0 >>> 4//2 2 >>> 4.0//2 2.0 >>> quit() [email protected]:~/Pytest/ch5$ python3.2 Python 3.2.3 (default, Apr 10 2013, 05:29:11) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> 5//2 2 >>> 5.0//2 2.0 >>> 4//2 2 >>> 4.0//2 2.0 >>> quit() [email protected]:~/Pytest/ch5$ |
[email protected]:~/Pytest/ch5$ python Python 2.7.8 (default, Dec 10 2014, 22:05:07) [GCC 4.5.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> id(123) 148881756 >>> type(123) <type 'int'> >>> quit() [email protected]:~/Pytest/ch5$ |
[email protected]:~/Pytest/ch5$ python Python 2.7.8 (default, Dec 10 2014, 22:05:07) [GCC 4.5.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> id(123) 151097692 >>> a = b = 123 >>> id(a) 151097692 >>> id(b) 151097692 >>> a 123 >>> b 123 >>> quit() [email protected]:~/Pytest/ch5$ |
[email protected]:~/Pytest/ch5$ python Python 2.7.8 (default, Dec 10 2014, 22:05:07) [GCC 4.5.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> id(True) 136288896 >>> type(True) <type 'bool'> >>> id(False) 136288876 >>> type(False) <type 'bool'> >>> quit() [email protected]:~/Pytest/ch5$ |
[email protected]:~/Pytest/ch5$ python Python 2.7.8 (default, Dec 10 2014, 22:05:07) [GCC 4.5.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> if(True): ... print 'yes' ... else: ... print 'no' ... yes >>> if(False): ... print 'yes' ... else: ... print 'no' ... no >>> quit() [email protected]:~/Pytest/ch5$ |
Operator 运算符 |
Description 描述 |
---|---|
== | 等于运算符,用于判断左右两个操作数的值是否相等,当相等时返回True,当不相等时则返回False |
!= | 不等于运算符,用于判断左右两个操作数的值是否不相等,当不相等时则返回True,当相等时则返回False |
<> | 也是不相等运算符,当两操作数不相等时返回True,相等时返回False,类似于上面的 != 运算符 |
> | 大于运算符,当左操作数的值大于右操作数的值时,则返回True,否则,返回False |
< | 小于运算符,当左操作数的值小于右操作数的值时,则返回True,否则,返回False |
>= | 大于等于运算符,当左操作数的值大于或等于右操作数的值时,则返回True,否则,返回False |
<= | 小于等于运算符,当左操作数的值小于或等于右操作数的值时,则返回True,否则,返回False |
[email protected]:~/Pytest/ch5$ python Python 2.7.8 (default, Dec 10 2014, 22:05:07) [GCC 4.5.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> 2 == 2 True >>> 2 != 2 False >>> 3 <> 2 True >>> 10 > 5 True >>> 5 < 10 True >>> 5 >= 10 False >>> 5 <= 10 True >>> quit() [email protected]:~/Pytest/ch5$ |
Operator 运算符 |
Description 描述 |
---|---|
= | 最基本的赋值运算符,用于将右侧表达式的值赋值给左侧的变量。 |
+= |
加赋值运算符,将左右两侧操作数相加,并将相加的结果赋值给左侧操作数。 例如:c += a 等效于 c = c + a |
-= |
减赋值运算符,将左右两侧操作数相减,并将结果赋值给左侧操作数。 例如:c -= a 等效于 c = c - a |
*= |
乘赋值运算符,将左右两侧操作数相乘,并将结果赋值给左侧操作数。 例如:c *= a 等效于 c = c * a |
/= |
除赋值运算符,将左右两操作数相除,并将结果赋值给左侧操作数。 例如:c /= a 等效于 c = c / a |
%= |
取余赋值运算符,对左右两操作数执行取余运算,并将结果赋值给左侧操作数。 例如:c %= a 等效于 c = c % a |
**= |
指数赋值运算符,对左右两操作数执行指数运算,并将结果赋值给左侧操作数。 例如:c **= a 等效于 c = c ** a |
//= |
舍入除赋值运算符,对左右两操作数执行舍入式除法运算,并将结果赋值给左侧操作数。 例如:c //= a 等效于 c = c // a |
[email protected]:~/Pytest/ch5$ python Python 2.7.8 (default, Dec 10 2014, 22:05:07) [GCC 4.5.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> a = 3 >>> a *= 1 + 2 >>> a 9 >>> quit() [email protected]:~/Pytest/ch5$ |
[email protected]:~/Pytest/ch5$ python Python 2.7.8 (default, Dec 10 2014, 22:05:07) [GCC 4.5.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> c = 3 >>> c 3 >>> c += 4 >>> c 7 >>> c -= 4 >>> c 3 >>> c *= 3 >>> c 9 >>> c /= 3 >>> c 3 >>> c %= 7 >>> c 3 >>> c **= 3 >>> c 27 >>> c //= 3 >>> c 9 >>> quit() [email protected]:~/Pytest/ch5$ |
[email protected]:~/Pytest/ch5$ python Python 2.7.8 (default, Dec 10 2014, 22:05:07) [GCC 4.5.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> '{0:b}'.format(42) '101010' >>> bin(42) '0b101010' >>> quit() [email protected]:~/Pytest/ch5$ |
Operator 运算符 |
Description 描述 |
---|---|
& |
按位与运算符,对左右两操作数的所有二进制位都进行按位与运算,即两操作数的相应二进制位上都为1时,结果才为1,但只要有一方为0时,结果就会是0 例如:0b111100 & 0b001101 得到的结果是:001100 |
| |
按位或运算符,对左右两操作数的所有二进制位都进行按位或运算,即两操作数的相应二进制位上只要有一方为1,那么结果就会是1,当双方都为0时,结果才会是0 例如:0b111100 | 0b001101 得到的结果是:111101 |
^ |
按位异或运算符,对左右两操作数的所有二进制位都进行按位异或运算,即相应二进制位上相同则为0,不同则为1 例如:0b111100 ^ 0b001101 得到的结果是:110001 |
~ |
按位取反运算符,将操作数的所有二进制位都进行反转,即0变1,1变0 例如:~0b00111100 得到的结果就是:11000011 即 -61 |
<< |
按位左移运算符,将左侧操作数的所有二进制位根据右操作数的值,进行左移,在左移后,低位用0填充 例如:0b111100 << 2 即111100左移两位,得到的结果就会是:11110000 |
>> |
按位右移运算符,将左侧操作数的所有二进制位根据右侧操作数的值,进行右移,在右移后,高位用符号位填充,即如果原来是正数,则用0填充,如果原来是负数,则用1填充,这样就可以保证右移后,整数的正负符号性质不变,因此,python的右移操作属于算术右移。 例如:0b111100 >> 2 即111100右移两位,得到的结果就会是:001111 |
[email protected]:~/Pytest/ch5$ python Python 2.7.8 (default, Dec 10 2014, 22:05:07) [GCC 4.5.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> '{:06b}'.format(0b111100 & 0b001101) '001100' >>> '{:06b}'.format(0b111100 | 0b001101) '111101' >>> '{:06b}'.format(0b111100 ^ 0b001101) '110001' >>> ~0b00111100 -61 >>> '{:08b}'.format(0b111100 << 2) '11110000' >>> '{:06b}'.format(0b111100 >> 2) '001111' >>> quit() [email protected]:~/Pytest/ch5$ |
[email protected]:~/Pytest/ch5$ python Python 2.7.8 (default, Dec 10 2014, 22:05:07) [GCC 4.5.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> {123, 'john'} | {456, 'hello'} set([456, 'john', 123, 'hello']) >>> {123, 'john'} & {123, 456} set([123]) >>> quit() [email protected]:~/Pytest/ch5$ |
Operator 运算符 |
Description 描述 |
---|---|
and |
逻辑与运算符,当左右两操作数都为True时,则返回True,只要有一个操作数为False,则返回False 例1:True and True 结果是:True 例2:True and False 结果是:False 例3:False and True 结果是:False 例4:False and False 结果是:False and的左右两操作数还可以是数字,当为数字时,0等效于False,非0的数则等效于True,只不过此时返回的将是一个数字(当操作数中有布尔对象时,也可能返回布尔对象),具体例子在下面会给出。 |
or |
逻辑或运算符,当左右两操作数中有一个为True时,结果就会是True,当两操作数都为False时,才返回False 例1:True or False 结果是:True 例2:False or False 结果是:False 与and类似的是,or的左右两操作数也都可以是数字,同样是0等效于False,非0的数等效于True,当为数字时,返回的将是一个数字(当操作数中有布尔对象时,也可能返回布尔对象)。 |
not |
逻辑非运算符,反转布尔对象的值,即True变False,False变True 例1:not True 结果是:False 例2:not False 结果是:True 如果not后面接的是数字,那么0就等效于False,而非0的数则等效于True 例3:not 0 结果是:True 例4:not 123 结果是:False |
[email protected]:~/Pytest/ch5$ python Python 2.7.8 (default, Dec 10 2014, 22:05:07) [GCC 4.5.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> True and True True >>> True and False False >>> False and True False >>> False and True False >>> False and False False >>> 123 and 0 0 >>> 0 and 123 0 >>> 0 and 0 0 >>> 123 and 234 234 >>> 123 and True True >>> True and 123 123 >>> 123.0 and 123 123 >>> 123 and 123.0 123.0 >>> True and 123.0 123.0 >>> False and 123.0 False >>> False and 0 False >>> 0 and False 0 >>> quit() [email protected]:~/Pytest/ch5$ |
[email protected]:~/Pytest/ch5$ python Python 2.7.8 (default, Dec 10 2014, 22:05:07) [GCC 4.5.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> True or False True >>> 123 or True 123 >>> False or True True >>> 0 or 456 456 >>> 0 or False False >>> False or 0 0 >>> quit() [email protected]:~/Pytest/ch5$ |
[email protected]:~/Pytest/ch5$ python Python 2.7.8 (default, Dec 10 2014, 22:05:07) [GCC 4.5.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> not True False >>> not False True >>> not 0 True >>> not 123 False >>> not 123.5 False >>> not '' True >>> not 'a' False >>> quit() [email protected]:~/Pytest/ch5$ |
Operator 运算符 |
Description 描述 |
---|---|
in |
如果左侧操作数存在于右侧操作数中,那么就返回True,否则返回False 例如:2 in [1,2,3] 得到的结果就是:True,因为可以在[1,2,3]的列表中找到2,因此2是该列表的成员 |
not in |
如果左侧操作数不存在于右侧操作数中,就返回True,否则返回False 例如:4 not in [1,2,3] 得到的结果就是:True,因为在[1,2,3]列表中找不到4这个数,因此4不是该列表的成员 |
[email protected]:~/Pytest/ch5$ python Python 2.7.8 (default, Dec 10 2014, 22:05:07) [GCC 4.5.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> 2 in [1,2,3] True >>> 4 not in [1,2,3] True >>> 'a' in 'abcd' True >>> 'e' not in 'abcd' True >>> 'e' in 'abcd' False >>> (1,3) in [(1,3),123] True >>> quit() [email protected]:~/Pytest/ch5$ |
Operator 运算符 |
Description 描述 |
---|---|
is | 当左右两操作数所属对象的标识值相等时,即它们属于同一个对象时,就返回True,否则,返回False |
is not | 当左右两操作数所属对象的标识值不相等时,返回True,否则,返回False |
[email protected]:~/Pytest/ch5$ python Python 2.7.8 (default, Dec 10 2014, 22:05:07) [GCC 4.5.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> a = b = 123 >>> a is b True >>> id(a) == id(b) True >>> a is not 123.5 True >>> id(a) != id(123.5) True >>> a is 123.5 False >>> id(a) == id(123.5) False >>> quit() [email protected]:~/Pytest/ch5$ |
Operator 运算符 |
Description 描述 |
---|---|
** | 指数运算符 |
~ + - | 取反运算符,正号与负号运算符,这里的+和-表示正号或负号,而不是加减运算符 |
* / % // | 乘、除、取余、舍入除运算符 |
+ - | 加、减运算符 |
>> << | 按位右移和按位左移运算符 |
& | 按位与运算符 |
^ | | 按位异或、按位或运算符 |
<= < > >= | 小于等于、小于、大于、大于等于运算符 |
<> == != | 等于、不等于运算符 |
= %= /= //= -= += *= **= | 加赋值,减赋值之类的赋值运算符 |
is is not | is和is not的对象标识运算符 |
in not in | in和not in的成员运算符 |
not or and | 逻辑取反、逻辑或、逻辑与运算符 |
#test.py a = 20 b = 10 c = 15 d = 5 e = 0 e = (a + b) * c / d #( 30 * 15 ) / 5 print "Value of (a + b) * c / d is ", e e = ((a + b) * c) / d # (30 * 15 ) / 5 print "Value of ((a + b) * c) / d is ", e e = (a + b) * (c / d); # (30) * (15/5) print "Value of (a + b) * (c / d) is ", e e = a + (b * c) / d; # 20 + (150/5) print "Value of a + (b * c) / d is ", e |
[email protected]:~/Pytest/ch5$ python test.py Value of (a + b) * c / d is 90 Value of ((a + b) * c) / d is 90 Value of (a + b) * (c / d) is 90 Value of a + (b * c) / d is 50 [email protected]:~/Pytest/ch5$ |