Operators in Python
Operators are used to perform operations on variables and values.
Operators in Python are classified as :
◆ Arithmetic Operator
◆ Assignment Operator
◆ Comparison Operator
◆ Logical Operator
◆ Identity Operator
◆ Bitwise Operator
1. Arithmetic Operator:
Arithmetic operators are used with numeric values to perform common mathematical operations.
------------------------------------------------------------------------------------
Operator Name Example
------------------------------------------------------------------------------------
+ Addition X+Y
- Subtraction X-Y
* Multiplication X*Y
/ Division X/Y
% Modulus X%Y
** Exponential X**Y
// Floor Division X//Y
-------------------------------------------------------------------------------------
2.Assignment Operator:
Assignment operators are used to assign values to variables.
-------------------------------------------------------------------------------------
Operator Example EqualsTo
-------------------------------------------------------------------------------------
= 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
-------------------------------------------------------------------------------------
3.Comparison Operators
Comparison operators are used to compare two values.
------------------------------------------------------------------------------------
Operator Name Example
------------------------------------------------------------------------------------
== Equal x == y
!= Not equal x != y
> Greater than x > y
< Less than x < y
>= Greater than x >= y
or equal to
<= Less than x <= y
or equal to
--------------------------------------------------------------------------------------
4. Logical Operators
Logical operators are used to combine conditional statements.
------------------------------------------------------------------------------------
Operator Description
------------------------------------------------------------------------------------
and Returns
True if both statements
are true
or Returns
True if one
of the
statements
is true
not Reverse the
result,
returns
False if the
result is true
-----------------------------------------------------------------------------------
5. Python Identity Operators
Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location.
-------------------------------------------------------------------------------------
Operator Description
-------------------------------------------------------------------------------------
is Returns true if both variables are the same object
is not Returns true if both variables are not
the same object
------------------------------------------------------------------------------------
6.Python Bitwise Operators
Logical operators are used to combine conditional statements.
------------------------------------------------------------------------------------
Oprt 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 Shift left by left shift pushing zeros from the right and let the leftmost bits fall off
>> Signed right Shift right
shift by pushing copies of the leftmost bit in from the left, and let the rightmost
shift by pushing copies of the leftmost bit in from the left, and let the rightmost
bit fall off
-----------------------------------------------------------------------------------
Good job
ReplyDeleteI really like your blog. Thanks for the info.
ReplyDeletePython Online Training
Data Science Online Training