# Python Operators

## Python Operators

In this section we will learn the different types of operators used in Python and their use with examples:

An operator is a special symbol that is used to carry out certain operations such as arithmetic or logic operations. The variables on which these operations are performed are called operands. Consider the following example:

CODE

>>> 4 + 9

OUTPUT

13

In the above example, + is the operator that is being performed on the two operands that is 4 and 9. And through the operation we got the output 13. In Python there are a number of operators that include:

1. Arithmetic Operators
2. Relational Operators
3. Bitwise Operators
4. Assignment Operators
5. Special Operators

### Arithmetic Operators

Arithmetic operator is a symbol that performs mathematical operation on data. The Python programming language provides many arithmetic operators. Following is a list of all operators used in Python:

 Operation Symbol Description Addition + Adds two values Subtraction – Subtracts one value from other Multiplication * Multiplies two values Division / Divides one values by other Modulus % Gives the remainder of division of two integers Floor Division // It results in a new number that will be on the left of the number line. Exponent ** It expresses the power of left variable to the power of right variable.

Consider the following example, in which all the arithmetic operators are used:

CODE

>>> a = 5

>>> b = 2

>>> print (‘a + b = ‘, a+b)

>>> print (‘a – b = ‘, a-b)

>>> print (‘a * b = ‘, a*b)

>>> print (‘a / b = ‘, a/b)

>>> print (‘a // b = ‘, a//b)

>>> print (‘a ** b = ‘, a**b)

OUTPUT

a + b =  7

a – b =  3

a * b =  10

a / b =  2.5

a // b =  2

a ** b =  25

### Relational Operators or Comparison Operators

The relational operators are used to specify conditions in programs. A relational operator compares two values. It produces result as true or false. The relational operators are sometimes called comparison operators as they test that are either true or false.

The Python programming language provides the following six basic relational operators:

 Operator Description > Greater than operator returns false if the value on right side of > is less than the value on the left side. Otherwise returns true. < Less than operator returns true if the value on left side of < is less than the value on right side. Otherwise returns false. == Equal to operator returns true if the values on both side of == are equal. Otherwise returns false. >= Greater than or equal to returns false if value on the right side of >= is less than the value on the left side. Otherwise returns true. <= Less than or equal to returns true if value on the left side of <= is less than the value on the right side. Otherwise returns false. != The not equal to operator returns true if the value on the left side of != is not equal to the value on the right side. Otherwise returns false.

Consider the following example, in which all the relational operators are used:

CODE

>>> a = 5

>>> b = 2

>>> print (‘a > b = ‘, a>b)

>>> print (‘a < b = ‘, a<b)

>>> print (‘a == b = ‘, a==b)

>>> print (‘a != b = ‘, a!=b)

>>> print (‘a >= b = ‘, a>=b)

>>> print (‘a <= b = ‘, a<=b)

OUTPUT

a > b =  True

a < b =  False

a == b =  False

a != b =  True

a >= b =  True

a <= b =  False

### Logical Operators

The logical operators are used to evaluate compound conditions. There are three logical operators in Python programming language:

1. AND Operator (&&)
2. OR Operator (||)
3. NOT Operator (!)

AND Operator

The symbol for AND operator is (and). It is used to evaluate two conditions. It produces true result if both conditions are true. It produces false result if any condition is false.

 Condition 1 Operator Condition 2 Result False and False False False and True False True and False False True and True True

The truth table of and operator is given below:

 A B A and B True True True True False False False True False False False False

OR Operator

The symbol used for OR operator is (or). It is used to evaluate two conditions. It gives true result if either condition is true. It gives false if either condition is false.

 Condition 1 Operator Condition 2 Result False or False False False or True True True or False True True or True True

The truth table of or operator is given below:

 A B A or B True True True True False True False True True False False False

Not Operator

The symbol for not operator is (not). It is used to reverse the result of a condition.

 Operator Condition Result not True False not False True

The truth table of not operator is given below:

 A not A True False False True

Consider the following example in which all the logical operators are used:

CODE

>>> a = True

>>> b = False

>>> print (‘a and b is ‘, a and b)

>>> print (‘a or b is ‘, a or b)

>>> print (‘not of x is ‘, not x)

>>> print (‘not of y is ‘, not y)

OUTPUT

a and b is  False

a or b is  True

not of x is  False

not of y is  False

### Bitwise Operators

The bitwise operators are like the strings of binary digits and act the same on the operands. The name indicates that the bitwise operator operates bit by bit. For example 3 is 11 in binary and 2 is 10 in binary.

Consider the following table in which there is operator its description and an example for each operator:

Suppose that a = 4 in binary 9 is (0000 0100) and suppose b = 10 in binary 10 is (0000 1010).

 Operator Description Use & BITWISE AND takes two binary digits and performs logical and operation by multiplying the operands. a & b = 0 (0000 0000) | BITWISE OR takes two binary digits and performs logical or operation by adding the operands. a | b = 14 (0000 1110) ~ BITWISE NOT takes one binary digit and performs logical not operation by negating the operand. ~ b = -11 (1111 0101) ^ BITWISE XOR takes two binary digits and performs logical exclusive or operation on the operands. a ^ b = 14 (0000 1110) >> BITWISE right shift right shifts the bits of the expression. b >> 2 = 2 (0000 0010) << BITWISE left shift left shifts the bits of the expression. b >> 2 = 42 (0010 1000)

### Assignment Operators

The assignment (=) is used in assignment statement to assign a value or computational result to a variable.

The name of the variable is written on the left side of the operator and the value is written on the right side of the operator. A statement that assigns a value to a variable is called assignment statement.

SYNTAX

Variable = expression;

EXAMPLES

A = 100;

C = A + B;

In Python, there are many compound assignment statements that add, multiply or perform any other operation on the variable and then store the resulting value in that variable.

Consider the following example in which we used a compound assignment statement to add 4 to the variable x and the expression is storing the resulting value to that variable:

EXAMPLE

x + = 4

In this expression the number 4 is added to the previous value of x and the resulting value is stored in x. The above expression is equal to the following expression:

x = x + 4

Consider the following table in which various compound assignment operators or statements are shown:

 Operator Use Equal to = y = 2 y = 2 += y += 2 y = y + 2 -= y -= 2 y = y – 2 *= y *= 2 y = y * 2 /= y /= 2 y = y / 2 %= y %= 2 y = y % 2 //= y //= 2 y = y // 2 **= y **= 2 y = y ** 2 &= y &= 2 y = y & 2 |= y |= 2 y = y | 2 ^= y ^= 2 y = y ^ 2 >>= y >>= 2 y = y >> 2 <<= y <<= 2 y = y << 2

### Special Operators in Python

In Python there are some operators that have a special meaning in the programming language such as identity operator and membership operator. The identity and membership operators are described below:

Identity Operators

In Python the identity operators are is and is not. These two operators are used to check that if two values or two variables are stored in the same part of the memory or not. The two values that seems to be equal or that are equal are not identical. The “is” and “is not” operators are described below:

IS Operator

The “is” operator returns a True when the two values or two variable r two operands are identical that is when they are referring to a same object. For example y is True.

IS NOT Operator

The “is not” operator returns a True when the two values or two variable r two operands are not identical that is when they are not referring to a same object. For example y is not True.

Consider the following example in which we have used is and is not operators to show that the two variables are identical or not:

CODE

>>> a = 2

>>> b = 2

>>> c = ‘Python’

>>> d = ‘Python’

>>> e = [1, 2, 3]

>>> f = [1, 2, 3]

>>> print (a is b)

>>> print (c is not d)

>>> print (e is f)

OUTPUT

True

False

False In the above example, a and b are equal and identical as well because they have the same value that is an integer. c and d are also identical and equal because they have same value which is a string. e and f are equal but are not identical because they are lists and as the list can be changed therefore the Python interpreter stores both the variables to different locations and hence are not identical.

Membership Operators

In Python the membership operators are “in” and “not in”. The “in” and “not in” operators are used to check that if the value or a variable is found in a sequence or not. A sequence includes string, list, tuple, set and dictionary. In the dictionary sequence the presence of only key can be checked.

The “in” and “not in” operators are described below:

IN Operator

The “in” operator returns a True if the entered value or variable is found in the sequence for example 2 in a.

NOT IN Operator

The “not in” operator returns a True if the entered value or variable is not found in the sequence for example 2 not in a.

Consider the following example in which we have declared a dictionary and a string and if the entered value or variable is found then interpreter will give the corresponding result regarding the operators that will be used. In case of dictionary keys will be used.

CODE

>>> z = ‘Python Programming’

>>> a = {5: ‘w’, 6: ‘b’}

>>> print (‘g’ in z)

>>> print (‘Python’ not in z)

>>> print (6 in a)

>>> print (‘w’ in a)

OUTPUT

True

False

True

False In the above example, the character g is in the string and hence the interpreter returned true. If you had written G instead of g then the interpreter would returned false as Python is a case sensitive language.