# Types of Operators in Python with Examples

## What is an Operator in Python?

Operators in Python are special symbols that perform arithmetic or logical computations. The value that the operator works on is called the “operand.” This lesson will teach you about the many types of operators in Python, their syntax, and how to utilize them with examples.

## Different Types of Operators in Python

Here are the different types of operators in Python:

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

### Python Arithmetic Operators

Python Tutorial 7 - Operators
Python Tutorial 7 - Operators

Arithmetic operators are used in mathematics to accomplish operations such as addition, subtraction, multiplication, and so on.

Arithmetic Operator Example:

``````a = 15
b = 4

# Output: a + b = 19
print('a + b =',a+b)

# Output: a - b = 11
print('a - b =',a-b)

# Output: a * b = 60
print('a * b =',a*b)

# Output: a / b = 3.75
print('a / b =',a/b)

# Output: a // b = 3
print('a // b =',a//b)

# Output: a ** b = 50625
print('a ** b =',a**b)``````

Output:

``````a + b = 19
a - b = 11
a * b = 60
a / b = 3.75
a // b = 3
a ** b = 50625``````

### Python Logical Operators

The operators and, or, and not are logical operators.

Logical Operator Example:

``````a = True
b = False

print('a and b is',a and b)

print('a or b is',a or b)

print('not a is',not a)``````

Output:

``````a and b is False
a or b is True
not a is False``````

#### Truth Table for Logical Operator

The Truth Table for Logical Operators is shown below.

and, or, not

The logical operators in Python are and, or, and not. and will only produce a True result if both operands are True. Below is the truth table for and:

If any of the operands is True, or will produce True. Below is the truth table for or:

The truth value is inverted using the not operator. Below is the truth table for not:

Example:

``````>>> True and False
False
>>> True or False
True
>>> not False
True``````

### Comparison Operators in Python

Value comparison is done using comparison operators. Depending on the criteria, it returns True or False.

Comparison Operator Example:

``````a = 10
b = 12

# Output: a > b is False
print('a > b is',a>b)

# Output: a < b is True
print('a < b is',a<b)

# Output: a == b is False
print('a == b is',a==b)

# Output: a != b is True
print('a != b is',a!=b)

# Output: a >= b is False
print('a >= b is',a>=b)

# Output: a <= b is True
print('a <= b is',a<=b)``````

Output:

``````a > b is False
a < b is True
a == b is False
a != b is True
a >= b is False
a <= b is True
``````

### Python Bitwise Operators

Bitwise operators in Python operate bit by bit and operate on bits.

For example a = 60 and b = 13.

In this case, their values in binary form would be a = 0011 1100 and b = 0000 1101, respectively.

Bitwise Operator Example:

``````a = 60            # 60 = 0011 1100
b = 13            # 13 = 0000 1101
c = 0

c = a & b;        # 12 = 0000 1100
print ("Line 1 - Value of c is ", c)

c = a | b;        # 61 = 0011 1101
print ("Line 2 - Value of c is ", c)

c = a ^ b;        # 49 = 0011 0001
print ("Line 3 - Value of c is ", c)

c = ~a;           # -61 = 1100 0011
print ("Line 4 - Value of c is ", c)

c = a << 2;       # 240 = 1111 0000
print ("Line 5 - Value of c is ", c)

c = a >> 2;       # 15 = 0000 1111
print ("Line 6 - Value of c is ", c)``````

Output:

``````Line 1 - Value of c is  12
Line 2 - Value of c is  61
Line 3 - Value of c is  49
Line 4 - Value of c is  -61
Line 5 - Value of c is  240
Line 6 - Value of c is  15``````

### Assignment Operators in Python

In Python, assignment operators are used to give variable values.

Using the simple assignment operator a = 5, the variable a on the left is given the value 5 on the right.

There are many different types of compound operators in Python. For example, a += 5 adds 5 to a variable and then assigns it. It has the same meaning as a = a + 5.

Assignment Operator Example:

``````a = 25
b = 5
c = 0

c = a + b
print ("Line 1 - Value of c is ", c)

c += a
print ("Line 2 - Value of c is ", c)

c *= a
print ("Line 3 - Value of c is ", c)

c /= a
print ("Line 4 - Value of c is ", c)

c  = 2
c %= a
print ("Line 5 - Value of c is ", c)

c **= a
print ("Line 6 - Value of c is ", c)

c //= a
print ("Line 7 - Value of c is ", c)``````

Output:

``````Line 1 - Value of c is  30
Line 2 - Value of c is  55
Line 3 - Value of c is  1375
Line 4 - Value of c is  55.0
Line 5 - Value of c is  2
Line 6 - Value of c is  33554432
Line 7 - Value of c is  1342177``````

### Membership Operators

The membership operators in Python are in and not in. They are used to determine if a value or variable can be found in a sequence (string, list, tuple, set, and dictionary).

In a dictionary, we can only check if the key is there, not if the value is there.

Membership Operator Example:

``````a = 'Hello world'
b = {1:'a',2:'b'}

# Output: True
print('H' in a)

# Output: True
print('hello' not in a)

# Output: True
print(1 in b)

# Output: False
print('a' in b)``````

Output:

``````True
True
True
False``````

Here, ‘H’ is in a, but ‘hello’ is not in a. (remember, Python is case sensitive). In the same way, 1 is the key, and ‘a’ is the value in dictionary b. So, ‘a’ in b returns False.

### Identity Operators

In Python, the identity operators are is and is not. They are used to see if two values (or variables) are located on the same part of memory. Even if two variables are identical, this does not indicate that they represent the same object.

Identity Operator Example:

``````a1 = 5
b1 = 5
a2 = 'Hello'
b2 = 'Hello'
a3 = [1,2,3]
b3 = [1,2,3]

# Output: False
print(a1 is not b1)

# Output: True
print(a2 is b2)

# Output: False
print(a3 is b3)``````

Output:

``````False
True
False``````

Here, we can see that a1 and b1 are both integers with the same value. This means that they are both equal and the same. The same is true for a2 and b2 (strings).

But a3 and b3 are both lists. They are the same, but not the same because the interpreter puts them in different places in his or her memory, even though they are the same.

## Python Operators Precedence

The table below displays operator precedence from most to least important.

## Summary

In summary, Operators in Python are what make Python work. Operators can be used to do a lot of different things, like add two numbers or give a variable a value. And in this tutorial, we covered the types of operators in Python.

Now that we know what operators are, we are one step closer to writing better and more efficient codes. So, start using all the operators you’ve learned today in your code.

In the next post, we’ll learn how to use Python to make decisions based on things that can happen while a program is running.

## Inquiries

If you’re into machine learning you can visit our article about Top 40 Machine Learning Projects with Source Code.

Finally, if you have any questions or suggestions regarding the Types of Operators in Python, please do not hesitate to contact me. Simply leave a comment on this page or send me a message using the contact page of our website.