Python Operators:
Definition:
Operators
are used to perform operations on values and variables.
Types:
Python
Operators are divided into 7 categories:
·
Python Arithmetic Operator
·
Python Relational Operator
·
Python Assignment Operator
·
Python Logical Operator
·
Python Membership Operator
·
Python Identity Operator
·
Python Bitwise Operator
- Python Arithmetic Operator:
Arithmetic
operators are used to perform mathematical operations like addition,
subtraction, multiplication and division.
Operator Name Example
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division a / b
% Modulus a % b
** Exponentiation a ** b
// Floor division a // b
Example:
a=9
b=3
print(a
+ b)
Output:
>>>12
Likewise,
you can try other arithmetic operators (a - b, a * b, a / b, etc.)
Relational
operators compares the values. It either returns True or False according to the
condition.
Operator Name Example
> Greater than a > b
< Less than a
< b
>= Greater than or equal
to a >= b
<= Less than or equal to a <= b
== Equal to a
== b
!= Not equal to a != b
Example:
a = 4
b = 5
print(a
> b)
Output:
>>>False
Likewise, you can try other relational operators (a < b, a==b, a!=b, etc.)
3. Python Assignment Operator:
Assignment
operators are used to assign values to the variables.
Operator Example Conventional form
= a = 5 a = 5
+= a += 3 a = a + 3
-= a -= 3 a = a - 3
*= a *= 3 a = a * 3
/= a /= 3 a = a / 3
%= a %= 3 a = a % 3
//= a //= 3 a = a // 3
**= a **= 3 a = a ** 3
&= a &= 3 a = a & 3
|= a |= 3 a = a | 3
^= a ^= 3 a = a ^ 3
>>= a >>= 3 a = a >> 3
<<= a <<= 3 a = a << 3
Example:
a = 4
b = 5
a+=b
print(a)
Output:
>>>9
Likewise, you can try other assignment operators (a-=b, a*=b, a/=b, etc.)
4.Python Logical Operator:
Logical
operators perform Logical AND, Logical OR and Logical NOT operations.
Operator Meaning Example
and True if both the
operands are true a
and b
or True if either of
the operands is true a
or b
not True if operand is
false not
a
Example:
a =
True
b =
False
print(a
and b)
print(a
or b)
print(not
a)
Output:
>>>False
>>>True
>>>False
5. Python Membership Operator:
in and
not in are the membership operators, used to test whether a value or variable
is in a sequence.
Operator Meaning Example
in True if
value/variable is found in 5 in a
the
sequence
not in True if value/variable is
not found 5 not
in a
in
the sequence
Example:
a = 3
b = 9
list =
[1, 2, 3, 4, 5 ];
if ( a
in list ):
print("a is
present in given list")
else:
print("a is
not present in given list")
if ( b
not in list ):
print("b
is not present in given list")
else:
print("b is
present in given list")
Output:
>>>
a is present in given list
>>> b is not present in given list
6.Python Identity Operator:
is and
is not are the identity operators both are used to check if two values are
located on the same part of the memory. Two variables that are equal does not imply that they are identical.
Operator Meaning Example
is Returns True if both
variables are a
is b
the
same object
is not Returns True if both
variables are a
is not b
not
the same object
Example:
a = 20
b = 20
if ( a
is b ):
print("a
& b same identity")
b=30
if ( a
is not b ):
print("a
& b have different identity")
Output:
>>>
a & b same identity
>>> a & b have different identity
7.Python Bitwise Operator:
Bitwise
operators acts on bits and performs bit by bit operation.
Operator Name Meaning
& 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
the rightmost bits fall off
Example:
a=7
b=4
print(a
& b)
Output:
>>>4
Likewise,
you can try other bitwise operators (a | b, a ^ b, a ~ b, etc.)
Comments
Post a Comment