# Python Operators

Operators in Python are special symbols used to perform operations on values or variables. Arithmetic, Comparison, Logical, and Assignment operators are the most commonly used. However, there are other types as well.

This tutorial covers the different types of operators used in python along with examples to give you an idea of how and when to use them.

• Arithmetic Operators
• Comparison Operators
• Logical Operators
• Assignment Operators
• Identity Operators
• Membership Operators
• Bitwise Operators
• Operator Precedence

Arithmetic operators are used for mathematical computations like addition, subtraction, multiplication, etc. They generally operate on numerical values and return a numerical value. They’re also referred to as mathematical operators.

Example:

```x = 20
y = 10
print("x+y =", x+y)
# Subtraction Operator
print("x-y =", x-y)
# Multiplication Operator
print("x*y =", x*y)
# Division Operator
print("x/y =", x/y)
# Integer Division Operator
print("x//y =", x//y)
# Modulus Operator (Gives the remainder on division)
print("x%y =", x%y)
# Exponent Operator
print("x**y =", x%y)```

The above code gives the following output on execution:

```x+y = 30
x-y = 10
x*y = 200
x/y = 2.0
x//y = 2
x%y = 0
x**y = 10240000000000```

Comparison operators are used for comparing two values. As an output, they return a boolean value, either `True` or `False`.

Example:

```x = 20
y = 10
# Equal-To Operator
print("x==y =", x==y)
# Not-Equal-To Operator
print("x!=y =", x!=y)
# Greater-Than Operator
print("x&gt;y =", x&gt;y)
# Less-Than Operator
print("x&lt;y =", x&lt;y)
# Greater-Than-Equal-To Operator
print("x&gt;=y =", x&gt;=y)
# Less-Than-Equal-To Operator
print("x&lt;=y =", x&lt;=y)```

The above code gives the following output on execution:

```x==y = False
x=!y = True
x&gt;y = True
x&lt;y = False
x&gt;=y = True
x&lt;=y = False```

These operators are used to perform logical `and`, `or`, and `not` operations. They operate on boolean values and return a boolean value.

Example:

```x = True
y = False
# Logical and
print("x and y =", x and y)
# Logical or
print("x or y =", x or y)
# not operator
print("not x =", not x)
print("not y =", not y)```

The above code gives the following output on execution:

```x and y = False
x or y = True
not x = False
not y = True```

In python, the “equal to” symbol, `=` is used as the assignment operator. It’s used to assign values to variables. For example, `x = 2` will assign the value 2 to the variable x.
The assignment operator can be compounded with different arithmetic or bitwise operators, for example, `x += 2` is the same as `x = x + 2`

Identity operators are special operators in python which are used to compare two operands based on the memory location they refer to. `is` and `is not` are the two identity operators used in python.

Example 1:

```# With Integer Type: Integers are premitive data types
x = 2
y = 2

print("x is y =", x is y)
print("x is not y =", x is not y)

# Memory location assigned to x and y
print("Memory location of x =", id(x))
print("Memory location of y =", id(y))```

The above code gives the following output on execution:

```x is y = True
x is not y = False
Memory location of x = 94583763778912
Memory location of y = 94583763778912```

In the above example, we can see that for primitive data types such as Integer, variables with the same values have been assigned to the same memory location. However, this is not the case with reference data types like lists. Even if they store the same values, they’re not allocated to the same location. See example #2 below.

Example #2:

```# With lists: Lists are reference data types
x_ls = [1,2,3]
y_ls = [1,2,3]

print("x_ls is y_ls =", x_ls is y_ls)
print("x_ls is not y_ls =", x_ls is not y_ls)

# Memory location assigned to x and y
print("Memory location of x_ls =", id(x_ls))
print("Memory location of y_ls =", id(y_ls))```

The above code gives the following output on execution:

```x_ls is y_ls = False
x_ls is not y_ls = True
Memory location of x_ls = 140378530740416
Memory location of y_ls = 140378530742080```

Membership operators are also special operators in python which are used to determine whether a value or variable is present in a sequence (String, list, tuple, set, and dictionary). `in` and `not in` are the membership operators used in python.

Example:

```x = 2
y = [1, 2, 3]

# in operator
print("x in y =", x in y)
# not in operator
print("x not in y =", x not in y)```

The above code gives the following output on execution:

```x in y = True
x not in y = False```

Bitwise operators operate on the binary values of the operands bit-by-bit. For example, a Bitwise AND operation on 9(`0000 1001` in binary) and 3(`0000 0011` in binary), that is, 9 `&` 3 results in 1(`0000 0001`). Whereas, a Bitwise Or operation, 9 | 3 results in 11(`0000 1011`).

Example: Assuming x=9 and y=3, the following table illustrates examples for each of the Bitwise operator.

Example:

```# x = 9 which is 0001 0001 in binary
x = 9
# y = 3 which is 0000 0011 in binary
y = 3

# Bitwise AND
print("x&amp;y = ", x&amp;y)
# Bitwise OR
print("x|y = ", x|y)
# Bitwise Not
print("~x = ", ~x)
# Bitwise XOR
print("x^y = ", x^y)
# Bitwise Right Shift
print("x&gt;&gt;2 = ", x&gt;&gt;2)
# Bitwise Left Shift
print("x&lt;&lt;2 = ", x&lt;&lt;2)```

The above code gives the following output on execution:

```x&amp;y =  1
x|y =  11
~x =  -10
x^y =  10
x&gt;&gt;2 =  2
x&lt;&lt;2 =  36```

For more on Bitwise operators refer to Python’s wiki on Bitwise operators.

Operator precedence refers to the order in which operators are executed in expressions. The following table lists the precedence of different operators in python in descending order.

Parenthesis, `()`, have the highest precedence during expression evaluations. Operators having the same precedence are evaluated left to right. For more info, refer to python docs on operator precedence.

Example:

```x =  5 + 9 * (4 - 2) / 3**2
print(x)```

The above code gives the following output on execution:

`7.0`

In the above example, the expression inside the parenthesis (4 – 2) is evaluated first, followed by the exponent, 3**2this reduces the above expression to `5 + 9 * 2 / 9`. Since multiplication and division have the same precedence, the expression `9 * 2 / 9` is evaluated left to right resulting in `2.0`. Now, the expression is reduced to `5 + 2.0` which results in `7.0`.

In this tutorial, we covered the different types of operators used in python. If you found this article useful do give it a share! For more such articles subscribe to us.

If you’re a beginner looking to start your data science journey and learn python, check out our Python for Data Science Series.