2 - 2 - Boolean and Arithmetic Operators
2 - 2 - Boolean and Arithmetic Operators
Highlights!
1
2/2/2022
Operator
Operators are the constructs, which can manipulate the value of operands.
Consider the expression,
Operator
Result
4+5=9
Operand Operand
2
2/2/2022
Types of Operator
Arithmetic Operators
Assignment Operators
Logical Operators
Bitwise Operators
Membership Operators
Identity Operators
Operator
ARITHMETIC OPERATORS
3
2/2/2022
Assume variable a holds the value 10 and variable b holds the value 21, then
a = 10
b = 21 Operator Description Example
+ Addition Adds values on either side of the a + b = 31
operator.
- Subtraction Subtracts right hand operand from left a – b = -11
hand operand.
* Multiplication Multiplies values on either side of the a * b = 210
operator
/ Division Divides left hand operand by right hand b / a = 2.1
operand
% Modulus Divides left hand operand by right hand b%a=1
operand and returns remainder
Assume variable a holds the value 10 and variable b holds the value 21, then
a = 10
b = 21
Floor Division the quotient in which the digits after the 9.0//2.0 = 4.0
decimal point are removed.
4
2/2/2022
a = 2
b = 3 Output??
c = a**b
print ("Line 6 - Value of c is ", c)
a = 10
b = 5
c = a//b
print ("Line 7 - Value of c is ", c)
10
5
2/2/2022
Line 1 - Value of c is 31
Line 2 - Value of c is 11
Have you got it all correct!!!
Line 3 - Value of c is 210
Line 4 - Value of c is 2.1
Line 5 - Value of c is 1
Line 6 - Value of c is 8
Line 7 - Value of c is 2
11
Operator
COMPARISON OPERATORS
12
6
2/2/2022
Assume variable a holds the value 10 and variable b holds the value 21, then
a = 10
b = 21
Outcome of a comparison is
a. True or
b. False
13
a = 10
b = 21
14
7
2/2/2022
a = 10
b = 21
Operator Description Example
Outcome of a comparison is < If the value of left operand is less than the (a < b) is true.
a. True or value of right operand, then condition
becomes true.
b. False
>= If the value of left operand is greater than (a >= b) is not
or equal to the value of right operand, true.
then condition becomes true.
15
a = 21
b = 10
Output??
if ( a == b ):
print ("Line 1 - a is equal to b")
else:
print ("Line 1 - a is not equal to b")
if ( a != b ):
print ("Line 2 - a is not equal to b")
else:
print ("Line 2 - a is equal to b")
if ( a < b ):
print ("Line 3 - a is less than b" )
else:
print ("Line 3 - a is not less than b")
16
8
2/2/2022
if ( a > b ):
print ("Line 4 - a is greater than b")
else: Output??
print ("Line 4 - a is not greater than b")
if ( a <= b ):
print ("Line 5 - a is either less than or equal to b")
else:
print ("Line 5 - a is neither less than nor equal to b")
if ( b >= a ):
print ("Line 6 - b is either greater than or equal to b")
else:
print ("Line 6 - b is neither greater than nor equal to b")
17
18
9
2/2/2022
Operator
PYTHON ASSIGNMENT OPERATORS
19
Assume variable a holds the value 10 and variable b holds the value 21, then
20
10
2/2/2022
21
a = 21
b = 10 Output??
c = 0
c = a + b
print ("Line 1 - Value of c is ", c)
c += a
print ("Line 2 - Value of c is ", c )
c *= a
print ("Line 3 - Value of c is ", c )
22
11
2/2/2022
c /= a Output??
print ("Line 4 - Value of c is ", c )
c = 2
c %= a
print ("Line 5 - Value of c is ", c)
c **= a
print ("Line 6 - Value of c is ", c)
c //= a
print ("Line 7 - Value of c is ", c)
23
Line 1 - Value of c is 31
Line 2 - Value of c is 52 Have you got it all correct!!!
Line 3 - Value of c is 1092
Line 4 - Value of c is 52.0
Line 5 - Value of c is 2
Line 6 - Value of c is 2097152
Line 7 - Value of c is 99864
24
12
2/2/2022
Operator
BITWISE OPERATORS
25
a = 60; and
b = 13;
a = 0011 1100
b = 0000 1101
-----------------
a&b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a = 1100 0011
Pyhton's built-in function bin() can be used to obtain binary representation of an integer
number.
26
13
2/2/2022
27
a = 60 # 60 = 0011 1100
b = 13 # 13 = 0000 1101
Output??
print ('a=',a,':',bin(a),'b=',b,':',bin(b))
c = 0
c = a | b; # 61 = 0011 1101
print ("result of OR is ", c,':',bin(c))
28
14
2/2/2022
c = a ^ b; # 49 = 0011 0001
print ("result of EXOR is ", c,':',bin(c))
Output??
29
a= 60 : 0b111100 b= 13 : 0b1101
30
15
2/2/2022
Operator
LOGICAL OPERATORS
31
a = True
b = False
Operator Description Example
32
16
2/2/2022
Operator
MEMBERSHIP OPERATORS
33
34
17
2/2/2022
if ( a in list ): Output??
print ("Line 1 - a is available in the given list")
else:
print ("Line 1 - a is not available in the given list")
if ( b not in list ):
print ("Line 2 - b is not available in the given list")
else:
print ("Line 2 - b is available in the given list")
c=b/a
if ( c in list ):
print ("Line 3 - a is available in the given list")
else:
print ("Line 3 - a is not available in the given list")
35
36
18
2/2/2022
Operator
IDENTITY OPERATORS
37
38
19
2/2/2022
if ( a is b ): Output??
print ("Line 2 - a and b have same identity")
else:
print ("Line 2 - a and b do not have same identity")
if ( id(a) == id(b) ):
print ("Line 3 - a and b have same identity")
else:
print ("Line 3 - a and b do not have same identity")
b = 30
print ('Line 4','a=',a,':',id(a), 'b=',b,':',id(b))
if ( a is not b ):
print ("Line 5 - a and b do not have same identity")
else:
print ("Line 5 - a and b have same identity")
39
40
20
2/2/2022
Operator
OPERATOR PRECEDENCE
41
For example,
x = 7 + 3 * 2;
here, x is assigned 13, not 20
42
21
2/2/2022
e = (a + b) * c / d #( 30 * 15 ) / 5
print ("Value of (a + b) * c / d is ", e)
e = ((a + b) * c) / d # (30 * 15 ) / 5
print ("Value of ((a + b) * c) / d is ", e)
e = (a + b) * (c / d) # (30) * (15/5)
print ("Value of (a + b) * (c / d) is ", e)
e = a + (b * c) / d # 20 + (150/5)
print ("Value of a + (b * c) / d is ", e)
43
Value of (a + b) * (c / d) is 90.0
Value of a + (b * c) / d is 50.0
44
22