3. Operator 3.1 arithmetic operator
- Arithmetic operators are one of the operators
- is the symbol used to complete the basic arithmetic operation to handle the arithmetic
Operator |
Describe |
Instance |
+ |
Add |
10 + 20 = 30 |
- |
Reducing |
10-20 = 10 |
* |
By |
10 * 20 = 200 |
/ |
Except |
10/20 = 0.5 |
// |
Take the Divide |
Returns the integer portion of the Division (quotient) 9//2 output result 4 |
% |
Take the remainder |
Returns the remainder of division 9% 2 = 1 |
** |
Power |
Also known as the Second party, the powers, 2 * * 3 = 8 |
- In Python, the * operator can also be used for strings, and the result is the result of repeating a specified number of strings.
In [1]: "-" * 50
OUT[1]: '----------------------------------------'
Precedence of arithmetic operators
- first multiplication , the sibling operator is calculated from left to right , and can be used () to adjust the priority of the calculation
Operator |
Describe |
Priority level |
** |
Power (highest priority) |
High |
* / % // |
Multiply, divide, take remainder, divide evenly |
In |
+ - |
addition, subtraction |
Low |
3.2 Comparison (relational) operators
Operator |
Describe |
== |
Checks whether the values of the two operands are equal and, if so, the condition, which returns True |
!= |
Check that the values of the two operands are not equal , and if so, the condition is true |
> |
Checks whether the value of the left operand is greater than the value of the right operand, and if so, returns True |
< |
Checks whether the value of the left operand is less than the value of the right operand, and if so, returns True |
>= |
Checks whether the value of the left operand is greater than or equal to the value of the right operand, and if so, returns True |
<= |
Checks whether the value of the left operand is less than or equal to the value of the right operand, and if so, returns True |
Python 2.x does not mean that you can also use the <> operator
! = in Python 2.x can also be used to judge not equal to
3.3 Logical operators
operator |
logical expression |
description |
and |
x and y |
only the values of x and Y are true to return true otherwise as long as X or Y has a value of False, Returns False |
or |
x or y |
|
not |
not X |
If X is true, return False if X is False, return True |
3.4 Assignment operators
- In Python, use = To assign a value to a variable
- In arithmetic operations, Python also provides a series of assignment operators that correspond to arithmetic operators in order to simplify the writing of the Code.
- Note: spaces cannot be used in the middle of an assignment operator
Operator |
Describe |
Instance |
= |
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 |
//= |
Take the divisible assignment operator |
C//= A is equivalent to C = c//A |
%= |
Modulo (remainder) assignment operator |
C%= A is equivalent to C = c% A |
**= |
Power assignment operator |
c = A is equivalent to C = C A |
Precedence of the 3.5 operator
- The following table has the arithmetic precedence from highest to lowest order
Operator |
Describe |
** |
Power (highest priority) |
* / % // |
Multiply, divide, take remainder, divide evenly |
+ - |
addition, subtraction |
<= < > >= |
Comparison operators |
== != |
equals operator |
= %= /= //= -= += *= **= |
Assignment operators |
Not OR and |
logical operators |
Python Basic 2-operator