Introduction

Operators are special symbols in Python that carry out arithmetic or logical computation. The value that the operator operates on is called the operand. Operator falls into below categories:

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

 

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication etc.

  • Addition(+)
  • Subtraction(-)
  • Multiplication(*)
  • Division(/) : Division results in a floating-point value.
  • Floor Division(//) : Divides and returns the integer value of the quotient.
  • Exponentiation(**) : Raises the first number to the power of the second.
  • Modulus(%) : Divides and returns the value of the remainder.
  • Unary Plus (+)
  • Unary Minus (-) : Opposite the sign of value
x = 15
y = 4

print('x + y =',x+y)
print('x - y =',x-y)
print('x * y =',x*y)
print('x / y =',x/y)
print('x // y =',x//y)
print('x ** y =',x**y)
print(+x)
print(-x)

# Output
x + y = 19
x - y = 11
x * y = 60
x / y = 3.75
x // y = 3
x ** y = 50625
15
-15

 

Comparison Operators

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

  • Less than(<)
  • Greater than(>)
  • Less than or equal to(<=)
  • Greater than or equal to(>=)
  • Equal to(= =)
  • Not equal to(!=)
x = 10
y = 12

print('x > y  is',x>y)
print('x < y  is',x<y)
print('x == y is',x==y)
print('x != y is',x!=y)
print('x >= y is',x>=y)
print('x <= y is',x<=y)

 

Assignment Operator

Assigns a value to the expression on the left.

  • Assign(=)
  • Add and Assign(+=)
  • Subtract and Assign(-=)
  • Divide and Assign(/=)
  • Multiply and Assign(*=)
  • Modulus and Assign(%=)
  • Exponent and Assign(**=)
  • Floor-Divide and Assign(//=)
a = 7
a+=2
a-=2
a/=7
a*=8
a%=3
a**=5
a//=3

 

Logical Operator

These are conjunctions that can be used to combine more than one condition.

  • and : If the conditions on both the sides of the operator are true, then the expression as a whole is true.
  • or : The expression is false only if both the statements around the operator are false. Otherwise, it is true.
  • not : This inverts the Boolean value of an expression.

A zero value is false. A non-zero value is true. An empty string is false. A non-empty string is true. None keyword is always false.

x = True
y = False

print('x and y is',x and y)
print('x or y is',x or y)
print('not x is',not x)

val1 = 0
val2 = 4
bool(None)
bool(val1)
not val1

print(val1 or val2)

# Output
x and y is False
x or y is True
not x is False
4

 

Membership Operator

These operators test whether a value is a member of a sequence. The sequence may be a list, a string, or a tuple.

  • in : Checks if a value is a member of a sequence.
  • not in : Checks if a value is not a member of a sequence.
pets=['dog','cat','ferret']

print('fox' in pets)
print ('pot' not in pets)

# Output
False
True

 

Identity Operator

These operators test if the two operands share an identity.

  • is : If two operands have the same identity, it returns True. Otherwise, it returns False.
  • is not : If two operands does not have the same identity, it returns True. Otherwise, it returns False.
# 1 
print (2 is 20)
print ('2' is "2")
print (2 is not '2')

# Output
False
True
True


# 2
x = 1001
y = 1000

y += 1

ptint(x == y)
ptint (x is y)

# Output
True
False

 

In #2, x and y both refer to objects whose value is 1001. They are equal. But they do not reference the same object, as you can verify:

id(x)
id(y)

 

Bitwise operators

Bitwise operators act on operands as if they were string of binary digits. It operates bit by bit, hence the name.

  • Binary AND(&)
  • Binary OR(|)
  • Binary XOR(^)
  • Binary One’s Complement(~)
  • Binary Left-Shift(<<)
  • Binary Right-Shift(>>)

 

print(2&3)
print(2|3)
print(2^3)
print(~-3)
print(2<<2)
print(3>>2)

# Output
2
3
1
2
8
0

 

Operator Precedence

All operators that the language supports are assigned a precedence. In an expression, all operators of highest precedence are performed first. Once those results are obtained, operators of the next highest precedence are performed. So it continues, until the expression is fully evaluated. Any operators of equal precedence are performed in left-to-right order. Here is the order of precedence of the Python operators you have seen so far, from lowest to highest:

OperatorDescription
orBoolean OR
andBoolean AND
notBoolean NOT
==, !=, <, <=, >, >=, is, is notcomparisons, identity
|bitwise OR
^bitwise XOR
&bitwise AND
<<, >>bit shifts
+, -addition, subtraction
*, /, //, %multiplication, division, floor division, modulo
+x, -x, ~xunary positive, unary negation, bitwise negation
**exponentiation

 

Operators at the top of the table have the lowest precedence, and those at the bottom of the table have the highest. Any operators in the same row of the table have equal precedence. Operator precedence can be overridden using parentheses. Expressions in parentheses are always performed first, before expressions that are not parenthesized.