## Python Tutorial

Rated 5/5 based on 11 customer reviews

# Python - Operators

The term operator refers to a symbol (or sometimes a phrase of alphabets) which is predefined to perform a certain process such as addition, comparison etc. Each symbol requires one or more objects for the process to be performed. The objects are called operands and symbol itself is called operator. Most of the operators are binary in nature, in the sense they require two operands. Unary operator requires only one operand.

### Arithmetic operators

Everybody is familiar with arithmetic operators performing addition, subtraction, multiplication and division. Python has additionally modulus exponent and floor operators.

OperatorPurposeDescription
-SubtractionSubtracts right hand operand from operand on left.
*Multiplicationreturns Multiplication of values on either side of the operator.
/Divisionleft operand acts as numerator and right operand denominator for division
Modulusreturns remainder of division of left hand operand by right.
**ExponentCalculates value of operand raised to right.
a**3 is a raised to 3 i.e. a*a*a
//Floor DivisionThe division of operands where the result is the quotient in which the digits after the decimal point are removed. But division is negative, the result is floored, i.e., rounded away from zero (towards negative infinity).
In :
a,b=21,10
a+b
Out:
31
In :
#subtraction
a-b
Out:
11
In :
#multiplication
a*b
Out:
210
In :
#division
a/b
Out:
2.1
In :
#modulus
a%b
Out:
1
In :
#floor of positive division
a//b
Out:
2
In :
#floor of negative division
-a//b
Out:
-3

### In-place assignment operators

These operators allow any arithmetic operation and assignment in one step. Result of corresponding arithmetic operation of two operands is assigned back to left operand.

symbolpurposeDescription
=assignmentAssigns values from right side operands to left side operand
+=Add AND assignadds right operand to the left and assign result to left operand
-=Subtract AND assignsubtracts right operand from left and assign the result to left operand
*=Multiply AND assignmultiplies right operand with left and assign result to left operand
/=Divide AND assigndivides left operand with right and assign result to left operand
%=Modulus AND assignassigns modulus of two operands and assigns result to left operand
**=Exponent AND assignPerforms exponentiation of left operand by right  and assign result to the left operand
//=Floor Division and assignIt performs floor division on operators and assign value to the left operand
In :
a,b=21,10
a+=b
a
Out:
31
In :
a,b=21,10
#subtract and assign
a-=b
a
Out:
11
In :
a,b=21,10
#multiply and assign
a*=b
a
Out:
210
In :
a,b=21,10
#divide and assign
a/=b
a
Out:
2.1
In :
a,b=21,10
#modulus and assign
a%=b
a
Out:
1
In :
a,b=21,10
#exponent and assign
a**=b
a
Out:
16679880978201
In :
a,b=21,10
#floor and assign
a//=b
a
Out:
2

### Logical operators

In order to compare two objects for equality or to decide whether one is greater than other etc. the logical comparison operators are used. Primarily used with numeric objects, they can very well be used with other Python objects such as string, list or tuple.

SymbolpurposeDescription
==equalsreturns true if both operands are equal false otherwise
!=not equal toreturns true if both operands are not equal false otherwise
>greater thanreturns true if left operand is greater than right operand, otherwise false
<less thanreturns true if left operand is less than right operand, otherwise false
>=greater than or equal toreturns true if left operand is greater or equal to right operand, otherwise false
<=less than or equal toreturns true if left operand is less than or equal to right operand, otherwise false
In :
a=21
b=10
# equals
a==b
Out:
False
In :
#not equal to
a!=b
Out:
True
In :
#greater than
a>b
Out:
True
In :
#less than
a<b
Out:
False
In :
#greater than or equal
a>=b
Out:
True
In :
#less than or equal to
a<=b
Out:
False

### Sequence operators

This category of operators is common to all sequence data type i.e. string, list and tuple. All of them use zero based index to access items in them. Hence operators for indexing and slicing have been commonly defined.

symbolpurposeDescription
+ConcatenationAppends second sequence to first
*Repetitionconcatenates multiple copies of the same sequence
[]SliceGives the item at given index
[ : ]Range Slicefetches item in range specified by two index operands separated by : symbol.
If first operand is omitted, range starts at zero index
If second operand is omitted, range goes upto end of sequence
inMembershipReturns true if a item exists in the given sequence
not inMembershipReturns true if a item does not exist in the given sequence
In :
s1,s2="Hello","World"
l1,l2=[1,2,3],[4,5,6]
t1,t2=(1,2,3),(4,5,6)
In :
#concatenation
print (s1+s2)
print (l1+l2)
print (t1+t2)
HelloWorld
[1, 2, 3, 4, 5, 6]
(1, 2, 3, 4, 5, 6)
In :
#repetition
print (s1*3)
print (l1*3)
print (t1*3)
HelloHelloHello
[1, 2, 3, 1, 2, 3, 1, 2, 3]
(1, 2, 3, 1, 2, 3, 1, 2, 3)
In :
#slice
print(s2)
print (l2)
print (t2)
r
6
6
In :
s1='Hello World'
l1=[1, 2, 3, 4, 5, 6]
t1=(1, 2, 3, 4, 5, 6)
In :
#range slice of string
print (s1[2:5])
print (s1[:3])
print (s1[4:])
llo
Hel
o World
In :
#range slice on list
print (l1[2:4])
print (l1[:3])
print (l1[3:])
[3, 4]
[1, 2, 3]
[4, 5, 6]
In :
#range slice on tuple
print (t1[2:4])
print (t1[:3])
print (t1[3:])
(3, 4)
(1, 2, 3)
(4, 5, 6)
In :
s1='Hello World'
l1=[1, 2, 3, 4, 5, 6]
t1=(1, 2, 3, 4, 5, 6)
In :
'He' in s1
Out:
True
In :
'W' not in s1
Out:
False
In :
10 in l1
Out:
False
In :
100 not in l1
Out:
True
In :
10/2 in t1
Out:
True
In :
10/3 not in t1
Out:
True

### Set operators

These operators are specifically designed for performing operations on set data type as defined in set theory of Mathematics.

purposedescription
UnionUnion of two sets is a set of all elements in both.
IntersectionIntersection of two sets is a set containing elements common to both
DifferenceDifference of two sets results in a set containing elements only in first but not in second set.
Symmetric differenceResult of Symmetric difference is a set consisting of elements in both sets excluding common element
In :
set1,set2={10,20,30,40,50},{20,40,60}
In :
#union
set3=set1|set2
set3
Out:
{10, 20, 30, 40, 50, 60}
In :
#intersection
set3=set1&set2
set3
Out:
{20, 40}
In :
#difference
set3=set1-set2
set3
Out:
{10, 30, 50}
In :
#symmetric difference
set3=set1^set2
set3
Out:
{10, 30, 50, 60}

### Identity operators

These operators are not really symbols but phrases. Each Python object is allocated required space in computer memory. Address of memory location is obtained by built-in id() function. Python variable is really reference name given to this location. More than one variables can refer to same location. Following identity operators tell us if id() values of both objects is same or different.

OperatorDescription
isreturns true if both operands point to the same object and false otherwise.
is notreturns true if both operands don’t point to the same object and false otherwise.
In :
a=10
b=a
print (id(a), id(b))
a is b
1860398432 1860398432
Out:
True
In :
a=10
b=a*2
print (id(a), id(b))
a is b
1860398432 1860398752
Out:
False

### Bitwise operators

Any object in memory is represented in binary bits. The bitwise representation of object is manipulated by following operators. In following illustration, built-in bin() function is used to verify result of operations. The bin() function returns the binary equivalent of integer object. For example:

a=45 and bin(45) is 00101101

b=25 and bin(25) is 00011001

Following table lists the bitwise operators.

symbolOperatorDescription
&Binary AND

0&0=0

0&1=0

1&0=0

1&1=1

|Binary OR

0|0=0

0|1=1

1|0=1

1|1=1

^Binary XOR

0^0=0

0^1=1

1^0=1

1^1=0

~Binary Ones ComplementIt is unary operator that flips bits.
<<Binary Left Shiftbits moved to left by specified places and adds trailing 0s
>>Binary Right Shiftbits moved to right by specified places and adds leading 0s
In :
a,b=45,25
bin(a),bin(b)
Out:
('0b101101', '0b11001')
In :
#binary and
c=a&b
c, bin(c)
Out:
(9, '0b1001')
In :
#binary or
c=a|b
c, bin(c)
Out:
(61, '0b111101')
In :
#binary xor
c=a^b
c, bin(c)
Out:
(52, '0b110100')
In :
#binary complement
c=~a
c, bin(c)
Out:
(-46, '-0b101110')
In :
#left shift
c=a<<2
c, bin(c)
Out:
(180, '0b10110100')
In :
#right shift
c=a>>2
c, bin(c)
Out:
(11, '0b1011')

Just to verify bitwise & operation on a and b by using truth table for & operator

a = 45 = 00101101
b = 25 = 00011001
-----------------------
a&b     = 00001001 = 9

## Swift Tutorial

### By KnowledgeHut

Introduction to Swift Tutorial
Rated 5/5 based on 44 customer reviews
13542
Swift Tutorial

Introduction to Swift Tutorial

## R Programming Tutorial

### By KnowledgeHut

R Programming
Rated 5/5 based on 19 customer reviews

## C# Tutorial

### By KnowledgeHut

C# is an object-oriented programming developed by Microsoft that uses the .Net Framework. It utilizes the Common Language Interface (CLI) that describes the executable code as well as the runtime environment. C# can be used for various applications such as web applications, distributed applications, database applications, window applications etc.For greater understanding of this tutorial, a basic knowledge of object-oriented languages such as C++, Java etc. would be beneficial.
Rated 5/5 based on 12 customer reviews
9127
C# Tutorial

C# is an object-oriented programming developed by Microsoft that uses ...