# Python Operators

Python Operators with Types and Examples:

Python Data Types were explained in detail along with their classification in our previous tutorial.

In this tutorial, we learn all about Python Operators along with their types. Simple examples pertaining to each type of Python operator are included in this tutorial.

This Python Training Tutorial Series will be a perfect guide for any beginner to enhance their knowledge on Python concepts.

### Watch the VIDEO Tutorials

Overview of Operators in Python (Part 1):

An In-Depth Look at Operators in Python (Part 2):

### Python Operators

What are Python Operators?

Operators are used for carrying out operations on values and variables.

Python has 7 types of Operators as stated below:

• Arithmetic Operator
• Comparison operators
• Logical operators
• Bitwise operators
• Assignment Operator
• Identity operators
• Membership operators

#### #1) Arithmetic Operators

Python programming language supports different kinds of arithmetic operators for both integer and floating point like addition, subtraction, multiplication, division and so on.

Operator TypeDefinition
Subtraction (-)Subtraction operator
Multiplication (*)Multiplication operator
Division (/)Division operator
Modulus (%)Reminder operator
Floor division (//)Divides and returns the value of the remainder.
Exponentiation (**)Raises the left operand to the power of right.

Example:

```x = 15
y = 10
print('x + y =', x+y)
```

Output: x + y = 25

` print('x - y =', x-y) `

Output: x – y = 5

` print('x * y =', x*y) `

Output: x * y = 150

` print('x / y =', x/y) `

Output: x / y = 1.5

` print('x % y =', x%y) `

Output: x % y = 5

` print('x // y =', x//y) `

Output: x // y = 1

` print('x ** y =', x**y) `

Output: x ** y = 576650390625 #### #2) Comparison Operators

Comparison operators are used for comparing values. It either returns True or False according to the condition.

OperatorsDefinition
Greater than (>)True if left operand is greater than the right
Less than (<)True if left operand is less than the right
Equal to (==)True if both operands are equal
Not equal to (!=)True if operands are not equal
Greater than or equal to (>=)True if left operand is greater than or equal to the right
Less than or equal to (<=)True if left operand is less than or equal to the right

Example:

```x = 8
y = 15
('x>y is',x>y)
```

Output: x > y is False

` print('x< y is',  x<y) `

Output: x < y is True

` print('x == y is', x==y) `

Output: x == y is False

` print('x != y is', x!=y) `

Output: x != y is True

` print('x >= y is', x>=y) `

Output: x >= y is False

` print('x<= y is', x<=y) `

Output: x <= y is True #### #3) Logical Operators

Logical operators are used for performing AND, OR and NOT operations. It either returns True or False according to the condition.

OperatorsDefinitions
andTrue if both the operands are true
orTrue if either of the operands is true
notTrue if operand is false

Example:

```a = True
b = False
print(‘a and b is’, a and b)
```

Output: a and b is False

` print(‘a or b is’, a or b) `

Output: a or b is True

` print(‘not a is’, not a) `

Output: not a is False #### #4) Bitwise Operators

Bitwise operators operate on bits and perform bit by bit operation.

OperatorsDefinitions
&Bitwise AND
|Bitwise OR
~Bitwise NOT
^Bitwise XOR
>> Bitwise right shift
<< Bitwise left shift

#### #5) Assignment Operator

An assignment operator is used to assign a value to a variable.

OperatorsDefinitionsOutput
=x = 15x = 15
+=x += 15x = x + 15
-=x -= 15x = x - 15
*=x *= 15x = x * 15
/=x /= 15x = x / 15
%=x %= 15x = x % 15
//=x //= 15x = x // 15
**=x **= 15x = x ** 15
&=x &= 15x = x & 15
|=x |= 15x = x | 15
^=x ^= 15x = x ^ 15
>>=x >>= 15x = x >> 15
<<=x <<= 15x = x << 15

#### #6) Identity Operators

Python offers 2 types of identity operators i.e is and is not.

Both are used to compare if two values are located on the same part of the memory. Two variables that are equal does not imply that they are identical.

OperatorsDefinitions
isTrue if the operands are identical
is notTrue if the operands are not identical

Example:

```a1 = 3
b1 = 3
a2 = "Python"
b2 = "Python"

a3 = [4,5,6]
b3 = [4,5,6]

print(a1 is not b1)

```

Output: False

` print(a2 is b2) `

Output: True

` print(a3 is b3) `

Output: False Here a3 and b3 are listed, interpreter allocates memory separately and even though they are equal, it returns False.

#### #7) Membership Operators

Python offers 2 types of membership operators i.e in and not in.

Both are used to test whether a value or variable is in a sequence.

OperatorsDefinitions
inTrue if value is found in the sequence

Example:

```a = “Python operators”
b = {1:'x',2:'y'}

print(“P” in a)
```

Output: True

` print(“python” not in a) `

Output: False

` print(1 in b) `

Output: True

` print('y' in b) `

Output: False 1 is key and ‘x’ is the value in dictionary b. Hence, ‘y’ in b returns False.

Hope you are clear about Python operators and their various types.

Watch out our upcoming to know more about Python Conditional Statements!!

PREV Tutorial | NEXT Tutorial