Python3 Operators
What is An Operator?
This chapter mainly explains the operators of Python.
A simple example:
4 + 5 = 9
In the example, 4 and 5 are called operands , and + is called operator .
The Python language supports the following types of operators:
Arithmetic Operators
Comparison / Relational Operators
Assignment Operators
Logical Operators
Bitwise Operators
Member Operators
Identity Operators
Operator's Priotiry
Next, let us learn Python operators one by one.
Python Arithmetic Operators
The following assumes that the variable a=10 and the variable b=21 :
Operator | Description | Example |
---|---|---|
+ | (Add) Add two objects | a + b, output: 31 |
- | (Subtract) Get a negative number or subtract one number from another | a-b, output: -11 |
* | (Multiply) Multiply two numbers or return a string repeated several times | a * b, output: 210 |
/ | (Divide) x divided by y | b / a, output: 2.1 |
% | (Modulo) Returns the remainder of the division | b% a, output: 1 |
** | (Power) Returns x to the power of y | a**b is 10 to the 21st power |
// | (Divide) round down to an integer close to the quotient | 9 // 2 output 4 |
The following example demonstrates the operation of all arithmetic operators in Python:
Examples (Python 3+)
#!/usr/bin/python3 a = 21 b = 10 x = 0 x = a + b print ("1 - the value of x:", x) x = a-b print ("2 - the value of x:", x) x = a * b print ("3 - the value of x:", x) x = a / b print ("4 - the value of x:", x) x = a% b print ("5 - the value of x:", x) # Modify variables a, b, x a = 2 b = 3 x = a**b print ("6 - the value of x:", x) a = 10 b = 5 x = a//b print ("7 - the value of x:", x)
The output of the above examples:
1 - the value of x: 31
2 - the value of x: 11
3 - the value of x: 210
4 - the value of x: 2.1
5 - the value of x: 1
6 - the value of x: 8
7 - the value of x: 2
Python Comparison Operators
The following assumes that the variable a is 10 and the variable b is 20:
Operator | Description | Example |
---|---|---|
== | (Equal to): Compare whether the objects are equal | (a == b), return False |
!= | (Not equal): Compares whether two objects are not equal | (a != b), return True |
> | (Greater than): Returns whether x is greater than y | (a> b), returns False |
< | (Less than): Returns whether x is less than y | (a <b), return True |
>= | (Greater than or equal to): Returns whether x is greater than or equal to y. | (a >= b), return False |
<= | (Less than or equal to): Returns whether x is less than or equal to y. | (a <= b), return True |
Note: All comparison operators return 1 for true and 0 for false. This is equivalent to the special variables True and False respectively.
The following example demonstrates the operation of all comparison operators in Python:
Examples (Python 3+)
#!/usr/bin/python3 a = 21 b = 10 c = 0 if (a == b): print("1-a is equal to b") else: print("1-a is not equal to b") if (a != b): print("2-a is not equal to b") else: print("2-a is equal to b") if (a < b): print("3-a is less than b") else: print("3-a is greater than or equal to b") if (a > b): print("4-a is greater than b") else: print("4-a is less than or equal to b") # Modify the values of variables a and b a = 5 b = 20 if (a <= b): print("5-a is less than or equal to b") else: print("5-a is greater than b") if (b >= a): print("6-b is greater than or equal to a") else: print("6-b is less than a")
The output of the above examples:
1-a is not equal to b
2-a is not equal to b
3-a is greater than or equal to b
4-a is greater than b
5-a is less than or equal to b
6-b is greater than or equal to a
Python Assignment Operators
The following assumes that the variable a is 10 and the variable b is 20:
Operator | Description | Example |
---|---|---|
= | Simple assignment operator | x = a + b assign the result of a + b to x |
+= | Addition assignment operator | x += a is equivalent to x = x + a |
-= | Subtraction assignment operator | x -= a is equivalent to x = x - a |
*= | Multiplication assignment operator | x *= a is equivalent to x = x * a |
/= | Division assignment operator | x /= a is equivalent to x = x / a |
%= | Modulo assignment operator | x %= a is equivalent to x = x % a |
**= | Power assignment operator | x **= a is equivalent to x = x ** a |
//= | Divide and Divide Assignment Operator | x //= a is equivalent to x = x // a |
:= | The walrus operator can assign values to variables inside expressions. (This is an operator are added in Python3.8) | In this example, the assignment expression can avoid calling len() twice: if (n := len(a)) > 10: print(f"List is too long ({n} elements, expected <= 10)") |
The following example demonstrates the operation of all assignment operators in Python:
Examples (Python 3+)
#!/usr/bin/python3 a = 21 b = 10 x = 0 x = a + b print("The value of 1-x is:", x) x += a print("2-the value of x:", x) x *= a print("3-the value of x:", x) x /= a print("4-the value of x:", x) x = 2 x %= a print("5-the value of x is:", x) x **= a print("6-the value of x:", x) x //= a print("7-the value of x:", x)
The output of the above example:
The value of 1-x is: 31 2-the value of x: 52 3-the value of x: 1092 4-the value of x: 52.0 5-the value of x is: 2 6-the value of x: 2097152 7-the value of x: 99864
Python Bitwise Operators
Bitwise operators treat numbers as binary to perform calculations. The bitwise algorithm in Python is as follows:
In the following table, the variable a is 60 and b is 13 and the binary format is as follows:
a = 0011 1100
b = 0000 1101
-----------------
a&b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a = 1100 0011
Operator | Description | Example |
---|---|---|
& | Bitwise AND operator: the two values involved in the operation, if the two corresponding bits are both 1, the result of the bit is 1, otherwise it is 0 | (a & b) Output result 12, binary interpretation: 0000 1100 |
| | Bitwise OR operator: As long as one of the two corresponding binary bits is 1, the result bit is 1. | (a | b) Output result 61, binary interpretation: 0011 1101 |
^ | Bitwise XOR operator: When two corresponding binary bits are different, the result is 1 | (a ^ b) Output result 49, binary interpretation: 0011 0001 |
~ | Bitwise inversion operator: Invert each binary bit of the data, that is, change 1 to 0 and 0 to 1. ~x is similar to -x-1 | (~a) Output result -61, binary interpretation: 1100 0011, in the complement form of a signed binary number. |
<< | Left shift operator: all binary bits of the operand are shifted to the left by several bits, the number to the right of "<<" specifies the number of bits to move, the high bits are discarded, and the low bits are filled with 0. | a << 2 Output result 240, binary interpretation: 1111 0000 |
>> | Right shift operator: shift all the binary bits of the operand on the left of ">>" to the right by several bits, and the number on the right of ">>" specifies the number of bits to move | a >> 2 Output result 15, binary interpretation: 0000 1111 |
The following example demonstrates the operation of all bitwise operators in Python:
Examples (Python 3+)
#!/usr/bin/python3 a = 60 # 60 = 0011 1100 b = 13 # 13 = 0000 1101 x = 0 x = a & b # 12 = 0000 1100 print ("1-the value of x:", x) x = a | b # 61 = 0011 1101 print ("2-the value of x:", x) x = a ^ b # 49 = 0011 0001 print ("3-the value of x:", x) x = ~a # -61 = 1100 0011 print ("4-the value of x:", x) x = a << 2 # 240 = 1111 0000 print ("5-the value of x:", x) x = a >> 2 # 15 = 0000 1111 print ("6-the value of x:", x)
The output of the above example:
1-the value of x: 12
2-the value of x: 61
3-the value of x: 49
4-the value of x: -61
5-the value of x: 240
6-the value of x: 15
Python Logical Operators
Python language supports logical operators. The following assumes that the variable a is 10 and b is 20:
Operator | Expression | Description | Example |
---|---|---|---|
and | x and y | Boolean "and": If x is False, x and y return the value of x, otherwise return the calculated value of y. | (a and b) returns 20. |
or | x or y | Boolean "or": if x is True, it returns the value of x, otherwise it returns the calculated value of y. | (a or b) returns 10. |
not | not x | Boolean "not": If x is True, return False. If x is False, it returns True. | not(a and b) returns False |
Examples (Python 3+)
#!/usr/bin/python3 a = 10 b = 20 if (a and b ): print ("1-variables a and b are both true") else: print ("1-one of the variables a and b is not true") if (a or b ): print ("2-variables a and b are both true, or one of the variables is true") else: print ("2-variables a and b are not true") # Modify the value of variable a a = 0 if (a and b ): print ("3-variables a and b are both true") else: print ("3-One of the variables a and b is not true") if (a or b ): print ("4-variables a and b are both true, or one of the variables is true") else: print ("4-variables a and b are not true") if not( a and b ): print ("5-variables a and b are both false, or one of the variables is false") else: print ("5-variables a and b are both true")
The output of the above examples:
1-variables a and b are both true
2-variables a and b are both true, or one of the variables is true
3-One of the variables a and b is not true
4-variables a and b are both true, or one of the variables is true
5-variables a and b are both false, or one of the variables is false
Python Member Operators
In addition to some of the above operators, Python also supports member operators. The test instance contains a series of members, including strings, lists, or tuples.
Operator | Description | Example |
---|---|---|
in | If the value is found in the specified sequence, it returns True, otherwise it returns False. | If x is in the y sequence, it returns True if x is in the y sequence. |
not in | If the value is not found in the specified sequence, it returns True, otherwise it returns False. | If x is not in the y sequence, return True if x is not in the y sequence. |
The following example demonstrates the operation of all member operators in Python:
Examples (Python 3+)
#!/usr/bin/python3 a = 10 b = 20 list = [1, 2, 3, 4, 5] if (a in list): print("1-variable a is in the given list in list") else: print("1-variable a is not in the given list list") if (b not in list): print("2-variable b is not in the given list list") else: print("2-variable b is in the given list in list") # Modify the value of variable a a = 2 if (a in list): print("3-variable a is in the given list in list") else: print("3-variable a is not in the given list list")
The output of the above example:
1-variable a is not in the given list list
2-variable b is not in the given list list
3-variable a is in the given list in list
Python Identity Operators
Identity operator is used to compare the storage units of two objects
Operator | Description | Example |
---|---|---|
is | is: To determine whether two identifiers refer to an object | x is y , similar to id(x) == id(y) , it returns True if the same object is referenced, otherwise it returns False |
is not | is not: To determine whether the two identifiers refer to different objects | x is not y , similar to id(a) != id(b) . If the reference is not the same object, the result is True, otherwise it returns False. |
Note: The id() function is used to obtain the memory address of the object.
The following example demonstrates the operation of all identity operators in Python:
Examples (Python 3+)
#!/usr/bin/python3 a = 20 b = 20 if (a is b): print("1-a and b have the same logo") else: print("1-a and b do not have the same logo") if (id(a) == id(b)): print("2-a and b have the same logo") else: print("2-a and b do not have the same logo") # Modify the value of variable b b = 30 if (a is b): print("3-a and b have the same logo") else: print("3-a and b do not have the same logo") if (a is not b): print("4-a and b do not have the same logo") else: print("4-a and b have the same logo")
The output of the above example:
1-a and b have the same logo
2-a and b have the same logo
3-a and b do not have the same logo
4-a and b do not have the same logo
The difference between is and ==:
is operator is used to determine whether two variable reference objects are the same
== operator is used to determine whether the value of the reference variable is equal.
Examples
>>>a = [1, 2, 3] >>> b = a >>> b is a True >>> b == a True >>> b = a[:] >>> b is a False >>> b == a True
Python Operator's Priority
The following table lists all operators from highest to lowest precedence:
Operator | Description |
---|---|
** | Index (highest priority) |
~ + - | Bitwise flip, unary plus and minus signs (the last two methods are named +@ and -@) |
* / % // | Multiply, divide, find remainder and divide |
+ - | Addition and subtraction |
>> << | Shift right, shift left operator |
& | Bit 'AND' |
^ | | Bit operator |
<= <> >= | Comparison operator |
== != | Equal operator |
= %= /= //= -= += *= **= | Assignment operator |
is / is not | Identity operator |
in / not in | Member operator |
not and or | Logical Operators |
The following example demonstrates the operation of all operator precedence in Python:
Examples (Python 3+)
#!/usr/bin/python3 a = 20 b = 10 c = 15 d = 5 e = 0 e = (a + b) * c / d # ( 30 * 15) / 5 print("(a + b) * c / d The result of the operation is: ", e) e = ((a + b) * c) / d # (30 * 15) / 5 print("((a + b) * c) / d The result of the operation is: ", e) e = (a + b) * (c / d) # (30) * (15/5) print("(a + b) * (c / d) The result of the operation is: ", e) e = a + (b * c) / d # 20 + (150/5) print("a + (b * c) / d The result of the operation is:", e)
The output of the above example:
(a + b) * c / d The result of the operation is: 90.0
((a + b) * c) / d The result of the operation is: 90.0
(a + b) * (c / d) The result of the operation is: 90.0
a + (b * c) / d The result of the operation is: 50.0
and has a higher priority:
Example
x = True y = False z = False if x or y and z: print("yes") else: print("no")
The output of the above example:
yes
Note: Pyhton3 no longer supports the <> operator, you can use != instead, if you must use this comparison operator, you can use the following method:>>> from __future__ import barry_as_FLUFL
>>> 1 <> 2
True