Python Operator Class 11
Python Operator Class 11
Operators
Operators are the symbols to do computation or check.
1) Unary Operator
The operator having one operand called unary operator.
Example:
+ Unary plus (Positive sing)
Example: + 5
- Unary minus (Negative sing)
Example: - 10
~ Bitwise complement (Flip the bit from 1 to 0 or 0 to 1)
x, y = -10, 20 print(~x, ~y) Output: 9 -21
** Exponent (power)
print(2 ** 4) Output: 16
b) Relational Operators
> Greater than == Equals to
print(10 > 20) Output: False print(10 == 10) Output: True
print(20 > 10) Output: True print(20 == 10) Output: False
c) Logical Operators
and logical AND
or logical OR
d) Augmented Assignment x += 2
(Shorthand) Operators print(x) Output: 12
= Assignment
x -= 7
print(x) Output: 3
+= Assign Sum
x *= 3
-= Assign Difference
print(x) Output: 30
*= Assign Product x **= 3
print(x) Output: 1000
**= Assign Exponent
x /= 7
/= Assign Quotient print(x) Output: 1.4285714285714286
g) Bitwise Operators
Bitwise operators are used to perform bitwise calculations on integers.
The integers are converted into binary format and then operations are performed
bit by bit, hence the name bitwise operators.
Bitwise operators work on integers only and the final output is returned in the
decimal format.
| Bitwise OR
Bitwise or operator returns If both the bits are 0, then it returns 0, otherwise 1.
print(c) Output: 15
Left Shift
Bitwise left shift operator shifts the left operand bits towards the left side for
the given number of times in the right operand.
a = 10 1010 (Binary)
a << 2 1 0 1 0 << 2
1 0 1 0 0 0
40 (decimal)
Right Shift
The left side operand bits are moved towards the right side for the given
number of times.
a = 10 1010 (Binary)
a >> 2 1 0 1 0 >> 2
1 0
2 (decimal)
a is b
a b
10 11 12 13
20 1 0 1 1 0
1 1 0 0
Special Case
>>> a = 10 >>> a = 10
>>> b = 10 >>> b = int(input("Enter a number: "))
>>> a == b Enter a number: 10
True >>> a == b
>>> a is b True
True >>> a is b
True
>>> a, b = 2.5, 2.5
>>> a == b >>> a = 2.5
True >>> b = float(input("Enter a number: "))
>>> a is b Enter a number: 2.5
True >>> a == b
True
>>> a, b = "this", "this" >>> a is b
>>> a == b False
True
>>> a is b
True
In case of float and string, accepting the data will give different
reference location.
The or operator will test the second operand only if the first operand is
false, otherwise ignore it.
If first operand, (x) has falsetval, then return first operand x as result.
Otherwise return y.
The or only evaluates the second argument if the first one is falsetvalue.
The and only evaluates the second argument if the first one is truetvalue.
Operation Result
x or y If x is falsetvalue , then returns y as result, otherwise x
x and y If x is falsetvalue, then returns x as result, otherwise y
not x If x is falsetvalue, then return True as result, otherwise false.
Note
All operators have left – to – right associativity.
2+5*6
Except exponentiation (**), which has right – to – left associativity.
Example:
2 ** 3 ** 4 will be evaluated as 2 ** (3 ** 4), not as (2 ** 3) ** 4