#Addition Operator x =10y =3z = x + yprint("Addition of {} and {} is {}.".format(x,y,z))#Value of x y zx ='hello 'y ='world'print(x+y)l1 = [ 1,2,3]l2 = [ 4,5,6]print(l2 + l1 )t1 = ('one','two','three')t2 = ('four','five','six')print(t1+t2)''' Dictionary can not be processed by Airthmatic Operatorsd = { 1:'one',2:'two'}d1 = { 3:'three',4:'four'}print(d+d1)'''""" Sets can not be processed by Airthmatic Operatorss1 = { 1,2,3 }s2 = { 4,5,6 }print(s1+s2)"""
Addition of 10 and 3 is 13.
hello world
[4, 5, 6, 1, 2, 3]
('one', 'two', 'three', 'four', 'five', 'six')
Example 2:
#Subtraction Operatorx =10y =50print(x-y)#s = 'hello'#l = 'world' #strings cannot be subtracted#print(s-l)l1 = [ 1,2,3] #list does not support subtraction operationl2 = [ 4,5,6]print(l1-l2)
-40
TypeError Traceback (most recent call last)
in () 8 l1 = [ 1,2,3] #list does not support subtraction operation 9 l2 = [ 4,5,6] ---> 10 print(l1-l2)
TypeError: unsupported operand type(s) for -: 'list' and 'list'
Example 3:
x =53y =20print(x*y)s ='Hello_World 'k =' Hi '#print(s*k)print(5*s)#print s 5 timesprint(k*3)#print k 3 timesl1 =[1,2,3]print(l1*3)#returns list 3 timesl2 = [ 4,5,6]#print(l1*l2) # does not support multiplication
1060
Hello_World Hello_World Hello_World Hello_World Hello_World Hi Hi Hi
[1, 2, 3, 1, 2, 3, 1, 2, 3]
TypeError Traceback (most recent call last)
in () 11 print(l13) #returns list 3 times 12 l2 = [ 4,5,6] ---> 13 print(l1l2) # does not support multiplication
TypeError: can't multiply sequence by non-int of type 'list'
t =Truef =Falseprint("X\tY\tand\tor")print("{}\t{}\t{}\t{}".format(t, t, t and t, t or t))print("{}\t{}\t{}\t{}".format(t, f, t and f, t or f))print("{}\t{}\t{}\t{}".format(f, t, f and t, f or t))print("{}\t{}\t{}\t{}".format(f, f, f and f, f or f))
k = ( (6*5/2-5) and (6<4or3>6) ) or (10/2*3-1)print(k)
14.0
Example 12:
x =6+7print(x)x,y =8,7l = ['hi','hello','how are you']a,b,c = lprint(x,y,a,b,c)
13
8 7 hi hello how are you
Example 13:
x =5y =6x,y=y,xprint(x,y)a =4b =6c =10a,b,c=a+b,a-b,c*bprint(a,b,c)
6 5
10 -2 60
Membership Operator
Example 14:
# in , not in s2 ="Dog is an animal."s1 ="Dog"x = s1 in s2if x :print("Pattern Found in step 1")#return this if x=Trueelse:print("Patten Not Found")p = s1 notin s2print(x)print(p)
Pattern Found in step 1
True
False
Example 15:
l1 = [ 1,2,6,5,[3,4]]l2 = [ 3,4]if l2 in l1 :print("l2 found in l1")else:print("No Pattern Found")
l2 found in l1
Example 16:
l = [ 1,2,6,324,23,4234,54,2342,3432]ifint(input("Enter a number : "))in l :#takes input and convert it into int print("Number found in list")print("Number matched")else:print("No such number in our list")
Enter a number : 324
Number matched
Identity Operator
Example 17:
#is or is notx =5y =5print(x is y)print( x isnot y )p =3q =4if p is q :print("Both are equal")else:print("Both are different")