lecture 8
lecture 8
Examples Output
a=10
b=5
print("a>b=>",a>b) a>b=> True
print("a>b=>",a<b) a>b=> False
print("a==b=>",a==b) a==b=> False
print("a!=b=>",a!=b) a!=b=> True
print("a>=b=>",a<=b) a>=b=> False
print("a>=b=>",a>=b) a>=b=> True
Assignment Operators
Assignment operators
are used in Python to
assign values to
variables ..
Operator Description Examples
= Assigns values from right side c = a + b assigns value of
operands to left side a + b into c
operand .
+= Add AND It adds right operand to the left c += a is equivalent to c =
operand and assign the result to c+a
left operand.
-= Subtract AND It subtracts right operand from c -= a is equivalent to c =
the left operand and assign the c-a
result to left operand .
*= Multiply AND It multiplies right operand with c *= a is equivalent to c =
the left operand and assign the c*a
result to left operand.
/= Divide AND It divides left operand with the c /= a is equivalent to c =
right operand and assign the c / ac /= a is equivalent
result to left operand. to c = c / a
%= Modulus AND It takes modulus using two c %= a is equivalent to c =
operands and assign the result to c%a
left operand.
Operator Description Examples
Examples Output
a = 21
b = 10
c=0
c=a+b
print("Line 1 - Value of c is ", c) Line 1 - Value of c is 31
c += a Line 2 - Value of c is 52
print("Line 2 - Value of c is ", c) Line 3 - Value of c is 1092
c *= a
print("Line 3 - Value of c is ", c)
c /= a
Example Output
c=2
c %= a
c **= a
c //= a
Example Output
a = True
b = False
print('a and b is',a and b) x and y is False
print('a or b is',a or b) x or y is True
print('not a is',not a) not x is False
Bitwise Operators:
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 Line 1 - Value of c is 12
c = a | b; # 61 = 0011 1101
print "Line 2 - Value of c is ", c Line 2 - Value of c is 61
c = a ^ b; # 49 = 0011 0001
print "Line 3 - Value of c is ", c Line 3 - Value of c is 49
c = ~a; # -61 = 1100 0011
print "Line 4 - Value of c is ", c Line 4 - Value of c is -61
c = a << 2; # 240 = 1111 0000
print "Line 5 - Value of c is ", c Line 5 - Value of c is 240
c = a >> 2; # 15 = 0000 1111
print "Line 6 - Value of c is ", c Line 6 - Value of c is 15
Membership Operators
Example: x=[5,3,6,4,1]
>>> 5 in x
True
>>> 5 not in x
False
Identity Operator
x=5
y=5
x2 = 'Hello’
y2 = 'Hello’
print(x1 is not y1)
print(x2 is y2)