0% found this document useful (0 votes)
16 views22 pages

2 - 2 - Boolean and Arithmetic Operators

Highlights! • Operators and their purpose.

Uploaded by

2312085
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
16 views22 pages

2 - 2 - Boolean and Arithmetic Operators

Highlights! • Operators and their purpose.

Uploaded by

2312085
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 22

2/2/2022

Python – Boolean and Arithmetic Operators


M M MAHBUBUL SYEED, PHD. ASSOCIATE PROFESSOR

Highlights!

• Operators and their purpose.


• Boolean Operators.
• Arithmetic Operators.

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

Python language supports the following types of operators-

 Arithmetic Operators

 Comparison (Relational) Operators

 Assignment Operators

 Logical Operators

 Bitwise Operators

 Membership Operators

 Identity Operators

Operator
ARITHMETIC OPERATORS

3
2/2/2022

Python Arithmetic Operators

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

Python Arithmetic Operators

Assume variable a holds the value 10 and variable b holds the value 21, then

a = 10
b = 21

Operator Description Example


** Performs exponential (power) calculation on a**b

Exponent operators 10 to the power 21


// The division of operands where the result is b//a = 2 and

Floor Division the quotient in which the digits after the 9.0//2.0 = 4.0
decimal point are removed.

4
2/2/2022

Python Arithmetic Operators


a = 21
b = 10
Output??
c = 0
c = a + b
print ("Line 1 - Value of c is ", c)
c = a - b
print ("Line 2 - Value of c is ", c )
c = a * b
print ("Line 3 - Value of c is ", c)
c = a / b
print ("Line 4 - Value of c is ", c )
c = a % b
print ("Line 5 - Value of c is ", c)

Python Arithmetic Operators

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

Python Arithmetic Operators

Verify your result here….

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

Python Comparison Operators


These operators compare the values on either side of them and decide the relation among them.
They are also called Relational operators.

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

Python Comparison Operators


Assume variable a holds the value 10 and variable b holds the value 21, then

a = 10
b = 21

Outcome of a comparison is Operator Description Example


a. True or == If the values of two operands are equal, (a == b) is not
b. False then the condition becomes true. true.

!= If values of two operands are not equal, (a!= b) is true.


then condition becomes true.

> If the value of left operand is greater than (a > b) is not


the value of right operand, then condition true.
becomes true.

14

7
2/2/2022

Python Comparison Operators


Assume variable a holds the value 10 and variable b holds the value 21, then

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.

<= If the value of left operand is less than or (a <= b) is true.


equal to the value of right operand, then
condition becomes true.

15

Python Comparison Operators

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

Python Comparison Operators

if ( a > b ):
print ("Line 4 - a is greater than b")
else: Output??
print ("Line 4 - a is not greater than b")

a,b=b,a #values of a and b swapped. a becomes 10, b becomes 21

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

Python Comparison Operators

Verify your result here….

Line 1 - a is not equal to b


Line 2 - a is not equal to b Have you got it all correct!!!
Line 3 - a is not less than b
Line 4 - a is greater than b
Line 5 - a is either less than or equal to b
Line 6 - b is either greater than or equal to b

18

9
2/2/2022

Operator
PYTHON ASSIGNMENT OPERATORS

19

Python Assignment Operators


These operators assigns value to a given variable (either directly or after some specified
computation as identified by the operator)

Assume variable a holds the value 10 and variable b holds the value 21, then

a = 10 Operator Description Example


b = 21
= Assigns values from right side operands to c=a+b
left side operand assigns value of
a + b into c

+= It adds right operand to the left operand c += a


Add AND and assign the result to left operand is equivalent to
c=c+a

-= It subtracts right operand from the left c -= a


Subtract AND operand and assign the result to left is equivalent to
operand c=c-a

20

10
2/2/2022

Python Assignment Operators


Assume variable a holds the value 10 and variable b holds the value 21, then

a = 10 Operator Description Example


b = 21
*= It multiplies right operand with the left c *= a
Multiply AND operand and assign the result to left operand is equivalent to
c=c*a

/= It divides left operand with the right operand c /= a


Divide AND and assign the result to left operand is equivalent to
c=c/a

**= Performs exponential (power) calculation on c **= a


Exponent operators and assign value to the left is equivalent to
AND operand c = c ** a

%= It takes modulus using two operands and c %= a


Modulus AND assign the result to left operand is equivalent to
c=c%a

21

Python Assignment Operators

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

Python Assignment Operators

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

Python Assignment Operators

Verify your result here….

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

Python Bitwise Operators


Bitwise operator works on bits and performs bit-by-bit operation.
Assume if

a = 60; and
b = 13;

Now in binary format they will be as follows,

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

Python Bitwise Operators


a = 60; and Operator Description Example
b = 13;
& Operator copies a bit to the result, if (a & b)
Binary AND it exists in both operands (means 0000 1100)
a = 0011 1100
b = 0000 1101
| It copies a bit, if it exists in either (a | b)
Binary OR operand. (means 0011 1101)

^ It copies the bit, if it is set in one (a ^ b)


Binary XOR operand but not both. (means 0011 0001)

~ It is unary and has the effect of (~a )


Binary Ones 'flipping' bits. -61 (means 1100 0011 in 2's
Complement complement form due to a
signed binary number.

<< Binary Left The left operand’s value is moved a <<


Shift left by the number of bits specified = 240
by the right operand. (means 1111 0000)

27

Python Bitwise Operators

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; # 12 = 0000 1100


print ("result of AND is ", c,':',bin(c))

c = a | b; # 61 = 0011 1101
print ("result of OR is ", c,':',bin(c))

28

14
2/2/2022

Python Bitwise Operators

c = a ^ b; # 49 = 0011 0001
print ("result of EXOR is ", c,':',bin(c))
Output??

c = ~a; # -61 = 1100 0011


print ("result of COMPLEMENT is ", c,':',bin(c))

c = a << 2; # 240 = 1111 0000


print ("result of LEFT SHIFT is ", c,':',bin(c))

c = a >> 2; # 15 = 0000 1111


print ("result of RIGHT SHIFT is ", c,':',bin(c))

29

Python Bitwise Operators

Verify your result here….

a= 60 : 0b111100 b= 13 : 0b1101

result of AND is 12 : 0b1100


Have you got it all correct!!!
result of OR is 61 : 0b111101

result of EXOR is 49 : 0b110001

result of COMPLEMENT is -61 : -0b111101

result of LEFT SHIFT is 240 : 0b11110000

result of RIGHT SHIFT is 15 : 0b111

30

15
2/2/2022

Operator
LOGICAL OPERATORS

31

Python Logical Operators


Assume variable a holds the value True and variable b holds the value False, then

a = True
b = False
Operator Description Example

and If both the operands are true then condition (a and b)


Logical AND becomes true. is False.

or If any of the two operands are non-zero then (a or b)


Logical OR condition becomes true. is True.

not Used to reverse the logical state of its operand. (not a)


Logical NOT Not is False.

32

16
2/2/2022

Operator
MEMBERSHIP OPERATORS

33

Python Membership Operators


Python’s membership operators test for membership in a sequence, such as strings, lists, or tuples.
There are two membership operators as explained below

Operator Description Example

in Evaluates to true, if it finds a variable in the x in y,


specified sequence and false otherwise. here in results in a 1 if
x is a member of
sequence y.

not in Evaluates to true, if it does not find a variable in x not in y


the specified sequence and false otherwise. here not in results in
a 1 if x is not a
member of
sequence y.

34

17
2/2/2022

Python Membership Operators


a = 10
b = 20
list = [1, 2, 3, 4, 5 ]

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

Python Membership Operators

Verify your result here….

Line 1 - a is not available in the given list

Line 2 - b is not available in the given list


Have you got it all correct!!!

Line 3 - a is available in the given list

36

18
2/2/2022

Operator
IDENTITY OPERATORS

37

Python Identity Operators


Identity operators compare the memory locations of two objects.
There are two Identity operators as explained below:

Operator Description Example

is Evaluates to true if the variables on either side of x is y,


the operator point to the same object and false here is results in 1 if id(x)
otherwise. equals id(y).

Is not Evaluates to false if the variables on either side of x is not y,


the operator point to the same object and true here is not results in 1 if
otherwise. id(x) is not equal to id(y).

38

19
2/2/2022

Python Identity Operators


a = 20
b = 20
print ('Line 1','a=',a,':',id(a), 'b=',b,':',id(b))

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

Python Identity Operators

Verify your result here….

Line 1 a= 20 : 1594701888 b= 20 : 1594701888

Line 2 - a and b have same identity


Have you got it all correct!!!

Line 3 - a and b have same identity

Line 4 a= 20 : 1594701888 b= 30 : 1594702048

Line 5 - a and b do not have same identity

40

20
2/2/2022

Operator
OPERATOR PRECEDENCE

41

Python Operator Precedence


The following table lists all the operators
from highest precedence to the lowest.

For example,
x = 7 + 3 * 2;
here, x is assigned 13, not 20

Because, the operator * has higher


precedence than +, so it first multiplies 3*2
and then is added to 7.

42

21
2/2/2022

Python Operator Precedence


a = 20
b = 10
c = 15
d = 5 Output??

print ("a:%d b:%d c:%d d:%d" % (a,b,c,d ))

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

Python Operator Precedence

Verify your result here….

a:20 b:10 c:15 d:5

Value of (a + b) * c / d is 90.0 Have you got it all correct!!!


Value of ((a + b) * c) / d is 90.0

Value of (a + b) * (c / d) is 90.0

Value of a + (b * c) / d is 50.0

44

22

You might also like