Operators in Python

Operators

Operators are used to perform an operation.

Type of operators are listed below.
  • Arithmetic Operators
  • Comparison (Relational) Operators
  • Assignment Operators
  • Logical Operators
  • Bitwise Operators
  • Membership Operators
  • Identity Operators
Learn operators in detail one by one.
 
Arithmetic Operators
 
(assume, a=10 and b=20)

Operator
Description Example
+ Addition Adds values a + b = 30
- Subtraction Subtracts values   a – b = -10
* Multiplication Multiplies values a * b = 200
/ Division Divides values b / a = 2
% Modulus Divides values and returns remainder b % a = 0
** Exponent Performs exponential (power) calculation on operators a**b =10 to the power 20
// Floor Division - The division of operands where the result is the quotient in which the digits after the decimal point are removed. 9//2 = 4 and 9.0//2.0 = 4.0

Comparison (Relational) Operators

Comparison operators compares the value and returns Boolean value , according to the condition.

Operator
Description
Example
== If the values of two operands are equal, then the condition becomes true.   (a == b) is not true.  
!=  If values of two operands are not equal, then condition becomes true.
(a != b) is true.
<>  If values of two operands are not equal, then condition becomes true.   (a <> b) is true. This is similar to != operator.  
>
If the value of left operand is greater than the value of right operand, then condition becomes true.
(a > b) is not true.  
<
If the value of left operand is less than the value of right operand, then condition becomes true.

(a < b) is true.  
>=
 If the value of left operand is greater than or equal to the value of right operand, then condition becomes true  (a >= b) is not true.
<=
If the value of left operand is less than or equal to the value of right operand, then condition becomes true.

 (a <= b) is true

Bitwise Operators

Bitwise operator performing operation on bits (binary numbers)
 
Assume a=60 and b=13

Binary of a and b are,

a=0011 1100
b=0000 1101

Operator Description  Example 
& (Binary AND) Operator copies a bit to the result if it exists in both operands  a&b = 0000 1100
| (Binary OR)
 It copies a bit if it exists in either operand.  a|b = 0011 1101
^ (Binary XOR)
It copies the bit if it is set in one operand but not both   a^b = 0011 0001
~ (Binary Ones Complement)
 It is unary and has the effect of 'flipping' bits.  ~a = 1100 0011
<< (Binary Left Shift)   The left operands value is moved left by the number of bits specified by the right operand
a << = 240 (means 1111 0000) 
>> (Binary Right Shift)  The left operands value is moved right by the number of bits specified by the right operand
a >> = 15 (means 0000 1111) 

Assignment Operators

Operator
Description   Example
 =  Assigns values from right side operands to left side operand  
c = a + b assigns value of a + b into

Membership Operators

Operator
 Description  Example
In   return true if variable find in sequence otherwise return false  x in y, here in results in a 1 if x is a member of sequence y
not in  return false if variable find in sequence otherwise return true 
x not in y, here not in results in a 1 if x is not a member of sequence y.
 

Operators Precedence

Operator Description 
** Exponentiation (raise to the power)
~ + - complement, unary plus and minus (method names for the last two are +@ and -@)
* / % // Multiply, divide, modulo and floor division
+ - Addition and subtraction
>> << Right and left bitwise shift 
& Bitwise 'AND'
^ | Bitwise exclusive `OR' and regular `OR' 
<= < > >=
Comparison operators
<> == !=
Equality operators 
= %= /= //= -= += *= **=  Assignment operators
is is not Identity operators 
in not in  Membership operators 
not or and Logical operators

Summary

In this chapter, you learnt type of operator in Python.