Operator is a symbol that performs certain operations.
Python provides the following set of operations.
1. Arithmetic Operators
2. Relational Operators or Comparison Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Special Operators
+ ===> Addition Operator
-===> Subtraction Operator
* ===> Multiplication Operator
/===> Division Operator
%===> Modulo Operator
// ===> Floor Division Operator
** ===> Exponent Operator or Power Operator
print(‘a + b=’ a+ b)
print(‘a - b=’ a - b)
print(‘a* b=’ a *b)
print(‘a / b=’ a / b)
print(‘a // b=’ a // b)
print(‘a % b=’ a % b)
a + b= 12
a – b = 8
a * b = 20
a / b = 5.0
a // b = 5
a % b =0
Note: / operator always perform floating point arithmetic. Hence it will always returns float value.
But Floor division ( //) can perform both floating point and integral arithmetic. If arguments are int type then the result is int type. If one argument is float type then the result is float type.
Equality Operators: ==, !=
Logical Operators :
and, or, not
For boolean types behaviour:
and===> if boh arguments are True then only result is True
or===> If atleast one argument is True then result is True
True and False ====> False
True or False ====> True
Not False ===> True
Fornon- boolean types behaviour:
0 means False
Non-zero means True
Empty string is always treated as false
X and y:
===> if x is evaluates to a false return x otherwise return y
10 and 20
0 and 20
If first argument is zero then he result is zero otherwise result is y.
X or y : ===> if x is evaluates to a True hen result is x otherwise result is y
10 or 20 ====>10
0 or 20 ====> 20
We can apply these operators bitwise. These operators are applicable only for int and Boolean types. By mistake if we are trying to apply for any other type then we will get Error.
Bitwise operators are :
If both bits are 1 then only result is 1 otherwise result is 0
If atleast one bit is 1 then only result is 1 otherwise result is 0
If bits are different then only result is 1 otherwise result is 0
Bitwise complement operator i.e 1 means 0 and o means 1
Bitwise Right shift operator
Bitwise Left shift operator
Python defines the following 2 special operators.
- Identity Operators
- Membership Operators
we can use identity operators for address comparison. 2 identity operators are available.
- Is not
r1 is r2 return true if both r1 and r2 are pointing to the same object.
r1 is not r2 return true if both r1 and r2 are not pointing to the same object.
Note: we can use is operator address comparison where as == operator for content comparison.
Membership Operators :
We can use Membership Operators to check whether the given object present in the given collection. ( it may be String, List, Set, Tuple or Dict).
In ---> Returns True if the given object present in the specified Collection.
Not in ---à Returns True if the given object not present in the specified Collection.