5 Operators
5 Operators
Operators are symbols that perform specific operations when applied on variables.
c=a+b # c,a,b, operands and “=”,”+” are operators
Above statement is an expression (combination of operands and operator.)
Types of operator
Arithmetic Operators : +,-,*, / , %, **, // =
Relational(Comparison) operators: < , > , <= , >= , = =(equal to),
!= (not equal to)
Assignment Operator:=,+ =, -=, *=, /=, //=, %=
Bitwise Operators: &, ^, |
Shift Operators: << , >>
Logical Operators: and, or, not
Identity Operators: is, is not
Membership Operators: in, not in
Arithmetic Operators
Assume variable a holds the value 10 and variable b holds the value 21, then −
- Subtraction Subtracts right hand operand from left hand operand. a – b = -11
% Modulus Divides left hand operand by right hand operand and b%a=1
returns remainder
// Floor Division - The division of operands where the result is 9//2 = 4 and
the quotient in which the digits after the decimal point are 9.0//2.0 = 4.0,
removed. But if one of the operands is negative, the result
is floored, i.e., rounded away from zero (towards negative -11//-3 = -4,
infinity): 11.0//3 = -4.0
** Exponent Performs exponential (power) calculation on operators a**b =10 to the
power 20
Assignment Operator
Assume variable a holds the value 10 and variable b holds the value 20, then −
Show Example
+= Add AND It adds right operand to the left operand and c += a is equivalent
assign the result to left operand to c = c + a
-= Subtract AND It subtracts right operand from the left operand c -= a is equivalent
and assign the result to left operand to c = c – a
*= Multiply AND It multiplies right operand with the left operand c *= a is equivalent
and assign the result to left operand to c = c * a
/= Divide AND It divides left operand with the right operand c /= a is equivalent
and assign the result to left operand to c = c / a
//= Floor Division It performs floor division on operators and c //= a is equivalent
assign value to the left operand to c = c // a
> If the value of left operand is greater than the value of right (a > b)
operand, then condition becomes true.
Result: False
< If the value of left operand is less than the value of right (a < b)
operand, then condition becomes true.
Result: True
>= If the value of left operand is greater than or equal to the value (a >= b)
of right operand, then condition becomes true.
Result: False
<= If the value of left operand is less than or equal to the value of (a <= b)
right operand, then condition becomes true.
Result: True
Chained comparison
Python can chain multiple comparison which are like shortened version of Boolean expression.
Rather than writing 1<2 and 2<3 we can write 1<2<3. This is a chained version of the earlier
Boolean expression.
Example
A=10
B=20
C=5
# C <A<B is same as C <A and A<B
print(C<A) #True
print(A<B) #True
print(C<A<B) #True
As per the and property C<A will be first evaluated and if only it is True, then the next
chained expression A<B will be evaluated.
If the value of ‘a’ is 100 what is the difference between the below statements.
Statement 1 >>> a == 60 # equality operator
Statement 2 >>> a = 60 # assignment operator
Logical value ------ Associated with other values
In Python every value is associated with Boolean value true or false. Any object can be tested
for truth value
Logical Operators
There are three logical operators supported by Python. These operators (and, or, not) are to be
written in lower case only. The logical operator evaluates to either True or False based on the
logical operands on its either side.
The and operator evaluates to True if both of its (relational) operand evaluates to True; False
if both or either of both operands evaluates to False
The or operator evaluates to True if either of its (relational) operand evaluates to True; False if
both operands evaluates to False
and -- in an expression x and y if first operand (i.e., the expression x) has false tval, then return
first operand x as result otherwise return y
truetval truetval y
truetval falsetval y
falsetval truetval x
falsetval falsetval x
Output
>>> (0) and (0) #0
>>>(0) and (10) #0
>>>(4) and (0) #0
>>> "hello" and " " #''
>>> (9) and (7) #7
>>>"xyz" and "abc" # 'abc'
or – in an expression x or y if first operand (i.e., the expression x) has false tval, then return
second operand y as result otherwise return x
truetval truetval x
truetval falsetval x
falsetval truetval y
falsetval falsetval y
Output
>>> (0) or (0) #0
>>>(0) or (10) # 10
>>>(4) or (0) #4
>>> "hello" or " " # 'hello '
>>> (9) or (7) #9
>>>"xyz" or "abc" # 'xyz'
Not Operator
>>> not 5 # False because 5 (truetval)
>>>not 0 #True because 0( falsetval)
>>> -4 #True because -4(truetval)
>>> not(5>2) #False because expression is True
>>>not(5 > 9) #True because expression is False
Identity Operators
There are two identity operators is and is not. The identity operators compares the memory
location of two objects and returns True or False accordingly.
Example
A=10
B=20
C=10
A is B # False
A is C # True
A is not B #True
A is not C #False
Membership Operators
There are two identity operators in and not in. Python’s membership operators test for
membership in a sequence, such as strings, lists, or tuples.
Example
>>> a=10
>>> b=50
>>> list=[10,20,30,40]
>>> a in list
True
>>> 50 in list
False
Operator precedence
The chart of Operator precedence from highest to lowest for the operators high
low
Operator Associativity
If an expression have multiple operators same precedence (*, /, //). All most all operators have
left to right associativity except exponentiation (**) which has right to left associativity.