Python Operators
Other than Keywords, Data Constants and Variables in any Programming
Language, we can use few symbols or special characters called as Operators.
Operators in general are used to perform certain operations on values and
variables. These are standard symbols used for the purpose of logical and
arithmetic operations. We will look into different types of operators available
in Python. Every Operator takes on 2 or more Operands.
Operators: are the special symbols .
Eg: +,-,*,/,=,etc.
Operand: It is the value on which the operator is
applied with.
for Example
print(10+5)
This adds the 2 values 10 and 5 and displays the Result as 15.
Python classifies the operators in the following groups:
1.
Arithmetic operators
2.
Assignment operators
3.
comparison operators
4.
Logical operators
5.
Identity operators
6.
Membership operators
7.
Bitwise operators
8.
Special operators
Arithmetic Operators
Arithmetic operators are used with Numeric values to perform common
Mathematical Operations:
|
Operator |
Name |
Usage Example |
|
+ |
Addition |
x+y |
|
- |
Subtraction |
x-y |
|
* |
Multiplication |
x*y |
|
/ |
Division |
x/y |
|
% |
Modulus |
x%y |
|
** |
Exponentiation |
x**y |
|
// |
Floor division |
x//y |
Operator Precedence:
As in Mathematics , these Operators when occurring in an Expression,
the evaluation is totally based on Operator precedence, which is as below.
·
P- Parentheses
·
E- Exponentiation
·
M- Multiplication (Multiplication and division have
the same precedence)
·
D- Division
·
A- Addition (Addition and Subtraction have the same
precedence)
·
S- Subtraction
The modulus operator helps us to extract the last digits of a number .
i.e. the remainder part of a Division. For example:
x % 10 -> yields the last digits
x % 100 -> yield last two digits
Example : Arithmetic Operators in Python
This following example illustrates the usage syntax of the Arithmetic
Operators.
a=9
b=4
#Addition of
numbers
add=a+b
print(add)
Output: 13
#Subtraction
of numbers
sub=a-b
print(sub)
Output: 5
#Multiplication
of number
mul=a*b
print(mul)
Output: 36
#Division
(float) of number
div1=a/b
print(div1)
Output: 2.25
#Division
(floor) of number
div2=a//b
print(div2)
Output: 2
#Modulo of
both number
mod=a % b
print(mod)
Output: 1
#Power
p=a**b
print(p)
Output: 6561
Assignment Operators
Assignment operators are used to assign values to variables. These
Operators are used in Assignment Statements. In Python we have multiple type of
Assignment Operators, they are
|
Operator |
Example |
Same As |
|
= |
x=5 |
x=5 |
|
+= |
x+=3 |
x=x+3 |
|
-= |
x-=3 |
x=x-3 |
|
*= |
x*=3 |
x=x*3 |
|
/= |
x/=3 |
x=x/3 |
|
%= |
x%=3 |
x=x%3 |
|
//= |
x//=3 |
x=x//3 |
|
**= |
x**=3 |
x=x**3 |
|
&= |
x&=3 |
x=x&3 |
|
|= |
x|=3 |
x=x|3 |
|
^= |
x^=3 |
x=x^3 |
#Examples of Assignment Operators
#Assign value
a=10
#Assign a
variable
b=a
print(b)
Output: 10
#Add and
assign value
b+=a
print(b)
Output: 20
#Subtract and
assign value
b-=a
print(b)
Output: 10
#Multiply and
assign
b*=a
print(b)
Output: 100
#bitwise left
shift operator
b<<=a
Output: 102400
Comparison Operators
Comparison operators are used to compare two values, Mostly used in
condition Checking of Control Statements. The Return value is Boolean.
|
Operator |
Name |
Example |
|
== |
Equal |
x==y |
|
!= |
Not Equal |
x!=y |
|
> |
Greater than |
x>y |
|
< |
Less than |
x<y |
|
>= |
Greater than or equal to |
x>=y |
|
<= |
Less than or equal to |
x<=y |
Note: = is an assignment
operator and ==comparison operator.
#Example of Relational Operators
a=13
b=33
#a>b is
False
print(a>b)
Output: False
#a<b is
True
print(a<b)
Output: True
#a==b is False
print(a==b)
Output: False
#a!=b is True
print(a!=b)
Output: True
#a>=b is
False
print(a>=b)
Output: False
#a<=b is
True
print(a<=b)
Output: True
Logical Operators
Logical operators are used to combine 2 or more Conditional statements
or Relational Expressions. Since the Operands are Boolean, the return value is
always Boolean only.
#Examples of Logical Operator
a=True
b=False
#Print a and b
is False
print(a and b)
Output: False
#Print a or b
is True
print(a or b)
Output: True
#Print not a
is False
print(not a)
Output: False
Identity Operators
Identity Operators are used to compare the objects, The comparison is
not for whether they are equal, but for whether they are actually the same
object, with the same memory location. In python only one instance of any
object is created irrespective of multiple variables has the same value
assigned to them , and that will be available till the end of the program or
object being destroyed. Any variable which is assigned this value will share
the same object only.
|
Operator |
Description |
Example |
|
is not |
Returns True if both variables are the same object |
x is y |
|
is not |
Returns True if both variables are not the same object |
x is not y |
#Example: Identity Operator
a=10
b=20
c=a
print(a is not
b)
Output: True
print(a is c)
Output: True
Membership operators
Membership operators are used to test if a value is present in a
sequence object or not, These operators in Python are normally used with
sequence Data Classes like List, Tuple, Set, Dictionary etc.,.
|
Operator |
Description |
Example |
|
in |
Returns True if a sequence with the specified value is present in the
object |
x in y |
|
not in |
Returns True if a sequence with the specified value is not present in
the object. |
x not in y |
#Example: Membership Operator
#Python program to
illustrate not 'in' operator
x=24
y=20
list=[10,20,30,40,50]
if (x not in
list):
print("x is NOT present in given
list")
else:
print("X is present in given
list")
if (y not in
list):
print("y is NOT present in given
list")
else:
print("y is present in given
list")
Output
x is NOT
present in given list
y is present
in given list
Bitwise
Operators
Bitwise operators are used
to perform certain operations on Binary numbers. These Operators are not used
in Application Programming, but are needed in System program development.
|
Operator |
Name |
Description |
|
& |
AND |
Sets each bit to 1 if both
bits are 1 |
|
| |
OR |
Sets each bit to 1 if one
of two bits is 1 |
|
^ |
XOR |
Sets each bit to 1 if only
one of two bits is 1 |
|
~ |
NOT |
Inverts all the bits |
|
<< |
Zero fill left shift |
Shift left by pushing
zeros in from the right and let the leftmost bits fall off. |
|
>> |
Signed Right shift |
Shift right by pushing
copies of the leftmost bit in from the left, and let the rightmost bits fall
off. |
When we use these Operators,
the Numeric operands are converted to its binary form and the Operation is
performed.
#Examples of
Bitwise operators
a=10 00001010
b=4 00000100
#Print bitwise AND operation
print(a & b)
Output: 2
#Print bitwise OR operation
print(a | b)
Output: 11
#Print bitwise NOT operation
print(~a)
Output: -11
#Print bitwise XOR operation
print(a ^ b)
Output: 14
#Print bitwise right shift operation
print(a>>2)
Output: 2
#Print bitwise left shift operation
print(a<<2)
Output: 40
No comments:
Post a Comment