# Python Operator

## What is an operator?

This section mainly describes the Python operators. For a simple example ** 4 +5 = 9 **.
In the example, **4** and **5** are called **operands**, and "**+**" are called operators.

The Python language supports the following types of operators:

Next let's learn Python operators one by one.

## Python Arithmetic Operator

The following hypothetical variables: ** a=10, b=20**:

Operator | Description | Instance |

+ | Plus - Add two objects | a + b Output 30 |

- | minus - get a negative number or a number minus another number | a - b output result -10 |

* | multiply - multiply two numbers or return a string that is repeated several times | a * b output result 200 |

/ | Division - x divided by y | b / a Output 2 |

% | modulo - returns the remainder of the division | b % a output 0 |

** | power - returns the power of y of x | a**b is the 20th power of 10, and the output is 100000000000000000000 |

// | Division - Returns the integer part of the quotient (**round down**) |
>>> 9//2
4
>>> -9//2
-5 |

The following example demonstrates the operation of all Python arithmetic operators:

## Instance(Python 2.0+)

a = 21
b = 10
c = 0
c = a + b
print "1 - c Value：", c
c = a - b
print "2 - c Value：", c
c = a * b
print "3 - c Value：", c
c = a / b
print "4 - c Value：", c
c = a % b
print "5 - c Value：", c
a = 2
b = 3
c = a**b
print "6 - c Value：", c
a = 10
b = 5
c = a//b
print "7 - c Value：", c

**note：**Python2.x ，Integers divide an integer and only get an integer. If you want to get the fractional part, change one of them to a floating point number.

## Python comparison operator

The following hypothesis variable a is 10 and variable b is 20:

Operator | Description | Instance |

== | is equal to - compare objects are equal | (a == b) returns False. |

!= | Not equal to - Compare two objects are not equal | (a != b) Returns true. |

<> | Not equal to - Compare 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. This is equivalent to the special variables True and False, respectively. | (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 example demonstrates the operation of all Python comparison operators:

The following example demonstrates the operation of all Python comparison operators.：

## Instance(Python 2.0+)

a = 21
b = 10
c = 0
if a == b :
print "1 - a equal b"
else:
print "1 - a not equal to b"
if a != b :
print "2 - a not equal to b"
else:
print "2 - a equal b"
if a <> b :
print "3 - a not equal to b"
else:
print "3 - a equal b"
if a < b :
print "4 - a Less than b"
else:
print "4 - a greater or equal to b"
if a > b :
print "5 - a more than the b"
else:
print "5 - a Less than or equal to b"
a = 5
b = 20
if a <= b :
print "6 - a Less than or equal to b"
else:
print "6 - a more than the b"
if b >= a :
print "7 - b greater or equal to a"
else:
print "7 - b Less than a"

The above example output：

1 - a not equal to b
2 - a not equal to b
3 - a not equal to b
4 - a greater or equal to b
5 - a more than the b
6 - a Less than or equal to b
7 - b greater or equal to a

## Python assignment operator

The following hypothesis variable a is 10，The variable b is 20：

Operator | Description | Instance |

= | Simple assignment operator | c = a + b Assign the result of a + b to c |

+= | Additional 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 |

%= | Modification Assignment Operator | c %= a Equivalent to c = c % a |

**= | Power assignment operator | c **= a is equivalent to c = c ** a |

//= | Rounding the assignment operator | c //= a is equivalent to c = c // a |

The following example demonstrates the operation of all Python assignment operators:

The output of the above example:

1 - c Value： 31
2 - c Value： 52
3 - c Value： 1092
4 - c Value： 52
5 - c Value： 2
6 - c Value： 2097152
7 - c Value： 99864

## Python Bit operator

The bitwise operator is calculated by treating the number as a binary. The bitwise algorithm in Python is as follows:

In the table below, the variable a is 60 and b is 13. 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

< Td> (a & b) output 12, binary interpretation: 0000 1100
Operator | Description | Instance |

& | Bitwise AND Operator: Two values participating in the operation. If both corresponding bits are 1, the result of this bit is 1, otherwise 0 |

| | Bitwise OR Operator: As long as one of the corresponding two binary digits is 1, the result bit is 1. | (a | b) Output 61, binary interpretation: 0011 1101 |

^ | bitwise XOR operator: When the two corresponding binary digits are different, the result is 1 | (a ^ b) Output 49, binary interpretation: 0011 0001 |

~ | Bitwise inversion operator: Inverts each binary bit of data, that is, changes 1 to 0 and 0 to 1. ~x is similar to -x-1 | (~a ) output -61 , binary interpretation : 1100 0011, in the complement form of a signed binary number. |

<< | Left move operator: Each binary of the operand is shifted left by several bits, to the right of <<<<>> The number specifies the number of bits to move, the high bit is discarded, and the low bit is zero. | a << 2 output 240, binary interpretation: 1111 0000 |

>> | Right Move Operator: Shifts each binary of the operand to the left of ">>" to the right by several bits, > > The number on the right specifies the number of bits moved | a >> 2 Output 15 , Binary Explanation: 0000 1111 |

The following example demonstrates the operation of all Python bitwise operators:

## Instance(Python 2.0+)

a = 60
b = 13
c = 0
c = a & b;
print "1 - c Value：", c
c = a | b;
print "2 - c Value：", c
c = a ^ b;
print "3 - c Value：", c
c = ~a;
print "4 - c Value：", c
c = a << 2;
print "5 - c Value：", c
c = a >> 2;
print "6 - c Value：", c

The output of the above example:

The Python language supports logical operators. The following assumes that the variable a is 10 and b is 20:

Operator | Logical Expression | Description | Instance |

and | x and y | Boolean "and" - If x is False, x and y return False, otherwise it returns the calculated value of y. | (a and b) returns 20. |

or | x or y | Boolean "or" - If x is nonzero, 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 0 if x is True. If x is False, it returns True. | not(a and b) returns False |

The output of the above example:

## Instance (Python 2.0+)

variable a And b are both true

"
else:
print "1 - variable a with b Have one not true"
if a or b :
print "2 - variable a with b All for true，Or one of the variables is true"
else:
print "2 - variable a with b Not for true"
a = 0
if a and b :
print "3 - variable a with b Not for true"
else:
print "3 - variable a with b Have one not true"
if a or b :
print "4 - variable a with b All for true, or one of the variables istrue"
else:
print "4 - variable a with b Not for true"
if not( a and b ):
print "5 - variable a with b All for false，Or one of the variables isfalse"
else:
print "5 - variable