0% found this document useful (0 votes)
9 views

Lecture 4

The document describes operator precedence in Python. It lists operators from highest to lowest precedence and provides examples of how operator precedence affects expression evaluation. For example, multiplication and division have higher precedence than addition and subtraction, so an expression like 7 + 3 * 2 is evaluated as 7 + (3 * 2) and results in 13, not 20. The document also provides examples of bitwise operators in Python and how they work.

Uploaded by

raghu kiran
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lecture 4

The document describes operator precedence in Python. It lists operators from highest to lowest precedence and provides examples of how operator precedence affects expression evaluation. For example, multiplication and division have higher precedence than addition and subtraction, so an expression like 7 + 3 * 2 is evaluated as 7 + (3 * 2) and results in 13, not 20. The document also provides examples of bitwise operators in Python and how they work.

Uploaded by

raghu kiran
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

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

Operator Description

** Exponentiation (raise to the power)

~+- Complement, unary plus and minus (method names for the last two are +@ and -
@)

* / % // Multiply, divide, modulo and floor division

+- Addition and subtraction

>> << Right and left bitwise shift

& Bitwise 'AND'td>

^| Bitwise exclusive `OR' and regular `OR'

<= < > >= Comparison operators

<> == != Equality operators

= %= /= //= -= += Assignment operators


*= **=

is is not Identity operators

in not in Membership operators

not or and Logical operators

Operator precedence affects how an expression is evaluated.


For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has higher
precedence than +, so it first multiplies 3*2 and then adds into 7.
Here, operators with the highest precedence appear at the top of the table, those with the lowest
appear at the bottom.
a = 20

b = 10

c = 15

d=5

e=0

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

e= a/d/2

print "a/b/c===", e

Output:

Value of (a + b) * c / d is 90
Value of ((a + b) * c) / d is 90
Value of (a + b) * (c / d) is 90
Value of a + (b * c) / d is 50
a/b/c==== 2

Bitwise operators

There are following Bitwise operators supported by Python language.

Operator Description Example

& Binary AND Operator copies a bit to the result if it


(a & b) (means 0000 1100)
exists in both operands

| Binary OR It copies a bit if it exists in either (a | b) = 61 (means 0011 1101)


operand.
^ Binary XOR It copies the bit if it is set in one operand
(a ^ b) = 49 (means 0011 0001)
but not both.

~ Binary Ones (~a ) = -61 (means 1100 0011 in


It is unary and has the effect of 'flipping'
Complement 2's complement form due to a
bits.
signed binary number.

<< Binary Left The left operands value is moved left by


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

>> Binary Right The left operands value is moved right by


Shift the number of bits specified by the right a >> 2 = 15 (means 0000 1111)
operand.
1’s complement of a binary number is another binary number obtained by
toggling all bits in it, i.e., transforming the 0 bit to 1 and the 1 bit to 0.
Examples: 
1's complement of "0111" is "1000"
1's complement of "1100" is "0011"

0101 -> 1010


2’s complement of a binary number is 1, added to the 1’s complement of
the binary number. 
Examples: 

2's complement of "0111" is "1001"

1000
1
1001

2's complement of "1100" is "0100"


Let’s take number 2,
2 = (0000 0010) 2

1’s complement of 2 = (1111 1101) 2


Add 1 to get the 2’s complement,
~2 = (11111101) 2
Result of ~2 is equal to the representation of -3 in binary, hence ~2 is -3.
a = 60 # 60 = 0011 1100

b = 13 # 13 = 0000 1101

c=0

c = a & b; # 12 = 0000 1100

print "Line 1 - Value of c is ", c


c = a | b; # 61 = 0011 1101

print "Line 2 - Value of c is ", c

c = a ^ b; # 49 = 0011 0001

print "Line 3 - Value of c is ", c

c = ~a; # -61 = 1100 0011

print "Line 4 - Value of c is ", c

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

print "Line 5 - Value of c is ", c


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

print "Line 6 - Value of c is ", c


output:

Line 1 - Value of c is 12
Line 2 - Value of c is 61
Line 3 - Value of c is 49
Line 4 - Value of c is -61
Line 5 - Value of c is 240
Line 6 - Value of c is 15

# Python program to illustrate the use


# of 'is' identity operator
x = 5
if (type(x) is int):
    print("true")
else:
    print("false")

output

true

You might also like