Operator: It is a symbol that performs some action. Python supports following types of operators:
- Arithmetic operators : + , – , * , / , // , % , **
- Relational/Comparison operators: < , <= , > , >= , == , !=
- Logical operators: and , or , not
- Assignment operators: =
- Augmented Assignment Operators/ Short hands : +=, -=, *=, /=, %=, //=, **=, |=, &=, >>=, <<= and ^=
- Identity operators: is , is not
- Membership operators: in , not in
- Bitwise operators: &, | ,~ , ^ , >> , <<
Arithmetic operators : Python supports 7 arithmetic operators
1) + : To perform addition
2) – : To perform subtraction
3) * : To perform multiplication
4) / : To perform division
5) // : To perform integer division ie returns greatest integer less than or equal to result of division operator
6) % : To find remainder. Remainder is calculated as : a % b = a – (a//b) * b
7) ** : To find power of a number/base to desired number/exponent ie ab
Arithmetic operators are used in daily life and all of us know about their working.
Examples to demonstrate arithmetic operators are:
Symbol | Name | Purpose |
+ | addition | To perform addition |
– | subtraction | To perform subtraction |
* | multiplication | To perform multiplication |
/ | division | To perform division |
// | integer division | To perform integer division ie returns greatest integer less than or equal to result of division operator |
% | remainder | To find remainder. Remainder is calculated as : a % b = a – (a//b) * b |
** | power | To find power of a number/base to desired number/exponent ie ab |
>>> a = 10 >>> b = 3 >>> a + b 13 >>> a - b 7 >>> b - a -7 >>> a * b 30 >>># this is same as expected in math >>> a / b 3.3333333333333335 >>> -a/b -3.3333333333333335 >>> a/-b -3.3333333333333335 >>> -a/-b 3.3333333333333335 >>>#this is also same as expected in math >>> b / a 0.3 >>> b/-a -0.3 >>> -b/a -0.3 >>> -b/-a 0.3 >>>#a/b = 3.3333333333333335 and greatest integer less than 3.3333333333333335 is 3 >>> a // b 3 >>># -a/b = -3.3333333333333335 and greatest integer less than -3.3333333333333335 is -4 >>> -a // b -4 >>># a/-b = -3.3333333333333335 and greatest integer less than -3.3333333333333335 is -4 >>> a // -b -4 >>>#-a/-b = 3.3333333333333335 and greatest integer less than 3.3333333333333335 is 3 >>> -a // -b 3 >>>#b/a = 0.3 and greatest integer less than 0.3 is 0 >>> b // a 0 >>>#-b/a = -0.3 and greatest integer less than -0.3 is -1 >>> -b // a -1 >>>#b/-a = -0.3 and greatest integer less than -0.3 is -1 >>> b // -a -1 >>>#-b/-a = 0.3 and greatest integer less than 0.3 is 0 >>> -b // -a 0 >>>#a % b = a - (a//b) * b >>>#10 % 3 = 10 - (10//3)*3 = 10 - 3 * 3 = 1 >>> a % b 1 >>>#-10 % 3 = -10 - (-10//3)*3 = -10 - (-4) * 3 = -10 + 12 = 2 >>> -a % b 2 >>>#10 % -3 = 10 - (10//-3)*(-3) = 10 - (-4) * (-3) = 10 - 12 = -2 >>> a % b -2 >>>#-10 % -3 = -10 - (-10//-3)*(-3) = -10 - 3 * (-3) = -10 + 9 = -1 >>> -a % -b -1 >>># 3 % 10 = 3 - (3//10)*10 = 3 - 0 * 10 = 3 - 0 = 3 >>> b % a 3 >>># -3 % 10 = -3 - (-3//10)*10 = -3 - (-1) * 10 = -3 + 10 = 7 >>> -b % a 7 >>># 3 % -10 = 3 - (3//-10)* (-10) = 3 - (-1) * (-10) = 3 - 10 = -7 >>> b % -a -7 >>># -3 % -10 = -3 - (-3//-10)* (-10) = -3 - (0) * (-10) = -3 + 0 = -3 >>> -b % -a -3 >>># 103=1000 >>> a ** b 1000 >>># 310 = 59049 >>> b ** a 59049
Relational/Comparison operators: There are 6 relational operators to compare two values.
< : Less than
<= : Less than or equal to
> : greater than
>= : greater than or equal to
== : equal to
!= : not equal to
Symbol | Name | Purpose |
< | Less than | a<b Returns True if a is less than b, False otherwise |
<= | Less than or equal to | a<=b Returns True if a is less than or equal to b, False otherwise |
> | greater than | a>b Returns True if a is greater than b, False otherwise |
>= | greater than or equal to | a>=b Returns True if a is greater than or equal to b, False otherwise |
== | equal to | a==b Returns True if a is equal to b, False otherwise |
!= | not equal to | a!=b Returns True if a is not equal to b, False otherwise |
>>> a=10 >>> b=3 >>> c=3 >>> a<b False >>> b<a True >>> b<c False >>> b<=a True >>> b<=c True >>> b<c False >>> a>b True >>> b>a False >>> b>c False >>> b>=a False >>> b>=c True >>> b>c False >>> a==b False >>> b==c True >>> a !=b True >>> b!=c False
Logical operators: These are used to join two or more conditions. There are 3 logical operators.
1) and
2) or
3) not
Name | Purpose |
and | Returns True, if all the joining conditions are True, returns False otherwise |
or | Returns True, if at least one or more of the joining conditions are True, returns False otherwise |
not | Returns True, if the Result is False and Returns False if the result is True |
>>> a=5 >>> b=4 >>> c=3 >>> a>b and b>c True >>> a>b and c>b False >>> a>b or c>b True >>> a<b or c>b False >>> a>b True >>> not a>b False >>> b>a False >>> not b>a True
Assignment operators = : It is used to assign a value or the result of an expression to a variable.
>>> a=8 >>> a 8 >>> b=a+4- 2*3 >>> b 6
Augmented Assignment Operators/ Short hands : It combines the functioning of the arithmetic or bitwise operators with the assignment operator. It is a way to write expressions in a shorter from, so these are also called short hands. Python supports various short hands to write the expressions as
a = a (operator) value/expression can be written as a (operator) = value/expression
Example:
a = a + 5 is equivalent to a += 5
a = a + b*c is equivalent to a + = b*c
Python supports following augmented operators/short hands
1) +=
2) -=
3) *=
4) /=
5) %=
6) //=
7) **=
8) |=
9) &=
10) >>=
11) <<=
12) ^=
Identity operators: These 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.
There are 2 identity operators in python
1) is : returns True, if both objects are identical, returns False otherwise.
2) is not: returns True, if both objects are not identical, returns False otherwise.
>>> a=3 >>> b=3 >>> c=5 >>> a is b True >>> a is c False >>> a is not b False >>> a is not c True
Membership operators: In Python, there are two membership operators. (in, not in), which are used to test whether or not the value is present in the data sequence (list/tuple/string/set/dictionary).
1) in : returns True if item is present in sequence, returns False otherwise
2) not in : returns True if item is not present in sequence, returns False otherwise
>>> list1=[2,3,5] >>> 2 in list1 True >>> 4 in list1 False >>> 2 not in list1 False >>> 4 not in list1 True >>> tuple1=(2,3,5) >>> 2 in tuple1 True >>> 4 in tuple1 False >>> 2 not in tuple1 False >>> 4 not in tuple1 True >>> dict1={1:"garg",5:"academy"} >>> 1 in dict1 True >>> 1 not in dict1 False >>> string1="gargs academy" >>> "gargs" in string1 True >>> "Guptas" in string1 False >>> "gargs" not in string1 False >>> "Guptas" not in string1 True >>> set1={2,3,4} >>> 2 in set1 True >>> 5 in set1 False >>> 2 not in set1 False >>> 5 not in set1 True
Bitwise operators: They act on individual digits of the operands in binary form. They operate bit by bit, hence the name bitwise operators.
Python supports following bitwise operators
1) &
2)|
3) ~
4) ^
5) >>
6) <<
Operator | Meaning | Example |
---|---|---|
& | Bitwise AND | x & y = 101 & 10 = 000 = (0) |
| | Bitwise OR | x | y = 101 | 10 = 111 = (7) |
~ | Bitwise NOT | ~x = ~101 = 010 = (6) |
^ | Bitwise XOR | x ^ y = 101 ^ 10 = 111 = (7) |
>> | Bitwise right shift | x >> 1 = 101 >> 1 = 010 = (2) |
<< | Bitwise left shift | x << 1 = 101 << 1 = 1010 = (10) |
>>> x=5 >>> y=2 >>> bin(x) '0b101' >>> bin(y) '0b10' >>> x & y 0 >>> x | y 7 >>> ~x -6 >>> ~y -3 >>> x ^ y 7 >>> y ^x 7 >>> x>>1 2 >>> x>>2 1 >>> x<<1 10 >>> x<<2 20