# Examples of Arithmetic Operator
a = 9
b = 4
# Addition of numbers
add = a + b
# Subtraction of numbers
sub = a - b
# Multiplication of number
mul = a * b
# Division(float) of number
div1 = a / b
# Division(floor) of number
div2 = a // b
# Modulo of both number
mod = a % b
# Power
p = a ** b
# print results
print("Addition of numbers = ",add)
print("Subtraction of numbers = ",sub)
print("Multiplication of numbers = ",mul)
print("Division(float) of numbers = ",div1)
print("Division(floor) of numbers = ",div2)
print("Modulo of numbers = ",mod)
print("Power of number = ",p)
Addition of numbers = 13
Subtraction of numbers = 5
Multiplication of numbers = 36
Division(float) of numbers = 2.25
Division(floor) of numbers = 2
Modulo of numbers = 1
Power of number = 6561
c=[1,2,3]
d=[1,2]
d[0]=c*6
d[1]='abc'
print("value is ",d)
print("value is ",c)
a=2 #input()
b=64 #input()
c=[1,2,3]
d=c*2
print("value is ",(c * 2))
#print("value is ",a//b)
value is [1, 2, 3, 1, 2, 3]
a=1567
b=1567//10
c=1567%10
print("b = ",b," c = ",c)
b1= b//10
c1= b%10
print("b1 = ",b1,"number = ",str(c)+str(c1))
b2= b1//10
c2= b1%10
print(a)
print("b2 = ",b2,"number = ",str(c)+str(c1)+str(c2)+str(b2))
b = 156 c = 7
b1 = 15 number = 76
1567
b2 = 1 number = 7651
# Reverse the integer value
s=input("Enter Integer Value: ")
print("Original = ",s[:])
i=int(s[::-1])
print("Reverse = ",i)
print(type(i))
Enter Integer Value: 654
Original = 654
Reverse = 456
<class 'int'>
# Examples of Comparison or Relational Operators
a = 13
b = 33
# a > b is False
print(a > b)
# a < b is True
print(a < b)
# a == b is False
print(a == b)
# a != b is True
print(a != b)
# a >= b is False
print(a >= b)
# a <= b is True
print(a <= b)
False
True
False
True
False
True
# Examples of Assignment Operators
a = 10
# Addition Assignment
a += 5
print ("a += 5 : ", a)
# Subtraction Assignment
a -= 5
print ("a -= 5 : ", a)
# Multiplication Assignment
a *= 5
print ("a *= 5 : ", a)
# Division Assignment
a //= 5
print ("a //= 5 : ",a)
# Remainder Assignment
a %= 3
print ("a %= 3 : ", a)
# Exponent Assignment
a=10
a **= 2
print ("a **= 2 : ", a)
# Floor Division Assignment
a=-100
a //= 3
print ("a //= 3 : ", a)
a += 5 : 15
a -= 5 : 10
a *= 5 : 50
a //= 5 : 10
a %= 3 : 1
a **= 2 : 100
a //= 3 : -34
# Examples of Bitwise operators
a = 60 # 60 = 0011 1100
b = 13 # 13 = 0000 1101
# Binary AND
c = a & b # 12 = 0000 1100
print ("a & b : ", c)
# Binary OR
c = a | b # 61 = 0011 1101
print ("a | b : ", c)
# Binary XOR
c = a ^ b # 49 = 0011 0001
print ("a ^ b : ", c)
# Binary Ones Complement
c = ~a; # -61 = 1100 0011
print ("~a : ", c)
# Binary Left Shift
c = a << 2; # 240 = 1111 0000
print ("a << 2 : ", c)
# Binary Right Shift
c = a >> 2; # 15 = 0000 1111
print ("a >> 2 : ", c)
a & b : 12
a | b : 61
a ^ b : 49
~a : -61
a << 2 : 240
a >> 2 : 15
# Examples of Logical Operator
a = True
b = False
# Print a and b is False
print(a and b)
# Print a or b is True
print(a or b)
# Print not a is False
print(not a)
#Python Membership Operators Example
a = 10
b = 20
list = [1, 2, 3, 4, 5 ];
if ( a in list ):
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")
a = 2
if ( a in list ):
print ("Line 3 - a is available in the given list")
else:
print ("Line 3 - a is not available in the given list")
a = '1'
b = 'csmss college of poly'
list1= {1: 2, 3: 4, 5:6 };
print(a in list1)
False
# Python program to illustrate not 'in' operator
x = 24
y = 20
list = [10, 20, 30, 40, 50]
if (x not in list):
print("x is NOT present in given list")
else:
print("x is present in given list")
if (y in list):
print("y is present in given list")
else:
print("y is NOT present in given list")
#Identity Operator Example
a = 10
b = 20
c = a
print(a is not b)
print(a is c)
#Identity Operator Example
a = 20
b = 20
if ( a is b ):
print ("Line 1 - a and b have same identity")
else:
print ("Line 1 - a and b do not have same identity")
if ( id(a) == id(b) ):
print ("Line 2 - a and b have same identity")
else:
print ("Line 2 - a and b do not have same identity")
b = 30
if ( a is b ):
print ("Line 3 - a and b have same identity")
else:
print ("Line 3 - a and b do not have same identity")
if ( a is not b ):
print ("Line 4 - a and b do not have same identity")
else:
print ("Line 4 - a and b have same identity")
a=10
b=10
#b=a
c=20
print(id(a))
print(id(b))
print(id(c))
print((a))
print((b))
print(a is not b)
# Program to demonstrate ternary operator or conditional operator
a, b = 100, 200
# Copy value of a in min if a < b else copy b
min = a if a < b else b
print(a if a < b else b)
100
# Python program to demonstrate ternary operator
a, b = 10, 20
# Use tuple for selecting an item (if_test_false,if_test_true)[test]
#if [a<b] is true it return 1, so element with 1 index will print
# else if [a<b] is false it return 0, so element with 0 index will
print
print( (b, a) [a < b] )
# Use Dictionary for selecting an item if [a < b] is true then value
of True key will print
# else if [a<b] is false then value of False key will print
print({True: a, False: b} [a < b])
# lambda is more efficient than above two methods because in lambda we
are assure that
# only one expression will be evaluated unlike in tuple and Dictionary
print((lambda: b, lambda: a)[a < b]())
a,b=100,20
print( (b, a) [a < b] )
20
# Python program to demonstrate nested ternary operator
a, b = 10, 20
print ("Both a and b are equal" if a == b else "a is greater than b"
if a > b else "b is greater than a")
#Find the Larger number among 2 using ternary operator in python3
a=5
b=7
# [statement_on_True] if [condition] else [statement_on_false]
print(a,"is greater") if (a>b) else print(b,"is Greater")
#Python Operators Precedence Example
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)