# Python Operators

Here you will learn about operators in python with examples, that is, arithmetic, logical, relational, assignment, bitwise, membership, and identity operators in python with examples.

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
- Subtraction Operator Used for subtraction
* Multiplication Operator Used for multiplication
/ Division Operator Used for division
% Modulus Operator Returns remainder after dividing
// Floor Division Operator Returns the quotient without any digits after decimal
** Exponent Operator Used to perform exponential calculation on 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: Let's take another Python arithmetic operator example code with some calculation directly using the program.

```print("What will be the simple interest on \
1500 sum of money for 3 years at 15% per annum ?");
chck = input("Press ENTER to find out..")
print("SI = (p*r*t)/100");
print("Simple Interest = (1500 * 3 * 15)/100");
print("(1500 * 3 * 15)/100 =", (1500*3*15)/100);

print("\nFollowing are some more direct calculation:");
chck = input("Press ENTER to check it out..");
print("10 - 5 + 7 =", 10-5+7);
print("10 * 3 =", 10*3);
print("10 / 5 =", 10/5);
print("10 / 3 =", 10/3);
print("10 % 3 =", 10%3);```

Here is the sample run of the above Python example code.

This is first initial output: This is second output after pressing ENTER key or button: This is third output after again pressing ENTER key: ## Python Comparison Operators

Here the following lists the comparison operators available in Python:

Operator Meaning
== Returns true if values of the two operands are equal, otherwise returns false if not equal
!= Returns true if value of the two operands are not equal, otherwise returns false if equal
> Returns true if value of the left operand is greater than right one, otherwise false if value of the right operand is greater than left one
< Returns true if value of the left operand is less than right one, otherwise false if value of the right operand is less than left one
>= Returns true if value of the left operand is greater than or equal to the value of the right one, otherwise false if value of the right operand is greater than left one
<= Returns true if value of the left operand is less than or equal to right one, otherwise false if value of the right operand is less than or equal to left one

### 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 Used for assigning values
+= 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