Chapter 01 : Operators and Expressions

Example 1: Page No.:4.35

In [5]:
#Usage of Arithmetic operators

#Variable declaration
i=10
j=20

#the sum value of variables i and j stores in k variable 
k=i+j

#Result
print "Value of k is %d"%(k)
Value of k is 30

Example 2: Page No.:4.35

In [8]:
# Demonstrations on arithmetic operators

#User Input

a,b,c,d=input("Enter values of a, b, c, d: ")

#Calculation
a+=(b*c)+d

#Result
print "Value of a = %d "%(a)
Enter values of a, b, c, d ,e: 12,90,56,43
Value of a = 5095 

Example 3: Page No.:4.36

In [10]:
#Find out all arithmetic operation on two given values

#Variable Declaration
sum=sub=mul=rem=0
div=0.0
a=0
#User Input
b,c,d=input("Enter the values of b, c, d: ")

sum = b + c
sub = b - c
mul = b * c
div = b/c
rem = b % d

print "Sum = %d, Sub = %d, Mul = %d, Div = %f"%(sum,sub,mul,div)
print "Remainder of division of b & d is %d"%(rem)
print "a=%d"%(a)
Enter the values of b, c, d: 2,6,45
Sum = 8, Sub = -4, Mul = 12, Div = 0.000000
Remainder of division of b & d is 2
a=0

Example 4: Page No.: 4.38

In [14]:
#Program to use various relational operators

#!=,==,>=,<= are assignment operator

print "Condition :  Return values"
print "  5!=5    :  %5d"%(5!=5)     
print "  5==5    :  %5d"%(5==5)
print "  5>=5    :  %5d"%(5>=5)
print "  5<=5    :  %5d"%(5<=50)
print "  5!=3    :  %5d"%(5!=3)
Condition :  Return values
  5!=5    :      0
  5==5    :      1
  5>=5    :      1
  5<=5    :      1
  5!=3    :      1

Example 5: Page No.:4.39

In [7]:
# Demonstrate of logical operator

#Variable Declaration
c1=c2=c3=0

#USer Input
c1,c2,c3=input("Enter Values of C1, C2 and C3: ")
print "\n"

if (c1<c2) and (c1<c3):
    print "C1 is less than C2 and C3"

if not(c1) < (c2 ):
    print "C1 is greater than C2"
    
if ((c1 < c2 or c1 < c3)):
    print "C1 is less than C2 or C3 or both"
    
#else:
 #   print "Something went wrong. Check"
Enter Values of C1, C2 and C3: 45,32,98


C1 is greater than C2
C1 is less than C2 or C3 or both

Example 6: Page No.: 4.40

In [44]:
#Demonstration of assignment operator

#Variable Declaration
i=4
j=5

#'=' is assignment operator
i=j
k=i

#Result
print "K = %d"%(k)
K = 5

Example 7: Page No.:4.42

In [50]:
#Program using Increment and Decrement Operator.

#Variable Declaration
a=10

#There is no post and pre increment in python
#There is no post and pre decrement in python

#Result
print "The Value of a = %d"%(a)
print "Increment of a = %d"%(a+1)
print "Decrement of a = %d"%(a-1)
The Value of a = 10
Increment of a = 11
Decrement of a = 9

Example 8: Page No.:4.42

In [4]:
#Program using increment and decrement operators

#Variable Declaration

i=3
j=4

#No Pre Decrement and Pre Increment in python

i=i+1                  #Increment value
j=j-1               #Decrement value

k=i+j

#Result
print "i = %d, j = %d, k = %d"%(i,j,k)
i = 4, j = 3, k = 7

Example 9: Page No.: 4.46

In [70]:
#Program to illustrate sizeof operator

import sys

num=int(1234567890)
dec=float(0.123456)
ext=0.123456789
ltr='A'
string="Something to write home about...."
class struct():
    a=0
boy=struct()

#Result
print "Size of num is in %d bytes."%(sys.getsizeof(num))
print "Size of dec is in %d bytes."%(sys.getsizeof(dec))
print "Size of ext double is in %d bytes."%(sys.getsizeof(ext))
print "Size of ltr char is in %d bytes."%(sys.getsizeof(ltr))
print "Size of the string is in %d bytes."%(sys.getsizeof(string))
print "Size of struct is in %d bytes."%(sys.getsizeof(struct()))
Size of num is in 12 bytes.
Size of dec is in 16 bytes.
Size of ext double is in 16 bytes.
Size of ltr char is in 22 bytes.
Size of the string is in 54 bytes.
Size of struct is in 32 bytes.

Case Study 1: PageNo.:4.52

In [74]:
#Convert number of days to months and days

#Variable Declaration
m=0
d=0

#User Input
nd=input("Enter the number of days= ")


m=nd/30
d=nd%30

#Result
print "Number of months...", m
print "Number of days...", d
Enter the number of days= 215
Number of months... 7
Number of days... 5

Case Study 2: Page No.:4.53

In [75]:
#Calculate Salesman Salary

#Variable Declaration
BASIC=2500
BONUS_RATE=200
COM=0.02

qty=0
GS=price=Bonus=Comm=0.0
#User input
qty,price=input("Enter the number of items Sold and Price...: ")

Bonus=BONUS_RATE*qty
Comm=COM*qty*price

GS=BASIC+Bonus+Comm

print "Bonus.... %6.2f"%(Bonus)

print "Commission.... %6.2f"%(Comm)

print "Gross Salary... %6.2f"%(GS)
Enter the number of items Sold and Price...: 10,200
Bonus.... 2000.00
Commission....  40.00
Gross Salary... 4540.00

Case Study 3: Page No.:4.54

In [77]:
#Post increment using different storage classes

#Defining function
def postinc():
    x=1
    print "X = %d" %(x)
    x=x+1
    
#Call Function
postinc()
postinc()
postinc()
X = 1
X = 1
X = 1