operator
Target
- arithmetic operators
- Comparison (relational) operators
- Logical Operators
- assignment operator
- operator precedence
Mathematical Symbol Table link: https://en.wikipedia.org/wiki/Mathematical Symbol Table
01. Arithmetic operators
- It is a symbol used to complete basic arithmetic operations and is used to process four arithmetic operations
operator | describe | example |
---|---|---|
+ | add | 10 + 20 = 30 |
- | reduce | 10 - 20 = -10 |
* | take | 10 * 20 = 200 |
/ | remove | 10 / 20 = 0.5 |
// | Divide | Returns the integer part (quotient) of the division 9 // 2 outputs the result 4 |
% | take the remainder | Returns the remainder of division 9 % 2 = 1 |
** | power | Also known as power, power, 2 ** 3 = 8 |
- In Python,
*
operators can also be used on strings, and the result of the calculation is the result of repeating the string a specified number of times.
In [1]: "-" * 50
Out[1]: '----------------------------------------'
02. Comparison (relational) operators
operator | describe |
---|---|
== | Checks whether the values of the two operands are equal , if so, the condition is true and returns True |
!= | Checks if the values of the two operands are not equal , if so, the condition is true and returns True |
> | Check if the value of the left operand is greater than the right operand, if so, the condition is true, return True |
< | Check if the value of the left operand is less than the right operand, if so, the condition is true and return True |
>= | Check if the value of the left operand is greater than or equal to the value of the right operand, if so, the condition is true, return True |
<= | Check if the value of the left operand is less than or equal to the value of the right operand, if so, the condition is true, return True |
In Python 2.x , you can also use the
<>
operator to judge not equal to
!=
In Python 2.x, it can also be used to judge not equal to
03. Logical Operators
operator | logical expression | describe |
---|---|---|
and | x and y | Returns True only if both x and y are True, otherwise returns False as long as either x or y has a value of False |
or | x or y | Returns True as long as either x or y has a value of True, and returns False only if both x and y are False |
not | not x | Returns False if x is True and returns True if x is False |
04. Assignment operator
- In Python, you
=
can assign values to variables using - In arithmetic operations, in order to simplify the writing of code,
Python
a series of assignment operators corresponding to arithmetic operators are also provided - Note: Spaces cannot be used between assignment operators
operator | describe | example |
---|---|---|
= | simple assignment operator | c = a + b assigns the result of the operation of a + b to c |
+= | addition assignment operator | c += a is equivalent to c = c + a |
-= | Subtraction assignment operator | c -= a is equivalent to c = c - a |
*= | multiplication assignment operator | c = a is equivalent to c = c a |
/= | division assignment operator | c /= a is equivalent to c = c / a |
//= | Integer division assignment operator | c //= a is equivalent to c = c // a |
%= | modulo (remainder) assignment operator | c %= a 等效于 c = c % a |
**= | 幂赋值运算符 | c *= a 等效于 c = c * a |
05. 运算符的优先级
- 以下表格的算数优先级由高到最低顺序排列
运算符 | 描述 |
---|---|
** | 幂 (最高优先级) |
* / % // | 乘、除、取余数、取整除 |
+ - | 加法、减法 |
<= < > >= | 比较运算符 |
== != | 等于运算符 |
= %= /= //= -= += *= **= | 赋值运算符 |
not or and | 逻辑运算符 |