# Python Operators

Operators in Python works same as Mathematics, that is operators such as +, -, * etc. used to perform operations. Now these are the two questions arises in our mind:

1. What operations are going to perform ?
2. Where the operations are performed ?

The answer of these two questions are:

1. It is decided by the operator used.
2. On variables and values.

For example, the following program shows the addition operation is performed (using +) on two variables, and the subtraction operation is performed (using -) on two values:

```a = 20
b = 10
c = a + b
print(c)

res = 100 - 50
print(res)```

This program produces the output like shown in the snapshot given below:

As you can see, the addition operation is performed on a and b, and whatever the result comes out, gets initialized to c variable. And in second case, the subtraction operation is performed directly on two values, that are 100 and 50, and again whatever the result value comes out, gets initialized to res variable.

Note - For Operator Precedence and operator Module, refer to its separate tutorial.

## Types of Operators

Operators that are used in Python, divided into 7 categories as listed here:

1. Arithmetic Operators
2. Assignment Operators
3. Comparison Operators
4. Logical Operators
5. Bitwise Operators
6. Identity Operators
7. Membership Operators

Now let's discuss about all these operators in brief with well designed example programs for each category.

## Arithmetic Operators

To perform arithmetical operations in Python program, we have arithmetic operators. The table given below listed all the arithmetic operators available along with syntax to use, and example. The example is given with variable's value as, a = 5 and b = 3:

Operator Name Syntax Output
+ Addition c = a + b c = 8
- Subtraction c = a - b c = 2
* Multiplication c = a * b c = 15
/ Division c = a / b c = 1.6666666666666667
% Modulus c = a % b c = 2
** Exponentiation c = a ** b c = 125
// Floor Division c = a // b c = 1

Important - Difference between / (Division) and // (Floor Division) operator is, division operator divides and return the exact division result value. Whereas the floor division operator divides and then takes the floor value of result value. For example, 5/3 gives 1.6666666666666667, whereas 5//3 gives 1. Here the floor value indicates to an integer value (a whole number) which is less than or equal to the original result.

Note - The ** operator when applies to a variable or a value, for example a ** b, it gets treated as ab. Therefore, 5 ** 3 gets treated as 53, that gives 125 as its result.

The program given below uses all the arithmetic operators. This program is created to clear all your remaining doubt (if any):

```a = 5
b = 3
c = a + b
print(a, "+", b, "=", c)
c = a - b
print(a, "-", b, "=", c)
c = a * b
print(a, "*", b, "=", c)
c = a / b
print(a, "/", b, "=", c)
c = a % b
print(a, "%", b, "=", c)
c = a ** b
print(a, "*", b, "=", c)
c = a // b
print(a, "//", b, "=", c)```

The snapshot given below shows sample output produced by this program:

## Assignment Operators

To assign (set) or re-assign the new value to a variable in Python programming, we have another category of operator named assignment operator. The table given below listed all those operators used for assigning purposes along with its syntax and example.

Operator Name Syntax Output
= Simple Assign b = a If a = 5
Then, b = 5
+= Add then Assign b += a If a = 5, b = 0
Then, b = 5
-= Subtract then Assign b -= a If a = 5, b = 12
Then, b = 7
*= Multiply then Assign b *= a If a = 5, b = 2
Then, b = 10
/= Divide then Assign b /= a If a = 5, b = 14
Then, b = 2.8
%= Modulo then Assign b %= a If a = 5, b = 27
Then, b = 2
//= Floor Divide then Assign b //= a If a = 5, b = 14
Then, b = 2
**= Exponent then Assign b **= a If a = 5, b = 2
Then, b = 32
&= Bitwise AND then Assign b &= a If a = 10, b = 4
Then, b = 0
|= Bitwise OR then Assign b |= a If a = 10, b = 4
Then, b = 14
^= Bitwise XOR then Assign b ^= a If a = 10, b = 4
Then, b = 14
>>= Bitwise Right Shift then Assign b >>= a If a = 1, b = 10
Then, b = 5
<<= Bitwise Left Shift then Assign b <<= a If a = 1, b = 10
Then, b = 20

The program given below implements all the above operators. This program is created to show you the things in better way to understand about assignment operators:

```a = 5
b = a
print("Simple Assign Result:", b)

a = 5
b = 0
b += a
print("Add then Assign Result:", b)

a = 5
b = 12
b -= a
print("Subtract then Assign Result:", b)

a = 5
b = 2
b *= a
print("Multiply then Assign Result:", b)

a = 5
b = 14
b /= a
print("Divide then Assign Result:", b)

a = 5
b = 27
b %= a
print("Modulo then Assign Result:", b)

a = 5
b = 14
b //= a
print("Floor Divide then Assign Result:", b)

a = 5
b = 2
b **= a
print("Exponent then Assign Result:", b)

a = 10
b = 4
b &= a
print("Bitwise AND then Assign Result:", b)

a = 10
b = 4
b |= a
print("Bitwise OR then Assign Result:", b)

a = 10
b = 4
b ^= a
print("Bitwise XOR then Assign Result:", b)

a = 1
b = 10
b >>= a
print("Bitwise Right Shift then Assign Result:", b)

a = 1
b = 10
b <<= a
print("Bitwise Left Shift then Assign Result:", b)```

Here is the output produced by this program of assignment operators in Python:

In assignment operator, the expression:

`b += a`

is basically the short version of, or same as:

`b = b + a`

## Comparison Operators

To compare two variable or directly values in Python programming, we've comparison operators. These operators are used to compare any two entity. The table given below shows all the comparison operators along with syntax to use, and its example. The example taken with two variables namely a and b. The value of these two variables are a = 12 and b = 7:

Operator Name Syntax Output
== Equal a == b False
!= Not equal a != b True
> Greater than a > b True
< Less than a < b False
>= Greater than or equal to a >= b True
<= Less than or equal to a <= b False

The program given below uses all these comparison operators. Since the comparison operators are used to compare two values. Therefore, on printing the comparison result, we'll only get the output in the form of True or False. For example, the statement, a < b returns True, if the value of a is less than b, otherwise False gets returned. Let's have a look at the example given below:

```a = 12
b = 7
print("\n---------", a, "==", b, "?--------")
print(a == b)
print("\n---------", a, "!=", b, "?--------")
print(a != b)
print("\n---------", a, ">", b, "?--------")
print(a > b)
print("\n---------", a, "<", b, "?--------")
print(a < b)
print("\n---------", a, ">=", b, "?--------")
print(a >= b)
print("\n---------", a, "<=", b, "?--------")
print(a <= b)```

Here is the output produced:

## Logical Operators

To connect two or more expressions and evaluate its logical output, then we've another operator category named logical operator. The table given below shows all the logical operators along with its syntax and output. The initial value of three variables namely a, b, and c are a=12, b=7, and c=9:

Operator Syntax Returns
and a > b and a < c False
or a > b or a < c True
not not(a > b) False

Now the example given below uses all these three logical operators:

```a = 12
b = 7
c = 9
print(a > b and a < c)
print(a > b or a < c)
print(not(a > b))```

And here is the output produced:

## Bitwise Operators

This section includes all the Bitwise operators that can be used in Python language. To learn how all these Bitwise operators perform operations, refer Bitwise Operators with Example to learn everything about Bitwise operators in very brief. For now, the table given below shows all Bitwise operators along with syntax and output. The example taken with two variables namely a and b, whose values are supposed to be a = 10 and b=3:

Operator Name Syntax Output
& Bitwise AND a & b 2
| Bitwise OR a | b 11
^ Bitwise XOR a ^ b 9
˜ Bitwise NOT ˜a -11
<< Bitwise Left Shift a << b 1
>> Bitwise Right Shift a >> b 80

The example given below uses all these Bitwise operators:

```a = 10
b = 3
print(a, "&", b, "=", a & b)
print(a, "|", b, "=", a | b)
print(a, "^", b, "=", a ^ b)
print("˜", a, "=", ˜a)
print(a, ">>", b, "=", a >> b)
print(a, "<<", b, "=", a << b)```

The output produced by this program is:

## Identity Operators

To determine whether a variable or a value is of certain type or class or not in Python programming, we've another category of operators called Identity operator. That is basically used to identify variables or values. The table given below listed identity operators that can be used in Python along with syntax and example. The example is taken with a and b variables whose values are a=[1, 2, 3], b=[4, 5]:

Operator Syntax Returns
is a is b False
is not a is not b True

The example given below implements these two identity operators available in Python:

```a = [1, 2, 3]
b = [4, 5]
c = a
print(a is b)
print(a is c)
print(a is not b)
print(a is not c)```

The snapshot given below shows the sample output produced by this Python program:

## Membership Operators

This is the last category of operator used in Python. This category named membership operators are basically used to check whether a specific value is a member of any specific list, string, tuple or not. The table given below listed membership operators with syntax, example, and output. The example taken with a variable named a whose initial value is a = [1, 2, 3]:

Operator Syntax Example Output
in element in a 2 in a True
not in element not in a 4 in a False

Let's have a look at the example given below to understand about the above two membership operators in better way:

```a = [1, 2, 3]
print(2 in a)
print(4 in a)
print(2 not in a)
print(4 not in a)```

The sample output produced by this program is shown in the snapshot given below:

Let's have a look at another example, that shows the famous use of membership operator (in operator) in Python programming:

```a = ["python", "operators", "tutorial"]
print("Enter an element to check for existence in list: ")
elem = input()
if elem in a:
print("\nIt is available")
else:
print("\nIt is not available")```

And here is the sample output produced by this program, after supplying python as input:

#### More Examples

Here are list of some more examples that uses operators in Python:

Python Online Test

« Previous Tutorial Next Tutorial »

Like/Share Us on Facebook 😋