📜  Python运算符

📅  最后修改于: 2020-10-24 08:52:59             🧑  作者: Mango

Python运算符

可以将运算符定义为负责两个操作数之间特定操作的符号。运算符是程序的基础,使用特定的编程语言在其上构建逻辑。 Python提供了各种运算符,其描述如下。

  • 算术运算运算符
  • 运算符
  • 赋值运算符
  • 逻辑运算符
  • 按位运算符
  • 会员运营商
  • 身份运算符

算术运算符

算术运算运算符用于在两个操作数之间执行算术运算。它包括+(加法),-(减法),*(乘法),/(除法),%(提醒),//(底数除法)和指数(**)运算符。

请考虑下表,以详细了解算术运算运算符。

Operator Description
+ (Addition) It is used to add two operands. For example, if a = 20, b = 10 => a+b = 30
– (Subtraction) It is used to subtract the second operand from the first operand. If the first operand is less than the second operand, the value results negative. For example, if a = 20, b = 10 => a – b = 10
/ (divide) It returns the quotient after dividing the first operand by the second operand. For example, if a = 20, b = 10 => a/b = 2.0
* (Multiplication) It is used to multiply one operand with the other. For example, if a = 20, b = 10 => a * b = 200
% (reminder) It returns the reminder after dividing the first operand by the second operand. For example, if a = 20, b = 10 => a%b = 0
** (Exponent) It is an exponent operator represented as it calculates the first operand power to the second operand.
// (Floor division) It gives the floor value of the quotient produced by dividing the two operands.

运算符

运算符用于比较两个操作数的值,并相应地返回布尔值true或false。运算符在下表中进行了描述。

Operator Description
== If the value of two operands is equal, then the condition becomes true.
!= If the value of two operands is not equal, then the condition becomes true.
<= If the first operand is less than or equal to the second operand, then the condition becomes true.
>= If the first operand is greater than or equal to the second operand, then the condition becomes true.
> If the first operand is greater than the second operand, then the condition becomes true.
< If the first operand is less than the second operand, then the condition becomes true.

赋值运算符

赋值运算符用于将右表达式的值赋给左操作数。下表描述了赋值运算符。

Operator Description
= It assigns the value of the right expression to the left operand.
+= It increases the value of the left operand by the value of the right operand and assigns the modified value back to left operand. For example, if a = 10, b = 20 => a+ = b will be equal to a = a+ b and therefore, a = 30.
-= It decreases the value of the left operand by the value of the right operand and assigns the modified value back to left operand. For example, if a = 20, b = 10 => a- = b will be equal to a = a- b and therefore, a = 10.
*= It multiplies the value of the left operand by the value of the right operand and assigns the modified value back to then the left operand. For example, if a = 10, b = 20 => a* = b will be equal to a = a* b and therefore, a = 200.
%= It divides the value of the left operand by the value of the right operand and assigns the reminder back to the left operand. For example, if a = 20, b = 10 => a % = b will be equal to a = a % b and therefore, a = 0.
**= a**=b will be equal to a=a**b, for example, if a = 4, b =2, a**=b will assign 4**2 = 16 to a.
//= A//=b will be equal to a = a// b, for example, if a = 4, b = 3, a//=b will assign 4//3 = 1 to a.

按位运算符

按位运算运算符对两个操作数的值进行逐位运算。考虑以下示例。

例如,

if a = 7 
   b = 6   
then, binary (a) = 0111  
    binary (b) = 0011  
  
hence, a & b = 0011  
      a | b = 0111  
             a ^ b = 0100  
       ~ a = 1000
Operator Description
& (binary and) If both the bits at the same place in two operands are 1, then 1 is copied to the result. Otherwise, 0 is copied.
| (binary or) The resulting bit will be 0 if both the bits are zero; otherwise, the resulting bit will be 1.
^ (binary xor) The resulting bit will be 1 if both the bits are different; otherwise, the resulting bit will be 0.
~ (negation) It calculates the negation of each bit of the operand, i.e., if the bit is 0, the resulting bit will be 1 and vice versa.
<< (left shift) The left operand value is moved left by the number of bits present in the right operand.
>> (right shift) The left operand is moved right by the number of bits present in the right operand.

逻辑运算符

逻辑运算符主要用于表达式评估中以做出决策。 Python支持以下逻辑运算符。

Operator Description
and If both the expression are true, then the condition will be true. If a and b are the two expressions, a → true, b → true => a and b → true.
or If one of the expressions is true, then the condition will be true. If a and b are the two expressions, a → true, b → false => a or b → true.
not If an expression a is true, then not (a) will be false and vice versa.

会员运营商

Python成员运算符用于检查Python数据结构中值的成员资格。如果该值存在于数据结构中,则结果值为true,否则返回false。

Operator Description
in It is evaluated to be true if the first operand is found in the second operand (list, tuple, or dictionary).
not in It is evaluated to be true if the first operand is not found in the second operand (list, tuple, or dictionary).

身份运算符

身份运算符用于确定元素是特定的类还是类型。

Operator Description
is It is evaluated to be true if the reference present at both sides point to the same object.
is not It is evaluated to be true if the reference present at both sides do not point to the same object.

运算符优先级

找出运算符的优先级至关重要,因为它使我们知道应该首先评估哪个运算符。下面给出了Python运算符的优先级表。

Operator Description
** The exponent operator is given priority over all the others used in the expression.
~ + – The negation, unary plus, and minus.
* / % // The multiplication, divide, modules, reminder, and floor division.
+ – Binary plus, and minus
>> << Left shift. and right shift
& Binary and.
^ | Binary xor, and or
<= < > >= Comparison operators (less than, less than equal to, greater than, greater then equal to).
<> == != Equality operators.
= %= /= //= -= +=
*= **=
Assignment operators
is is not Identity operators
in not in Membership operators
not or and Logical operators