Python operators
What is an operator?
This chapter mainly explains Python’s operators. Let’s take a simple example 4 +5 = 9 . In the example, 4 and 5 are called operands, and "+" is called the operator.
The Python language supports the following types of operators:
Arithmetic operators
Comparison (relational) operators
Assignment operator
Logical operator
Bitwise operator
Member operator
Identity operator
Operator precedence
Next let us learn Python operators one by one.
Python arithmetic operators
The following assumes that variable a is 10 and variable b is 20:
Operator | Description | Example |
---|---|---|
+ | Add - Add two objects | a + b Output result 30 |
- | Subtract - Get a negative number or one number minus another number | a - b Output result-10 |
* | Multiplication - Multiply two numbers or return a string repeated several times | a * b Output result 200 |
/ | Divide - x divided by y | b / a Output result 2 |
% | Modulo - Returns the remainder of division | b % a Output result 0 |
** | Power - Returns x raised to the yth power | a**b is 10 raised to the 20th power, and the output result is 100000000000000000000 |
// | Return the integer part of the quotient | 9//2 Output result 4, 9.0//2.0 Output result 4.0 |
The following example demonstrates the operation of all arithmetic operators in Python:
# -*- coding: UTF-8 -*-
a = 21
b = 10
c = 0
c = a + b
print "1 - The value of c is:", c
c = a - b
print "2 - The value of c is: ", c
c = a * b
print "3 - The value of c is: ", c
c = a / b
print "4 - The value of c is: ", c
c = a % b
print "5 - The value of c is: ", c
# Modify variables a, b, c
a = 2
b = 3
c = a**b
print "6 - The value of c is:", c
a = 10
b = 5
c = a//b
print "7 - The value of c is:", c
Try it »
The above example output result:
2 - The value of c is: 11
3 - The value of c is: 210
4 - The value of c is: 2
5 - The value of c is: 1
6 - The value of c is: 8
7 - The value of c is: 2
Python comparison operator
The following assumes that variable a is 10 and variable b is 20:
Operator | Description | Example |
---|---|---|
== | Equal - compare whether the objects are equal | (a == b) Return False. |
!= | Not equal to - Compares whether two objects are not equal | (a != b) Returns true. |
<> | Not equal to - Compares whether two objects are not equal | (a <> b) Returns true. This operator is similar to != . |
> | Greater than - Returns whether x is greater than y | (a > b) Returns False. |
< | Less than - Returns whether x is less than y. All comparison operators return 1 for true and 0 for false. These are equivalent to the special variables True and False respectively. Note the capitalization of these variable names. | (a < b) returns true. |
>= | Greater than or equal to - Returns whether x is greater than or equal to y. | (a >= b) Returns False. |
<= | Less than or equal to - Returns whether x is less than or equal to y. | (a <= b) returns true. |
The following examples demonstrate the operation of all comparison operators in Python:
# -*- coding: UTF-8 -*-
a = 21
b = 10
c = 0
if ( a == b ):
print "1 - a equals 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 not equal to b"
else:
print "3 - a is equal to b"
if ( a < b ):
print "4 - a is less than b"
else:
print "4 - a is greater than or equal to b"
if ( a > b ):
print "5 - a is greater than b"
else:
print "5 - a is less than or equal to b"
# Modify the values of variables a and b
a = 5 ;
b = 20;
if ( a <= b ):
print "6 - a is less than or equal to b"
else:
print "6 - a is greater than b"
if ( b >= a ):
print "7 - b is greater than or equal to b"
else:
print "7 - b is less than b"
The output result of the above example:
2 - a is not equal to b
3 - a is not equal to b
4 - a is greater than or equal to b
5 - a is greater than b
6 - a is less than or equal to b
7 - b is greater than or equal to b
Python assignment operator
The following assumes that variable a is 10 and variable b is 20:
Operator | Description | Example | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
= | Simple assignment operator | c = a + b The operation of a + b The resulting assignment is c | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
+= | Additional assignment operator | c += a is equivalent to c = c + a | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
-= | Subtractive 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 integer division assignment operator | c //= a is equivalent to c = c // a |
Operator | Description | Instance | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
& | Bitwise AND operator: Two values participating in the operation, if the two corresponding bits are 1, the result of the bit is 1, otherwise it is 0 | (a & b) The output result is 12, binary interpretation : 0000 1100 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Bitwise OR operator: As long as one of the two corresponding binary bits is 1, the result bit will be 1. | (a | b) The output result is 61, binary interpretation: 0011 1101 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Bitwise XOR operator: when two corresponding When the binary bits are different, the result is 1 | (a ^ b) and the output result is 49. Binary interpretation: 0011 0001 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
bitwise Negation operator: Negate each binary bit of the data, that is, change 1 to 0, and change 0 to 1 | (~a) The output result is -61, binary interpretation: 1100 0011, in a The two's complement form of a signed binary number. | ##<< | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
a << 2 The output result is 240, binary interpretation: 1111 0000 | >> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
a >> 2 The output result is 15, binary Explanation: 0000 1111 |
Instance | and | x and y | ||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
(a and b) returns 20. | or | x or y | ||||||||||||||||||||||||||||||||||||||||||||||
(a or b) returns 10. | not | not x | ||||||||||||||||||||||||||||||||||||||||||||||
not(a and b) returns False |
Operator | Description | Instance |
---|---|---|
in | Returns True if a value is found in the specified sequence, False otherwise. | x is in the y sequence, returns True if x is in the y sequence. |
not in | Returns True if the value is not found in the specified sequence, False otherwise. | x is not in the y sequence, returns True if x is not in the y sequence. |
The following example demonstrates the operation of all member operators in Python:
# -* - coding: UTF-8 -*-
a = 10
b = 20
list = [1, 2, 3, 4, 5 ];
if ( a in list ):
print "1 - Variable a is in the given list 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 list"
# Modify the value of variable a
a = 2
if ( a in list ):
print "3 - Variable a is in the given list list "
else :
print "3 - Variable a is not in the given list list"
The above example output result:
2 - variable b is not in the given list list
3 - variable a is in the given list list
Python identity operator
Identity operator used to compare storage locations of two objects
Operator | Description | Instance |
---|---|---|
is | is is used to determine whether two identifiers refer to an object. | x is y, if id(x) is equal to id(y ), is returns the result 1 |
is not is to determine whether the two identifiers refer to different objects | x is not y, if id(x) is not equal to id(y). | is not Return result 1 |
Operations Symbol | Description |
---|---|
** | Index (highest priority) |
~ + - | Bitwise flip, unary plus sign and minus sign (the last two methods are named +@ and -@) |
* / % // | Multiplication, division, modulo and integer division |
+ - | Addition and subtraction |
>> < ;< | Right shift, left shift operator |
& | bit'AND' |
^ | | Bitwise operators |
Comparison operators | |
Equal operator | |
Assignment operator | |
Identity operator | |
Member operator | |
Logical operator |