# Python Programming (Basics)- Operators in Python

## Operators in Python

Operator is a symbol used in various computations performed on operands.
For example:
>>>20+3
here 20 and 3 are operands and ‘+’ is operator.

### Types of Operators in Python

Depending upon the number of operands and required computations ,operators are divided in following major types:
1. Arithmetic Operator
2. Assignment Operator
3. Comparison/Relational Operator
4. Bit-wise Operator
5. Logical Operator
6. Membership Operator
7. Identity Operator

#### Arithmetic Operator

Arithmetic operators are used for mathematical calculations like addition, subtraction, modulus, division etc.

OperatorDescriptionExamples
+It adds value on either sides of operator or can be used as unary operatotra=8
b=2
a+b=10
-It subtracts value of right operand from left operand or it can be used as unary minusa=8
b=2
a-b=6
*Multiplication of two operands a=8
b=2
a*b=16
/Divides left operand with right operanda=8
b=2
a/b=4.0
// (Floor operator)This is used to carry out floor division ie. removes decimal valuesa=8
b=2
a//b=4
%(modulus)It is used to find remainder of divisiona=8
b=2
a%b=0
**(exponent)It is used to perform exponential calculation on operators(ie, left operand raised to the power of right)a=8
b=2
a**b=64

#### Assignment Operator

‘=’ sign is the basic assignment operator used to assign values to variables.
In addition ,Python has a set of short hand assignment operators as follows:

OperatorDescriptionExampleEquivalent to
=It is used to assign values of right side operands to left side>>>a=5
>>>a
5
>>>b='itvoyagers"
>>>b
itvoyagers
a=5
b="itvoyagers"
+=It is used to add values of right side operands to left side operand and the result is assigned to left operandx+=4x=x+4
-=It is used to subtract values of right side operands from left side operand and the result is assigned to left operandx-=4x=x-4
*=It is used to multiply values of both operands and the result is assigned to left operandx*=4x=x*4
/=It is used to divide values of left side operands by right side operand and the result is assigned to left operandx/=4x=x/5
%=It is used to divide values of left side operands by right side operand and the resulting reminder is assigned to left operandx%=4x=x%4
**=It performs Exponential calculation and result is assigned to left operandx**=4x=x**4
//=It is used to divide values of left side operands by right side operand and the result is assigned to left operand by removing decimal placesx//=4x=x//4

Note : Other operator like logical , bit-wise etc can also be used as short hand assignment operator.

#### Comparison/Relational Operator

It is used to compare operands.It is also called as Relational operator.
It returns Boolean values as output (True or False).

OperatorDescriptionExample

== (equal to)
This returns true if both operands are true otherwise false>>> a=8
>>> b=2
>>> c=8
>>> a==b
False
>>> a==c
True
> (greater than)This returns True if left operand is greater than right operand otherwise false>>> a=8
>>> b=2
>>> c=1
>>> a>b
True
>>> c>b
False
< (less than)This returns True if left operand is less than right operand otherwise false>>> a=8
>>> b=2
>>> c=10
>>> a False
>>> a True
>= (greater than or equal to)This returns True if left operand is greater than or equal to right operand otherwise false>>> a=5
>>> b=3
>>> c=5
>>> a>=c
True
>>> b>=a
False
>>> a>=b
True
<= (less than or equal to)This returns True if left operand is less than or equal to right operand otherwise false>>> a=8
>>> b=2
>>> c=2
>>> a<=b
False
>>> b<=c
True
>>> c<=a
True
!= (not equal to)This returns True when operands are not equal otherwise false>>> a=8
>>> b=2
>>> c=8
>>> a!=b
True
>>> a!=c
False

#### Bitwise Operator

It is used to perform operation on bit.
It performs bit by bit operations.

OperatorNameDescriptionExample
&Binary ANDOperator copies a bit to the result if it exists in both the operands.
i.e. returns 1 when both operands are 1
and 0 otherwise
>>> w=5
>>> a=6
>>> w & a
4
>>> bin(a)
'0b110'
>>> bin(w)
'0b101'
>>> bin(4)
'0b100'
>>>
|Binary OROperator copies a bit to the result if it exists in any of the operands.
i.e. returns 1 when either of operand is 1
and 0 otherwise
>>> w=5
>>> a=6
>>> w | a
7
>>> bin(a)
'0b110'
>>> bin(w)
'0b101'
>>> bin(7)
'0b111'
^Binary XORIt copies the bit if it is set in one operand but not in both.
i.e.returns 1 if one operand is 1 and other is 0
and 0 otherwise
>>> w=5
>>> a=6
>>> w ^ a
3
>>> bin(a)
'0b110'
>>> bin(w)
'0b101'
>>> bin(3)
'0b11'
~Binary Ones ComplementIt is unary operator and has effect of flipping bits>>> w=5
>>> bin(w)
'0b101'
>>> ~w
-6
>>> bin(-6)
'-0b110'
<<Binary Left ShiftThe left operand value is moved to left by the number of bits specified in the right operand>>> w=5
>>> w << 2
20
>>> bin(20)
'0b10100'
>>Binary Right ShiftThe left operand value is moved to right by the number of bits specified in the right operand>>> w=5
>>> w >> 2
1
>>> bin(1)
'0b1'

#### Logical Operator

This operator is used to perform logical operations used logical and, or and not.These operators are case sensitive.
It returns Boolean values as output (True or False).

Operator DescriptionExample
andLogical AND returns True if both operands are True.>>> p=100
>>> q=500
>>> r=700
>>> q>p and q>r
False
>>> q>p and r>q
True
orLogical OR returns True if either of the operands is True.>>> p=100
>>> q=500
>>> r=700
>>> p>q or q>r
False
>>> q>p or q>r
True
notLogical NOT is used to reverse the state of operand.>>> a=''
>>> not a
True
>>> b=10
>>> not b
False
>>> not q>p
False

#### Membership Operator

This operator is used to check if given variable is a part of specified sequence or not.
This operator (in and not in) can be used with strings, lists etc
It returns Boolean values as output (True or False).

OperatorsDescriptionExample
inIt returns True if it finds variable in specified sequence , False otherwise>>> 'I' in 'INDIA'
True
>>> 'i' in 'INDIA'
False
not inIt returns True if it does not find variable in specified sequence, False otherwise>>> 'V' not in 'ITVoyagers'
False
>>> 'v' not in 'ITVoyagers'
True

#### Identity Operator

This operator is used to compare memory location of two objects.
id() is used to show memory location of any object.
This operator (is and is not) can be used compare memory location of two objects.
It returns Boolean values as output (True or False).

OperatorsDescriptionExample
isIt returns True if the variable on both sides of operator are pointing to same object ,False otherwise>>> f=88
>>> o=100
>>> g=88
>>> id(f)
1872877376
>>> id(g)
1872877376
>>> id(o)
1872877568
>>> f is o
False
>>> f is g
True
is notIt returns True if the variable on both sides of operator are pointing to different object ,False otherwise>>> f=88
>>> o=100
>>> g=88
>>> id(f)
1872877376
>>> id(g)
1872877376
>>> id(o)
1872877568
>>> f is not o
True
>>> f is not g
False Loading… Taking too long? Reload document
| Open in new tab