#Addition Operator
x = 10
y = 3
z = x + y
print("Addition of {} and {} is {}.".format(x,y,z))
#Value of x y z
x = '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 Operators
d = { 1:'one',2:'two'}
d1 = { 3:'three',4:'four'}
print(d+d1)
'''
""" Sets can not be processed by Airthmatic Operators
s1 = { 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 Operator
x = 10
y = 50
print(x-y)
#s = 'hello'
#l = 'world' #strings cannot be subtracted
#print(s-l)
l1 = [ 1,2,3] #list does not support subtraction operation
l2 = [ 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 = 53
y = 20
print(x*y)
s = 'Hello_World '
k = ' Hi '
#print(s*k)
print(5*s) #print s 5 times
print(k*3) #print k 3 times
l1 =[1,2,3]
print(l1*3) #returns list 3 times
l2 = [ 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'
Example 4:
#Division Operator
x = 53
y = 12
print(x/y) #Absolute Division
print(x%y) #Remainder
print(x//y) #Quotient
print( 5>7 and 6-5*3+6)
print( 5<7 and 6-5*3+6)
print( 5<7 and 6-4*3+6)
print( 6-4*3+6 and 5<7)
print( 6-4*3+6 and 5>7)
False
-3
0
0
0
Or
If x is false, return y
else x
Example 7:
print( 6-4*3+6 or 6 < 5)
print( 6-4*3+6 or 6 > 5)
print( 6 > 5 or 6-4*3+6)
print( 6 < 5 or 6-4*3+6)
print( 6 < 5 or 16-4*3+6)
print( not True )
False
True
True
0
10
False
Example 8:
t = True
f = False
print("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<4 or 3 > 6) ) or (10/2*3-1)
print(k)
14.0
Example 12:
x = 6+7
print(x)
x,y = 8,7
l = ['hi','hello','how are you']
a,b,c = l
print(x,y,a,b,c)
13
8 7 hi hello how are you
Example 13:
x = 5
y = 6
x,y=y,x
print(x,y)
a = 4
b = 6
c = 10
a,b,c=a+b,a-b,c*b
print(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 s2
if x :
print("Pattern Found in step 1") #return this if x=True
else : print("Patten Not Found")
p = s1 not in s2
print(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]
if int(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 not
x = 5
y = 5
print(x is y)
print( x is not y )
p = 3
q = 4
if p is q :
print("Both are equal")
else :
print("Both are different")