Operator Classifications:
Arithmetic operators, assignment operators, comparison operators, logical operators, member operators
One, arithmetic operators
Operator |
Describe |
Example (a=3,b=2) |
+ |
Add operation: Two number added |
A + b = 5 |
- |
Minus operation: two number subtraction |
A-B = 1 |
* |
Multiplication: Multiply by two numbers |
A * b = 6 |
/ |
Divide operation: Two number division (note and c,c++ "/" difference) |
A/b =1.5 |
% |
Take-rest operation: Two-digit division-taking surplus |
A% B = 1 |
** |
Power operation: Returns the power of B of a |
A * * b = 9 |
// |
Rounding operation: Returns the integer part of the quotient |
A//b = 1 |
Note:%, to output the percent sign "%" must be print ("percent")
Second, assignment operation
operator |
Description |
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 |
%= |
Modulo assignment operator |
C%= A is equivalent to C = c% A |
**= |
Power assignment operator |
C **= A is equivalent to C = c * * A |
//= |
Take the divisible assignment operator |
C//= A is equivalent to C = c//A |
Third, comparative operations
1.==, >, >=, <=,! =2. Return Result: Boolean true, Faluse (all comparison operators return 1 for true, return 0 for false) 3. Application scenario: A common set of comparison values, which we call conditional expressions
operator |
Description |
Example |
== |
Equals-compares objects for equality |
(A = = B) returns FALSE. |
!= |
Not equal-compares two objects for unequal |
(A! = B) returns TRUE. |
> |
Greater than-returns whether x is greater than Y |
(A > B) returns FALSE. |
< |
Less-Returns whether x is less than Y. All comparison operators return 1 for true, and return 0 for false. This distinction is equivalent to the special variable true and false. Note that these variable names are capitalized. |
(A < B) returns TRUE. |
>= |
Greater than or equal-returns whether X is greater than or equal to Y. |
(a >= B) returns FALSE. |
<= |
Less than or equal-returns whether X is less than or equal to Y. |
(a <= B) returns True. |
Four, logical operators
1. And, Or2, and generally are often used by us as conditional expressions: If, while3, Result: and: True True or false false
operator |
Logical Expressions |
Description |
Example |
and |
X and Y |
Boolean "and"-if x is False,x and y returns FALSE, otherwise it returns the computed value of Y. |
(A and B) returns 20. |
Or |
X or Y |
Boolean "or"-if X is non-0, it returns the value of x, otherwise it returns the computed value of Y. |
(A or B) returns 10. |
Not |
Not X |
Boolean "Non"-returns False if X is True. If X is False, it returns TRUE. |
Not (A and B) returns False |
Five, member operators
1.in, not in2. Result: Boolean value True, Faluse
operator |
Description |
Example |
Inch |
Returns False if the value found in the specified sequence returns True. |
x in the y sequence, if X returns True in the y sequence. |
Not in |
Returns True if no value is found in the specified sequence, otherwise False. |
X is not in the Y sequence if x does not return True in the y sequence. |
Six, identity operators
operator |
Description |
Example |
Is |
is to determine whether two identifiers are referenced from an object |
X is Y, similar to ID (x) = = ID (y), returns True if the same object is referenced, otherwise False |
is not |
Is does not determine whether two identifiers are referenced from different objects |
X is not y, similar to ID (a)! = ID (b). If the reference is not the same object, the result returns True, Otherwise, False is returned. |
Usage examples:
#python identity operatorA = 20b= 20#is to determine whether two identifiers refer to the same objectif(A isb):Print('A and B have the same identity')Else: Print('A and B do not have the same identity') #is does not determine whether two identifiers are referenced from different objectsif(A is notb):Print('A and B do not have the same identity')Else: Print('A and B have the same identity') #Modify the value of variable Bb = 30if(A isb):Print('A and B have the same identity')Else: Print('A and B do not have the same identity') if(A is notb):Print('A and B do not have the same identity')Else: Print('A and B have the same identity')
Result of instance output: A and B have the same identity A and B have the same identity A and b do not have the same identity A and b do not have the same identity
Seven, Python operator precedence
The following table lists the precedence of operators: High---> Low
operator |
Description |
** |
Index (highest priority) |
~ + - |
Bitwise flip, unary Plus and minus (the last two methods are called [email protected] and [email protected]) |
* / % // |
Multiply, divide, modulo, and divide |
+ - |
Addition subtraction |
>> << |
Shift right, left shift operator |
& |
Bit ' and ' |
^ | |
Bitwise operators |
<= < > >= |
Comparison operators |
<> = = = |
equals operator |
= %= /= //= -= += *= **= |
Assignment operators |
Is isn't |
Identity operator |
In No in |
Member operators |
Not OR and |
logical operators
|
python.day.01--operator Classification