Python operators What is an operator?
This section mainly describes the Python operators. To give a simple example 4 +5 = 9 . In the example, 4 and 5 are called operands, and the "+" number is the operator.
The Python language supports the following types of operators:
- Arithmetic operators
- Compare (relational) operators
- Assignment operators
- logical operators
- Bitwise operators
- Member operators
- Identity operator
- Operator Precedence
Next, let's learn about Python's operators.
Python arithmetic operators
The following assumes that the variable A is 10 and the variable B is 20:
operator |
description |
instance |
+ |
Plus-two object additions |
A + b outputs the result of a |
- |
minus-get negative numbers or one number minus another |
a-B output result -10 |
* |
multiply by two numbers or return a string that is repeated several times |
A * b output result x |
/ |
except-x divided by y |
b/a output 2 |
% |
modulo-Returns the remainder of division |
B% A output result 0 |
* * |
Power-Returns the Y power of x |
a**b output Results |
// |
to divide-Returns the integer portion of a quotient |
9//2 output result 4, 9.0//2.0 output 4.0 |
The following example demonstrates the operation of all Python arithmetic operators: #!/usr/bin/python a = 21b = 10c = 0 c = A + bprint "line 1-value of C is", c C = a-bprint "L Ine 2-value of C is ', c c = A * bprint "line 3-value of C are", c C = a/bprint "line 4-value of C is", c C = A% bprint "line 5-value of C are", c a = 2b = 3c = a**b print "line 6-value of C is", c a = 10b = 5c = a//b print "L Ine 7-value of C is ", C above instance output result: line 1-value of C is 31Line 2-value of C is 11Line 3-value of C is 210Line 4- Value of C is 2Line 5-value of C are 1Line 6-value of C is 8Line 7-value of C is 2 python comparison operator
The following assumes that the variable A is 10 and the variable B is 20:
operator |
description |
instance |
= |
equals -The comparison object is equal to |
(a = = B) to return False. |
! = |
Not equal-compare two objects are not equal |
(A! = B) returns true. |
<> |
does not equal-compares two objects for equality |
(a <> B) returns True. This operator is similar to! =. |
, |
greater than-return x is greater than Y |
(a > B) returns FALSE. |
<</td> |
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. |
<= |
is less than or equal-returns whether X is less than or equal to Y. |
(a <= B) returns True. |
The following example demonstrates the operation of all Python comparison operators: #!/usr/bin/python a = 21b = 10c = 0 if (A = = B): print "L Ine 1-a is equal-B "Else: print" line 1-a was not equal to B " if (A! = b): p Rint "line 2-a isn't equal to B" Else: print "line 2-a are equal to B" if (a <> b): print "line 3-a isn't equal to B" Else: print "line 3-a are equal to B" if (A < b ): print "line 4-a are less than B" else: print "line 4-a are not less than B" &n Bsp;if (A > B): print "line 5-a was greater than B" Else: print "line 5-a are not Greater than B " a = 5;b = 20;if (a <= B): print" line 6-a was either less than or equal to &NB Sp;b "Else: print" line 6-a was neither less than nor equal to b " if (b >= a):   ; print "Line 7-b is either greater than or equal to B "Else: print" line 7-b is neither greater than nor equ Al to B "above instance output: line 1-a are not equal to bLine 2-a are not equal to bLine 3-a are not equal to BLine 4-a are n OT less than bLine 5-a are greater than bLine 6-a are either less than or equal to BLine 7-b are either greater than or Equal to B python assignment operator
The following assumes that the variable A is 10 and the variable B is 20:
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 |
The following example shows the operation of all Python assignment operators: #!/usr/bin/python a = 21b = 10c = 0 c = A + bprint "line 1-value of C is", c c + = Aprint "line 2-value of C are ", C C *= aprint" line 3-value of C are ", C C/= a print" line 4-value of C is ", c C = 2c%= A Print "line 5-value of C are", C C **= aprint "line 6-value of C are", C C//= aprint "line 7-value of C is", C above instance Output: Line 1-value of C are 31Line 2-value of C is 52Line 3-value of C are 1092Line 4-value of C is 52Line 5-value of c is 2Line 6-value of C are 2097152Line 7-value of C is 99864 python bit operator
A bitwise operator computes a number as a binary. The bitwise algorithms in Python are as follows:
operator |
description |
instance |
& |
bitwise-AND operator |
(A & B) Output result 12, binary interpretation: 0000 1100 |
| |
bitwise OR operator |
(A | b) output result 61, Binary interpretation: 0011 1101 |
^ |
bitwise XOR operator |
(a ^ b) output result 49, binary interpretation: 0011 0001 |
~ |
bitwise inverse operator |
(~a) output result-61, Binary interpretation: 1100 0011, in the complement form of a signed binary number. |
<< |
left move operator |
a << 2 output result 240, binary interpretation: 1111 0000 |
>> |
Right Move operator |
a >> 2 output result 15, binary interpretation: 0000 1111 |
The following example demonstrates the operation of all Python bitwise operators: #!/usr/bin/python a = # = 0011 1100 b = # 13 = 0000 1101 c = 0 c = A & B; # = 0000 1100print "line 1-value of C is", C c = a | b # = 0011 1101 print "line 2-value of C is", c c = a ^ b; # = 0011 0001print "line 3-value of C is", c c = ~a; # -61 = 1100 0011print "line 4-value of C is", c c = a << 2; # = 1111 0000print "line 5-value of C is", C c = a >> 2; # = 0000 1111print "line 6-value of C is", output of C above example: line 1-value of C i s 12Line 2-value of C is 61Line 3-value of C is 49Line 4-value of C is-61line 5-value of C are 240Line 6-value of C is 15 python logical operator
The Python language supports logical operators, with the following assumption that variable A is 10 and variable B is 20:
operator |
Description |
Example |
and |
Boolean "and"-if x is False,x and Y returns false, otherwise it returns the computed value of Y. |
(A and B) returns True. |
Or |
Boolean "or"-if x is true, it returns true, otherwise it returns the computed value of Y. |
(A or B) returns true. |
Not |
Boolean "Non"-returns False if X is true. If x is False, it returns true. |
Not (A and B) returns false. |
The following example demonstrates the operation of all Python logical operators: #!/usr/bin/python a = 10b = 20c = 0 if (A and B): print " Line 1-a and B were true "Else: print" line 1-either A was not true or B was not true " if (A or B) : print "Line 2-either A was true or B is true or both be true" Else: print "line 2-n Either A is true nor B was true " a = 0if (A and B): print" line 3-a and B are true "ELSE:&NB Sp; print "line 3-either A was not true or B was not true" if (A or B): print "line 4- Either A is true or B is true or both be true "Else: print" line 4-neither A was true nor B is true "&NB Sp;if not (A and B): print ' line 5-a and B ' is true ' else: print ' line 5-either A is Not true or B is not true "the above instance output results: line 1-a and B are Trueline 2-either A is true or B is true or both is true Line 3-either a isn't True or B is not Trueline 4-either A was true or B is true or both be trueline 5-a and B are true python member operators
In addition to some of the above operators, Python also supports member operators, which contain a series of members, including strings, lists, or tuples.
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. |
The following example demonstrates the operation of all Python member operators: #!/usr/bin/python a = 10b = 20list = [1, 2, 3, 4, 5]; if (A in list): print "line 1-a are available in the given list" Else:print "line 1-a are not available in the GIV En list ' if (b not in list): print ' line 2-b is not available in the given list ' else:print ' line 2-b ' is Availab Le in the given list ' A = 2if (A in list): ' print ' line 3-a are available in the given list ' Else:print ' line 3-a is not available in the given list "above instance output: Line 1-a are not available in the given Listline 2-b are not available in T He given Listline 3-a is available in the given list Python identity operator
The identity operator is used to compare the storage units of two objects
operator |
description |
instance |
is |
are to determine if two identifiers are references From an object |
x is y if ID (x) equals ID (y), is returns result 1 |
is not |
is does not determine whether two identifiers are referenced from different objects |
x is not y if the ID (x) does not equal the ID (y). is not returns result 1 |
The following example shows the operation of all Python identity operators: #!/usr/bin/python a = 20b = if (A is B): print "line 1-a and B has same identity" else: Print "line 1-a and B don't have same identity" if (ID (a) = = ID (b)): print "line 2-a and B has same identity" E Lse:print "line 2-a and B don't have same identity" B = 30if (A was B): print "line 3-a and B has same identit Y "Else:print" line 3-a and B don't have the same identity "if (A is not B): print" line 4-a and B does not have same The identity "Else:print" line 4-a and B has same identity "above instance output: line 1-a and B has same identityline 2-a and b There are same identityline 3-a and b do not having same identityline 4-a and b do not have same identity Python operator precedence
The following table lists all the operators from highest to lowest priority:
operator |
description |
* |
Index (highest priority) |
~ +- |
bitwise rollover, unary plus and minus signs (the last two methods are named [email protected] and [email protected]) |
*/%// |
multiply, divide, modulo, and take divide |
+- |
addition subtraction |
>> << |
Right shift, left-shift operator |
& |
bits ' and ' |
^ | |
Bitwise operators |
<= < > >= |
comparison operators |
<> = = = |
equals operator |
=%=/=//=-= + = *= **= |
assignment operator |
is a not |
identity operator |
in Not in |
member operator |
not OR and |
logical operators |
The following example demonstrates the operation precedence of all Python operators: #!/usr/bin/python a = 20b = 10c = 15d = 5e = 0 E = (A + b) * C/D # (* +)/5print " Value of (A + B) * C/D is ", e-E = ((A + b) * c)/D # (+ *)/5print" Value of ((A + b) * C)/d is ", E = (A + B) * (C/D); # (15/5) print "Value of (A + B) * (C/D) is", e E = a + (b * c)/D; # + (150/5) print "Value of a + (b * c)/d is", E above instance output result: Value of (A + B) * C/D is 90Value of ((A + b) * c)/d is 90Value of (A + B) * (C/D) are 90Value of A + (b * c)/d is 50
Python arithmetic operators