Operators are used to manipulate the values of the operands. Python supports the following types of operators. They are -
1. Arithmetic Operators - These operators are used to perform the mathematical operations like addition, subtraction, multiplication and so on.
Operators
|
Meaning
|
+
|
Add two operands (or) unary plus
|
-
|
Subtract right operand from the left (or) unary minus
|
*
|
Multiply two operands
|
/
|
Divide left operand by right one (result float)
|
%
|
Modulus-remainder of the division of left operand by right
|
//
|
Floor division- division that results into whole number adjusted
to the left in number line.
|
**
|
Exponent - left operand raised to
the power of right.
|
Example 1 -
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)
Output -
('x+y= ', 19)
('x-y= ', 11)
('x*y= ', 60)
('x/y= ', 3)
('x//y= ', 3)
('x**y= ', 50625)
Example 2 -
x=15.0
y=4
print 'x/y = ',x/y
print 'x//y = ',x//y
Output -
x/y = 3.75
x//y = 3.0
Example 3 -
x='rakesh'
y=4
print 'x*y = ',x*y
print 'y*x = ',y*x
Output -
x*y = rakeshrakeshrakeshrakesh
y*x = rakeshrakeshrakeshrakesh
2. Comparison / Relational Operators - Comparison operators are used to compare the values. Comparison operators returns either True/False
Operators
|
Meaning
|
>
|
Greater than - True if left operator is greater than right one
|
<
|
Less than - True if left operand is less than right one
|
==
|
Equal to - True if both operands are equal.
|
!=
|
Not equal to - True if both not equal
|
>=
|
Greater than (or) equal to - True if left operand is greater
than or equal to right.
|
<=
|
Less than (or) equal to - True if left operand is less than or
equal to right
|
Example 1 -
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
Output -
x>y is False
x<y is True
x==y is False
x!=y is True
x>=y is False
x<=y is True
Example 2 -
x='Rakesh'
y='Rakesh'
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
Output -
x>y is False
x<y is False
x==y is True
x!=y is False
x>=y is True
x<=y is True
3. Logical Operators - They are used to perform the logical operation like and, or, not. Logical operators returns either True/False values.
Operators
|
Meaning
|
and
|
True if both the operand are True
|
or
|
True if either of the operand is True
|
not
|
True if operand is false (complements the operand)
|
Example 1 -
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
Output -
x and y is False
x or y is True
not x is False
4. Bit-wise Operator - These operators are used to perform the operations on the values based on the bits.
Operators
|
Meaning
|
&
|
Bitwise AND
|
|
|
Bitwise OR
|
~
|
Bitwise NOT
|
^
|
Bitwise XOR
|
>>
|
Bitwise Right-shift
|
<<
|
Bitwise Left-shift
|
Example 1 -
x=10
y=4
print 'x&y : ',x&y
print 'x|y : ',x|y
print '~x : ',~x
print 'x^y : ',x^y
print 'x>>y : ',x>>y
print 'x<<y : ',x<<y
Output -
x&y : 0
x|y : 14
~x : -11
x^y : 14
x>>y : 0
x<<y : 160
Note : Bitwise operators internally converts the operands values in the form of binary format and performs the operation and gives the results in the form of decimal format.
5. Assignment Operators - It is used to assign the values to the variables. It contains both bitwise and arithmetic operators.
Operators
|
Example
|
Equivalent to
|
=
|
x=5
|
x=5
|
+=
|
x+=5
|
x=x+5
|
-=
|
x-=5
|
x=x-5
|
*=
|
x*=5
|
x=x*5
|
/=
|
x/=5
|
x=x/5
|
%=
|
x%=5
|
x=x%5
|
//=
|
x//=5
|
x=x//5
|
**=
|
x**=5
|
x=x**5
|
&=
|
x&=5
|
x=x&5
|
|=
|
x|=5
|
x=x|5
|
^=
|
x^=5
|
x=x^5
|
>>=
|
x>>=5
|
x=x>>5
|
<<=
|
x<<=5
|
x=x<<5
|
Example 1 -
x=10
print 'x=10 : ',x
x+=5
print 'x+=5 : ',x
x-=5
print 'x-=5 : ',x
x*=5
print 'x*=5 : ',x
x**=5
print 'x**=5 : ',x
x/=5
print 'x/=5 : ',x
x%=5
print 'x%=5 : ',x
x//=5
print 'x//=5 : ',x
y=20
y&=10
print 'y&=10 : ',y
y|=30
print 'y|=30 : ',y
y^=10
print 'y^=10 : ',y
y>>=3
print 'y>>=3 : ',y
y<<=3
print 'y<<=3 : ',y
Output -
x=10 : 10
x+=5 : 15
x-=5 : 10
x*=5 : 50
x**=5 : 312500000
x/=5 : 62500000
x%=5 : 0
x//=5 : 0
y&=10 : 0
y|=30 : 30
y^=10 : 20
y>>=3 : 2
y<<=3 : 16
No comments:
Post a Comment