Operators
What is
an operator ?
Simple answer can be given
using expression 4+5. Here “4”and “5” are
called operands and “+” is called operator. Python
language supports
following type of operators,
· Arithmetic
Operators
· Comparison
(ie Relational) Operators
· Assignment
Operators
· Logical
Operators
· Bitwise
Operators
· Membership
Operators
· Identity
Operators
Python Arithmetic Operators :-
Addition (+): Adds values in either side of operator
Ex:- Assume a=5
and b=8,
a + b = 13
Now we consider,
How we perform this expression in python
Subtraction (-): Subtract values in either side of operator
Ex:- Assume a=5
and b=8,
a - b = -3
Now we consider,
How we perform this expression in python
Multiplication (*): Multiplies values on either side of the operator
Ex:- Assume a=5
and b=8,
a * b = 40
Now we consider,
How we perform this expression in python
Division (/): Divides left hand operand by right hand operand
Ex:- Assume a=16
and b=8,
a / b = 2
Now we consider,
How we perform this expression in python
Modulus (%): Divides left hand operand by right hand operand and returns remainder
Ex:- Assume a=16 and b=8,
a % b = 0
Now we consider,
How we perform this expression in python
Exponent (**): Performs exponential (power) calculation on operators
Ex:- Assume a=2 and b=3,
a ** b = 8
Now we consider,
How we perform this expression in python
Floor Division (//): The division of operands where the result is the quotient in which the digits after the decimal point are removed.
Ex:- Assume a=25.0 and b=2.0,
a / b = 12.5
but,
a // b =12.0
Now we consider,
How we perform this expression in python
Python Comparison Operators :-
( == ) : Checks if the value of two operands
are equal or not, if yes then condition becomes true.
Ex:- (i) Assume a=5 and b=8,
a==b
result : False
(ii) Assume a=5 and b=5,
a==b
result : True
Now we consider,
How we perform this expression in python
( != ) : Checks if the value of two operands are equal or not, if values are not equal then condition becomes true.
Ex:- (i) Assume a=5 and b=8,
a!=b
result : True
(ii) Assume a=5 and b=5,
a!=b
result : False
Now we consider,
How we perform this expression in python
( <> ) : Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. This is similar to “ != ” operator.
Ex:- (i) Assume a=5 and b=8,
a<>b
result : True
(ii) Assume a=5 and b=5,
a<>b
result : False
Now we consider,
How we perform this
expression in python
( > ) : Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.
Ex:- (i) Assume a=5 and
b=8,
a>b
result : False
(ii) Assume a=10 and b=5,
a>b
result : True
Now we consider,
How we perform this
expression in python
( < ) : Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.
( >= ) : Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.
Ex:- (i) Assume a=5 and
b=8,
a>=b
result : False
(ii) Assume a=5 and b=5,
a>=b
result : True
(iii) Assume a=10 and b=5,
a>=b
result : True
Now we consider,
How we perform this
expression in python
( <= ) : Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.
Python Assignment Operators :-
( = ) : Simple assignment operator, Assigns
values from right side operands to left side operand
Ex:- Assume a=5 and b=3
c
= a + b
now, c = 8
( += ) : Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand. c += a is equivalent to c = c + a
Ex:- Assume a=5
and b=3
a
= a +b
Now,
a=8
Or
a
+= b
Now,
a=8
( -= ) : c -= a is equivalent to c = c – a
( /= ) : c /= a is equivalent to c = c / a
( %= )
: c %= a
is equivalent to c = c % a
( **= )
: c **= a
is equivalent to c = c ** a
( //= )
: c //= a
is equivalent to c = c // a
Python Bitwise Operators :-
Bitwise operator works on
bits and perform bit by bit operation.
Assume if a = 60; and b
= 13;
Now in
binary format
they will be
as follows:
a = 0011 1100
b = 0000 1101
( & ) : Binary AND Operator copies a bit to
the result if it exists in both operands.
a & b =
0000 1100
Decimal
value = 12
( | ) : Binary OR Operator copies a bit if it exists in either operand.
a | b = 0011
1101
Decimal
value = 61
( ^ ) : Binary XOR Operator copies the bit if it is set in one operand but not both.
a ^ b = 0011
0001
Decimal
value = 49
( ~ ) : Binary Ones Complement Operator is unary and has the efect of 'flipping' bits.
~a = 1100
0011
Decimal
value = -61
( << ) : Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.
a<<2 =
1111 0000
Decimal
value = 240
( >> ) : Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.
a>>2 =
0000 1111
Decimal
value = 15
No comments:
Post a Comment