**Python ****Operators** are used to performing operations on variables and values. The operators use numbers, strings other data types to get the action and application make math calculation or logic

In this tutorial, you will learn about different types of **Python ****Operators**, their syntax expression, and how to use them with examples.

Python language supports the following types of operators.

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

## Let’s see the one by one Python Operators :

**Python Arithmetic Operators**

Operator | Name |
---|---|

+ | Addition |

– | Subtraction |

* | Multiplication |

/ | Division |

% | Modulus |

** | Exponentiation |

// | Floor division |

You can perform math operation with Arithmetic’s operators, here are examples of Arithmetic’s operators :

a = 10 b = 4 print("Addition : ", 10 + 4) print("Subtraction : ", 10 - 4) print("Multiplication : ", 10 * 4) print("Division : ", 10 / 4) print("Modulus : ", 10 % 4) print("Exponentiation : ", 10 ** 4) print("Floor division : ", 10 // 4)

**Output :** Addition : 14

Subtraction : 6

Multiplication : 40

Division : 2.5

Modulus : 2

Exponentiation : 10000

Floor division : 2

**Python Assignment Operators**

The **assignment operators in Python** are used to store data into a variable.

Operator | Name & Description | Example |
---|---|---|

= | Assigns values from right side operands to left side operand | b = a assigns value of an into b |

+= | Add AND – It adds right operand to the left operand and assigns the result to left operand | c += a is equivalent to c = c + a |

-= | Subtract AND – It subtracts right operand from the left operand and assign the result to left operand | c -= a is equivalent to c = c – a |

*= | Multiply AND – It multiplies right operand with the left operand and assign the result to left operand | c *= a is equivalent to c = c * a |

/= | Divide AND – It divides left operand with the right operand and assign the result to left operand | c /= a is equivalent to c = c / ac /= a is equivalent to c = c / a |

%= | Modulus AND – It takes modulus using two operands and assign the result to left operand | c %= a is equivalent to c = c % a |

**= | Exponent AND – Performs exponential (power) calculation on operators and assign a value to the left operand | c **= a is equivalent to c = c ** a |

//= | Floor Division – It performs floor division on operators and assign value to the left operand | c //= a is equivalent to c = c // a |

a = 5 b = a print("Assigns - value of b : ", b) b += a print("Add AND - value of b : ", b) b -= a print("Subtract AND - value of b : ", b) b *= a print("Multiply AND - value of b : ", b) b /= a print("Divide AND - value of b : ", b) b %= a print("Module AND - value of b : ", b) b **= a print("Exponent AND - value of b : ", b) b //= a print("Floor Division AND - value of b : ", b)

**Output :** Assigns – value of b : 5

Add AND – value of b : 10

Subtract AND – value of b : 5

Multiply AND – value of b : 25

Divide AND – value of b : 5.0

Module AND – value of b : 0.0

Exponent AND – value of b : 0.0

Floor Division AND – value of b : 0.0

**Python Comparison Operators**

Operator | Name | Example |
---|---|---|

== | Equal | (a == b) is not true. |

!= | Not equal | (a != b) is true. |

> | Greater than | (a > b) is not true. |

< | Less than | (a < b) is true. |

>= | Greater than or equal to | (a >= b) is not true. |

<= | Less than or equal to | (a <= b) is true. |

Let’s see the examples

a = 10 b = 4 print("Equal : ", 10 == 4) print("Not equal : ", 10 != 4) print("Greater than : ", 10 > 4) print("Less than : ", 10 < 4) print("Greater than or equal to : ", 10 >= 4) print("Less than or equal to: ", 10 <= 4)

**Output : **Equal : False

Not equal : True

Greater than : True

Less than : False

Greater than or equal to : True

Less than or equal to: False

**Python Logical Operators**

Operator | Description | Example |
---|---|---|

and | Returns True if both statements are true | a < 5 and b < 10 |

or | Returns True if one of the statements is true | a < 5 or b < 4 |

not | Reverse the result, returns False if the result is true | not(a < 5 and b < 10) |

a = 10 b = 4 print("and : ", a < 5 and b < 10) print("or : ", a < 5 or b < 10) print("not : ", not(a < 5 and b < 10))

**Output : **and : False

or : True

not : True

**Python Bitwise Operators**

Operator | Name | Description |
---|---|---|

& | AND | Sets each bit to 1 if both bits are 1 |

| | OR | Sets each bit to 1 if one of two bits is 1 |

^ | XOR | Sets each bit to 1 if only one of two bits is 1 |

~ | NOT | Inverts all the bits |

<< | Zero fill left shift | Shift left by pushing zeros in from the right and let the leftmost bits fall off |

>> | Signed right shift | Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off |

**Python Membership Operators**

Python membership operators test(search or find) for membership in a sequence, such as strings, lists, or tuples. There are two membership operators as below :

Operator | Description | Example |
---|---|---|

in | Returns True if a sequence with the specified value is present in the object | an in listOne |

not in | Returns True if a sequence with the specified value is not present in the object | a not in listOne |

Returns **True** if a sequence with the value is in the list else **False**

colorList = ["red", "yellow", "black"] print("red" in colorList) print("orange" in colorList) print("red" not in colorList)

**Output : **True

False

False

**Python Identity Operators**

Operator | Description | Example |
---|---|---|

is | Returns true if both variables are the same object | a is b |

is not | Returns true if both variables are not the same object | a is not b |

Example

a = 10 b = a print("is : ", a is b) print("is not : ", a is not b)

**Output: **is : True

is not : False

Do comment if you have any doubts and suggestions on this tutorial.

Note: All Python Operators examples is developed in PyCharm 2018.2 (Community Edition)

JRE: 1.8.0

JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o

macOS 10.13.6

Python 3.7All

Examples of Operatorsare in Python 3, so it may change its different from python 2 or upgraded versions.

Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.