Session1 Programs
Session1 Programs
php
Variables
#!/usr/bin/python
print(counter)
print(miles)
print(name)
#!/usr/bin/python
#!/usr/bin/python
a = 21
b = 10
c=0
c=a+b
c=a-b
c=a*b
c=a/b
c=a%b
b=3
c = a**b
a = 10
b=5
c = a//b
Comparison operator
#!/usr/bin/python
a = 21
b = 10
c=0
if ( a == b ):
else:
if ( a != b ):
else:
else:
if ( a < b ):
else:
if ( a > b ):
else:
a = 5;
b = 20;
if ( a <= b ):
else:
if ( b >= a ):
else:
Assignment Operator
#!/usr/bin/python
a = 21
b = 10
c=0
c=a+b
c += a
c *= a
c /= a
c =2
c %= a
c **= a
c //= a
#!/usr/bin/python
a = 60 # 60 = 0011 1100
b = 13 # 13 = 0000 1101
c=0
c = a | b; # 61 = 0011 1101
c = a ^ b; # 49 = 0011 0001
Membership opetator
#!/usr/bin/python
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")
Identity Operator
#!/usr/bin/python
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")
When you execute the above program it produces the following result −
Line 1 - a and b have same identity
Line 2 - a and b have same identity
Line 3 - a and b do not have same identity
Line 4 - a and b do not have same identity
Operator Precedence
#!/usr/bin/python
a = 20
b = 10
c = 15
d=5
e=0
e = (a + b) * c / d #( 30 * 15 ) / 5
e = ((a + b) * c) / d # (30 * 15 ) / 5
e = a + (b * c) / d; # 20 + (150/5)
When you execute the above program, it produces the following result −
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