Chapeter 2 : Variables

Example 2.1, Page No 29

In [2]:
iNo = raw_input("Specify Quantity: ")
print iNo
Specify Quantity: 10
10

Example 2.2, Page No 31

In [3]:
iNo = raw_input("Specify Quantity: ")
print "You entered", iNo
specify quantity22
You entered 22

Example 2.3, Page No 32

In [4]:
iNo = raw_input("specify quantity: ")
dPrice = raw_input("Specify unit Price: ")
print "You entered the quantity ", iNo , " and the price " , dPrice
specify quantity: 5
Specify unit Price: 12.45
You entered the quantity  5  and the price  12.45

Example 2.4, Page No 36

In [1]:
dTaxPerc = 25.0
iNo = int(raw_input("Specify Quantity: "))
dUnitPr = float(raw_input("Specify Unit Price: "))
dPriceExTax = dUnitPr * iNo
dTax = dPriceExTax * dTaxPerc / 100
dCustPrice = dPriceExTax + dTax
print "INVOICE"
print "========"
print "Quantity: ", iNo
print "Price per Unit: ", dUnitPr
print "Total Price: ", dCustPrice
print "Tax: ",dTax
Specify Quantity: 5
Specify Unit Price: 12.4
INVOICE
========
Quantity:  5
Price per Unit:  12.4
Total Price:  77.5
Tax:  15.5

Example 2.5, Page No 38

In [2]:
iNoOfSec = int(raw_input("Speicfy No. of Seconds: "))
iNoOfMin = iNoOfSec / 60
iSecLeft = iNoOfSec % 60
iNoOfHours = iNoOfMin / 60
iMinLeft = iNoOfMin % 60
print "Number of hours = ", iNoOfHours
print "Number of Minutes = ", iNoOfMin
print "Number of Seconds = ", iSecLeft
Speicfy No. of Seconds: 10000
Number of hours =  2
Number of Minutes =  166
Number of Seconds =  40

Example 2.6, Page No 41

In [1]:
import random
iNo = 5
iRoll1 = random.randint(0,100) % 6 + 1
iRoll2 = random.randint(0,100) % 6 + 1
iRoll3 = random.randint(0,100) % 6 + 1
iRoll4 = random.randint(0,100) % 6 + 1
iRoll5 = random.randint(0,100) % 6 + 1
dAverage = float(iRoll1 + iRoll2 + iRoll3 + iRoll4 + iRoll5) / iNo;
print "Number of Rolls: ", iNo
print "Average Score: ", dAverage
Number of Rolls:  5
Average Score:  3.0
In [ ]: