# Python Operators

Operators in Python, are used to perform mathematical and logical operations. There are the following types of operators available in Python:

• Arithmetic Operators
• Logical Operators
• Comparison (Relational) Operators
• Assignment Operators
• Bitwise Operators
• Membership Operators
• Identity Operators

## Python Arithmetic Operators

Here the following table lists the arithmetic operators available in python with their name and meaning:

Operator Name Meaning
+ Addition Operator This operator is used to add the values on either side of the operator
- Subtraction Operator This operator is used to subtract the right hand operand from the left hand operand
* Multiplication Operator This operator is used to multiply the values on either side of the operator
/ Division Operator This operator is used to divide the left hand operand by the right hand operand
% Modulus Operator This operator is used to divide the left hand operand by the right hand operand and returns the remainder
// Floor Division Operator This is the division of the operands where the result is the quotient in which the digits after the decimal point are removed
** Exponent Operator This operator simply performs the exponential (power) calculation on the operators

### Example

Here is an example program, uses arithmetic operators in Python:

```# Python Operators - Python Arithmetic Operators - Example Program

num1 = 23
num2 = 10
res = 0

print("If num1 = 23 and num2 = 10. Then,");

res = num1 + num2
print("num1 + num2 = ", res)

res = num1 - num2
print("num1 - num2 = ", res)

res = num1 * num2
print("num1 * num2 = ", res)

res = num1 / num2
print("num1 / num2 = ", res)

res = num1 % num2
print("num1 % num2 = ", res)

#changing the values of num1 and num2
num1 = 2
num2 = 3

print("\nIf num1 = 2 and num2 = 3. Then,");

res = num1 ** num2
print("num1 ** num2 = ", res)

#again changing the values of num1 and num2
num1 = 10
num2 = 5

print("\nIf num1 = 10 and num2 = 5. Then,");

res = num1 // num2
print("num1 // num2 = ", res)```

Here is the output produced by the above Python program:

## Python Comparison Operators

Here the following lists the comparison operators available in Python:

Operator Meaning
== This operator checks if the value of the two operands are equal or not. If equal, then the condition becomes true, otherwise false
!= This operator checks if the value of the two operands are equal or not. If not equal, then the condition becomes true, otherwise false
> This operator checks if the value of the left operand is greater than the value of the right operand or not. If yes, then the condition becomes true, otherwise false
< This operator checks if the value of the left operand is less than the value of the right operand or not. If yes, then the condition becomes true, otherwise false
>= This operator checks if the value of the left operand is greater than or equal to the value of the right operand or not. If yes, then the condition becomes true
<= This operator checks if the value of the left operand is less than or equal to the value of the right operand or not. If yes, then the condition becomes true

### Example

Here is an example, uses comparison operators in Python:

```# Python Operators - Comparison Operators - Example Program

num1 = 23
num2 = 10
res = 0

print("If num1 = 23 and num2 = 10. Then,");

if(num1 == num2):
print("num1 is equal to num2");
else:
print("num1 is not equal to num2");

if(num1 != num2):
print("num1 is not equal to num2");
else:
print("num1 is equal to num2");

if(num1 < num2):
print("num1 is less than num2");
else:
print("num1 is not less than num2");

if(num1 > num2):
print("num1 is greater than num2");
else:
print("num1 is not greater than num2");

if(num1 <= num2):
print("num1 is either less than or equal to num2");
else:
print("num1 is neither less than or equal to num2");

if(num1 >= num2):
print("num1 is either greater than or equal to num2");
else:
print("num1 is neither greater than or equal to num2");

# changing the values of num1 and num2
num1 = 40
num2 = 40

print("\nIf num1 = 40 and num2 = 40. Then,");

if(num1 <= num2):
print("num1 is either less than or equal to num2");
else:
print("num1 is neither less than or equal to num2");

if(num1 >= num2):
print("num1 is either greater than or equal to num2");
else:
print("num1 is neither greater than or equal to num2");```

Here is the sample run of the above Python program:

## Python Assignment Operators

Here, the following table lists the assignment operators available in Python:

Operator Name Meaning
= Simple Assignment Operator This operator assigns values from the right side operand to the left side operand
+= Add and Assignment Operator This operator adds the right operand to the left operand and assigns the result to the left operand
-= Subtract and Assignment Operator This operator subtracts the right operand from the left operand and then assigns the result to the left operand
/= Divide and Assignment Operator This operator divides the left operand with the right operand and then assigns the result to the left operand
*= Multiply and Assignment Operator This operator multiplies the right operand with the left operand and then assigns the result to the left operand
**= Exponent and Assignment Operator This operator performs the exponential calculation on operators and then assign the value to the left operand
%= Modulus and Assignment Operator This operator takes modulus using the two operands and then assigns the result to the left operand
//= Floor Division and Assignment Operator This operator performs the floor division on the operators and then assign the value to the left operand

### Example

Here is an example program, uses assignment operators in Python:

```# Python Operators - Python Assignment Operators - Example Program

num1 = 25
num2 = 10
res = 0

print("If num1 = 25 and num2 = 10. Then,");

res = num1 + num2
print("num1 + num2 = ", res);

res += num1
print("res + num1 = ", res);

res -= num1
print("res - num1 = ", res);

res *= num1
print("res * num = ", res);

res /= num1
print("res / num1 = ", res);

# changing the values of res
res = 2

res %= num1
print("res % num1 = ", res);

res **= num1
print("res ** num1 = ", res);

res //= num1
print("res // num1 = ", res);```

Here is the sample output of the above Python program:

## Python Bitwise Operators

Bitwise operators in Python, used to perform bitwise operations. The following table lists the bitwise operators available in Python. This table lists all the bitwise operators of python with their name and meaning:

Operator Name Meaning
& Binary AND Operator This operator copies a bit to the result only if its exists in both the operands
^ Binary OR Operator This operator copies a bit only if it exists in either the operand
| Binary XOR Operator This operator copies the bit only if it is the set of one operand but not the both
~ Binary Ones Complement Operator This is unary operator, has the effect of 'flipping' bits
>> Binary Right Shift Operator The left operand's value is moved right by the number of bits specified by the right operand
<< Binary Left Shift Operator The left operand's value is moved left by the number of bits specified by the right operand

### Python Bitwise AND

```  0011 1100   60
& 0000 1101   13
-----------
0000 1100   12```

### Python Bitwise OR

```  0011 1100   60
| 0000 1101   13
-----------
0011 1101   61```

### Python Bitwise XOR

```  0011 1100   60
^ 0000 1101   13
-----------
0011 0001   49```

### Python Bitwise Ones Complement

```  0011 1100   60
^ 0000 1101   13
-----------
1100 0011   -61```

### Example

Here is an example program, uses bitwise operators in Python:

```# Python Operators - Python Bitwise Operators - Example Program

num1 = 60
num2 = 13
res = 0

print("If num1 = 60 and num2 = 13. Then,");

res = num1 & num2;
print("num1 & num2 = ", res);

res = num1 | num2;
print("num1 | num2 = ", res);

res = num1 ^ num2;
print("num1 ^ num2 = ", res);

res = ~num1;
print("~num1 = ", res);

res = ~num2;
print("~num2 = ", res);

res = num1 << 2;
print("num1 << 2 = ", res);

res = num2 << 2;
print("num2 << 2 = ", res);

res = num1 >> 2;
print("num1 >> 2 = ", res);

res = num2 >> 2;
print("num2 >> 2 = ", res);

# changing the values of num1 and num2
num1 = 60
num2 = 0

print("\nIf num1 = 60 and num2 = 0. Then,");

res = num1 & num2;
print("num1 & num2 = ", res);

res = num1 | num2;
print("num1 | num2 = ", res);```

Here is the sample output produced by the above python program:

Here, the following table lists the above (produced as output) decimal value in binary format:

Decimal Value Equivalent Binary Value
60 0011 1100
13 0000 1101
12 0000 1100
61 0011 1101
49 0011 0001
-61 1100 0011
-14 1111 0010
240 1111 0000
52 0011 0100
15 0000 1111
3 0000 0011

## Python Logical Operators

Here, the following table lists the logical operators available in python:

Operator Name Meaning
and Logical AND Operator If both the operands are true, then the condition becomes true, otherwise false
not Logical NOT Operator This operator used to reverse the logical state of its operand
or Logical OR Operator If any of the two operands are non-zero, then the condition becomes true, otherwise false

### Example

Here is an example program, uses logical operators in Python:

```# Python Operators - Python Logical Operators - Example Program

num1 = 100
num2 = 200
res = 0

print("If num1 = 100 and num2 = 200. Then,");

if(num1 and num2):
print("Both num1 and num2 are true");
else:
print("Either num1 or num2 is not true, or both are not true");

if(num1 or num2):
print("Either num1 or num2 is true, or both are true");
else:
print("Neither num1 nor num2 is true");

# changing the values of num1 and num2
num1 = 5
num2 = 0

print("\nIf num1 = 5 and num2 = 0. Then,");

if(num1 and num2):
print("Both num1 and num2 are true");
else:
print("Neither num1 nor num2 is true");

if(num1 or num2):
print("Either num1 or num2 is true, or both are true");
else:
print("Neither num1 nor num2 is true");

if not(num1 and num2):
print("Neither num1 nor num2 is true");
else:
print("Both num1 and num2 are true");
```

Here is the output produced by the above python program:

## Python Operators Precedence

Here the following table lists the operators available in python, with their precedence order:

Higher
**
* / % //
~ + -
+ -
&
>> <<
^ |
<= < > >=
= %= /= //= -= += *= **=
<> == !=
is is not
not or and
in not in
Lower

### More Examples

Here are list of some more examples uses operators in python:

Tools
Calculator

Quick Links
Signup - Login - Give Online Test