Chapter two, Python the operator
Simple is better than complex. ——The Zen of Python
Operators are special symbols, mainly used for mathematical calculations, logic operations, and compare the size. The Python operators include arithmetic operators, assignment operators, relational operators comparison, logical operators . Expression using operators to connect different types of data together according to certain rules, called expression . For example, using the arithmetic operators connected equation called arithmetic expressions, using the equation connecting the logical operator, a logical expression is called.
First, arithmetic operators
Arithmetic operators deal four signed operation , in most applications of digital processing.
Operators | Explanation | Examples | result |
+ | plus | 220+284 | 504 |
- | Less | 1210-1184 | 26 |
* | Multiply | 220*284 | 62480 |
/ | except | 1210/1184 | 1.0219594594594594 |
% | Remainder returns the remainder of the division | 1210%284 | 74 |
// | Take divisible supplier returns the integer part | 1210//220 | 5 |
** | Power returns to the power of y to x | 2**3 | 8 |
When using the "%" in arithmetic operators remainder calculation, if the divisor is negative, then the result obtained is a negative number. When using the division operator and the remainder operator, the divisor is not 0, or will be error. In Python, arithmetic operators can be direct digital calculation, operator precedence and Mathematics and the priorities are the same.
Further in Python, operator * may also be used in the string, the string is the result of the calculation results of the specified number of times, for example:
In [1]: print('Love'*10) LoveLoveLoveLoveLoveLoveLoveLoveLoveLov
Second, the assignment operator
Assignment operator primarily used to assign values to variables. Can be directly "=" substantially right value using the assignment operator assigned to a variable on the left, may be performed after some calculation variable value is assigned to the left of the assignment operator used is as follows:
Operators | Explanation | For example | Expanded form |
= | Simple assignment operator | x = y | x = y |
+= | Addition assignment | x + = y | x=x+y |
-= | Subtraction assignment | x- = y | x=x-y |
*= | Multiplication assignment | x * = y | x=x*y |
/= | Division assignment | x / = y | x=x/y |
%= | Take the remainder assignment | x% = y | x=x%y |
**= | Power assignment | x ** = y | x=x**y |
//= | Rounding assignment | // x = y | x=x//y |
Note: The "=" and "==" It's easy to confuse the two operators, the former is just talked about the assignment operator "==" is the comparison operator.
Third, the comparison operator
Comparison operators, also known as relational operators. The results for the variable expression or expression will be the size, true and false comparison, if the comparison is true, it returns True, if false, False is returned. Comparison operators commonly used in conditional statements as basis for judgment. Python common comparison operators as follows:
Operators | effect | Examples | result |
> | more than the | 220>284 | Falae |
< | Less than | 220<284 | True |
== | equal | 1210==1210 | True |
!= | not equal to | 220!=220 | False |
>= | greater or equal to | 220>=284 | False |
<= | Less than or equal | 220<=284 | True |
In Python, when a need to determine whether the value is between two values, may take the form "Value 1 <variable <value 2", for example:
In [1]: a=26 In [2]: 0<26<30 Out[2]: True
四、逻辑运算符
逻辑运算符是对真和假两种布尔值进行运算,然后运算的结果仍是一个布尔值,Python中的逻辑运算主要包含and(逻辑与)、or(逻辑或)、not(逻辑非)。赋值运算符说明如下:
运算符 | 含义 | 用法 | 结合方向 |
and | 逻辑与 | eg1 and eg2 | 从左到右 |
or | 逻辑或 | eg1 or eg2 | 从左到右 |
not | 逻辑非 | not eg | 从右到左 |
常用的逻辑运算符运算结果:
表达式1 | 表达式2 | 表达式1 and 表达式2 | 表达式1 or 表达式2 | not 表达式1 |
True | True | True | True | False |
True | False | False | True | False |
False | False | False | False | True |
False | True | False | True | True |
五、位运算符
位运算符是把数字看做二进制数来进行计算的,因此,需要先将要执行的数据转化为你二进制,然后才能执行计算。Python中的位运算符有位与(&)、位或(|)、位异或(^)、取反(~)、左移位(<<)和右位移(>>)运算符。
1. “位与(&)”运算
“位与”运算的运算符为“&”,运算法则:参与运算的两个数字都用二进制表示,只有对应为都是1时,结果位才是1,否则为0.如果两个参与运算的数字精度不同,则结果的精度与精度高的数字相同。
2. “位或(|)”运算
“位或”运算的运算符为“|”,运算法则:参与运算的两个数字都用二进制表示,只有对应为都是0时,结果位才是0,否则为1.如果两个参与运算的数字精度不同,则结果的精度与精度高的数字相同。
3.“位异或(^)”运算
“位异或”运算的运算符为“^”,运算法则:参与运算的两个数字都用二进制表示,当两个二进制位相同(同时为0或同时为1)时,结果为0,否则为1.如果两个参与运算的数字精度不同,则结果的精度与精度高的数字相同。
4. “位取反”运算
“位取反”运算也称“位非”运算,运算符“~”,运算法则:就是讲操作数中对应的二进制位数1修改为0,0修改为1.
5.左移位运算符 <<
左移位运算符<<是将一个二进制数,向左移动指定的位数,左边(高位端)溢出的位被丢弃,右边(低位端)的空位用0补充,左移位运算相当于乘以2的n次幂.
6.右移位运算>>
右移位运算符>>是将一个二进制数向右移动指定的位数,右边(低位端)溢出的位被丢弃,而在填充左边(高位端)的空位时,如果最高位是0(正数),左侧空位填入0;如果最高位是1(负数),左侧空位填入1。右移位运算相当于除以2的n次幂.
1 a = 60 # 60 = 0011 1100
2 b = 13 # 13 = 0000 1101
3 c = 0
4 c = a & b; # 12 = 0000 1100
5 print("1 - c 的值为:", c)
6 c = a | b; # 61 = 0011 1101
7 print("2 - c 的值为:", c)
8 c = a ^ b; # 49 = 0011 0001
9 print("3 - c 的值为:", c)
10 c = ~a; # -61 = 1100 0011
11 print("4 - c 的值为:", c)
12 c = a << 2; # 240 = 1111 0000
13 print("5 - c 的值为:", c)
14 c = a >> 2; # 15 = 0000 1111
15 print("6 - c 的值为:", c)
16 输出:
17 1 - c 的值为: 12
18 2 - c 的值为: 61
19 3 - c 的值为: 49
20 4 - c 的值为: -61
21 5 - c 的值为: 240
22 6 - c 的值为: 15
六、成员运算符
Python支持成员运算符,判断某值是否包含在指定序列中。
in 运算符:如果在指定序列中找到值返回True,否则返回False。eg: x在y序列中,如果x在y序列中返回True.
not in 运算符:如果在指定序列中没有找到返回值,返回True,否则返回False。 eg:x不在y序列中,如果x不在y序列中,则返回True.示例如下:
1 a = 10
2 b = 20
3 list = [1, 2, 3, 4, 5];
4 if (a in list):
5 print("变量 a 在给定的列表中 list 中")
6 else:
7 print("变量 a 不在给定的列表中 list 中")
8 if (b not in list):
9 print("变量 b 不在给定的列表中 list 中")
10 else:
11 print("变量 b 在给定的列表中 list 中")
12 # 修改变量 a 的值
13 a = 2
14 if (a in list):
15 print("变量 a 在给定的列表中 list 中")
16 else:
17 print("变量 a 不在给定的列表中 list 中")
18 输出:
19 变量 a 不在给定的列表中 list 中
20 变量 b 不在给定的列表中 list 中
21 变量 a 在给定的列表中 list 中
七、身份运算符
身份运算符用于比较两个对象的存储单元.
is 运算符: 判断两个标识符是否引用自同一个对象,eg: x is y,类似id(x)==id(y),如果引用的是同一个对象则返回True,否则返回 False.
is not 运算符: 判断两个标识符是否引用自不同对象,eg: x is not y,类似id(x)!=id(y),如果引用的不是同一个对象则返回True,否则返回 False.
1 a = 20;b = 20 2 if (a is b): 3 print("1 - a 和 b 有相同的标识") 4 else: 5 print("1 - a 和 b 没有相同的标识") 6 if (id(a) == id(b)): 7 print("2 - a 和 b 有相同的标识") 8 else: 9 print("2 - a 和 b 没有相同的标识") 10 # 修改变量 b 的值 11 b = 30 12 if (a is b): 13 print("3 - a 和 b 有相同的标识") 14 else: 15 print("3 - a 和 b 没有相同的标识") 16 17 if (a is not b): 18 print("4 - a 和 b 没有相同的标识") 19 else: 20 print("4 - a 和 b 有相同的标识") 21 输出: 22 1 - a 和 b 有相同的标识 23 2 - a 和 b 有相同的标识 24 3 - a 和 b 没有相同的标识 25 4 - a 和 b 没有相同的标识
is 与 == 区别:is 用于判断两个变量引用对象是否为同一个, == 用于判断引用变量的值是否相等。
>>> a = [1,2,3] >>> b=a >>> b is a True >>> b==a True >>> b=a[:] >>> b is a False >>> b == a True
八、运算符的优先级
如下表所示,该表格列出了从最高到最低优先级的所有运算符:
运算符 | 描述 |
---|---|
** | 指数 (最高优先级) |
~ + - | 按位翻转, 一元加号和减号 (最后两个的方法名为 +@ 和 -@) |
* / % // | 乘,除,取模和取整除 |
+ - | 加法减法 |
>> << | 右移,左移运算符 |
& | 位 'AND' |
^ | | 位运算符 |
<= < > >= | 比较运算符 |
<> == != | 等于运算符 |
= %= /= //= -= += *= **= | 赋值运算符 |
is is not | 身份运算符 |
in not in | 成员运算符 |
and or not | 逻辑运算 |
在实际开发中,如果搞不清楚优先级可以使用括号来确保运算的执行顺序.