Chapter 2: First Steps in Programming¶

Program 2.1, page no. 27¶

In [1]:
print "My salary is $10000"  My salary is$10000


Program 2.2, page no. 29¶

In [2]:

salary = 10000; #Declare and store 10000 in variable called salary
print "My salary is %d." %salary

My salary is 10000.


Program 2.3, page no. 31¶

In [3]:
brothers = 7 #declaring variable & storing value
brides = 7 #declaring variable & storing value

print "%d brides for %d brothers" %(brides, brothers)

7 brides for 7 brothers


Program 2.4, page no. 34¶

In [4]:
cats = 2
dogs = 1
ponies = 1
others = 46

total_pets = cats + dogs + ponies + others;

print "We have %d pets in total\n" %total_pets

We have 50 pets in total



Program 2.5, page no. 37¶

In [5]:
cookies = 5
total_eaten = 0
eaten = 2
total_eaten = total_eaten + eaten

eaten = 3
total_eaten = total_eaten + eaten
print "I have eaten %d more. Now there are %d cookies left\n" %(eaten, cookies)
print "Total energy consumed is %d calories.\n" %(total_eaten*cookie_calories)

I have eaten 2 cookies. There are 3 cookies left
I have eaten 3 more. Now there are 0 cookies left

Total energy consumed is 625 calories.



Program 2.6, page no. 39¶

In [6]:
cookies = 45
children = 7


You have 7 children and 45 cookies

There are 3 cookies left over.



Program 2.7, page no. 48¶

In [7]:
plank_length = 10.0
piece_count = 4.0
piece_length = 0.0

piece_length = plank_length/piece_count;
print "A plank %f feet long can be cut into %f pieces %f feet long.\n" %(plank_length, piece_count, piece_length)

A plank 10.000000 feet long can be cut into 4.000000 pieces 2.500000 feet long.



Program 2.8, page no. 51¶

In [8]:
radius = 0.0
diameter = 0.0
circumference = 0.0
area = 0.0
Pi = 3.14159265

print "Input the diameter of the table:",
diameter = float(raw_input())

print "\nThe circumference is %0.2f" %circumference
print "\nThe area is %0.2f\n" %area

Input the diameter of the table:6

The circumference is 18.85

The area is 28.27



Program 2.9, page no. 54¶

In [9]:
PI = 3.14159

diameter = 0.0
circumference = 0.0
area = 0.0

print "Input the diameter of a table:",
diameter = float(raw_input())

print "\nThe circumference is %.2f. " %circumference
print "\nThe area is %.2f.\n" %area

Input the diameter of a table:6

The circumference is 18.85.

The area is 28.27.



Program 2.10, page no. 55¶

In [10]:
diameter = 0.0
Pi = 3.14159

print "Input the diameter of the table:",
diameter = float(raw_input())

print "\nThe circumference is %.2f." %(2.0*Pi*radius)

Input the diameter of the table:6

The circumference is 18.85.

The area is 28.27.



example 2.12, page no. 59¶

In [11]:
import sys

print "Size of Integer is: " + str(sys.getsizeof(int()))
print "Size of Float is: " + str(sys.getsizeof(float()))
print "Size of Long is: " + str(sys.getsizeof(long()))
print "Size of String is: " + str(sys.getsizeof(str()))

Size of Integer is: 24
Size of Float is: 24
Size of Long is: 24
Size of String is: 37


Program 2.13, page no. 60¶

In [12]:
Revenue_Per_150 = 4.5
JanSold = 23500
FebSold = 19300
MarSold = 21600
RevQuarter = 0.0

QuarterSold = JanSold + FebSold + MarSold

print "Stock sold in\n Jan: %d\n Feb: %d\n Mar: %d\n" %(JanSold, FebSold, MarSold)
print "Total stock sold in first quarter: %d\n" %QuarterSold

RevQuarter = QuarterSold/150*Revenue_Per_150
print "Sales revenue this quarter is:$%.2f\n" %RevQuarter  Stock sold in Jan: 23500 Feb: 19300 Mar: 21600 Total stock sold in first quarter: 64400 Sales revenue this quarter is:$1930.50



Program 2.15, page no. 68¶

In [13]:
first = 'T'
second = 63

print "The first example as a letter looks like this - ", first
print "The first example as a number looks like this - ", ord(first)
print "The second example as a letter looks like this - ", chr(second)
print "The second example as a number looks like this - %d\n" %second

The first example as a letter looks like this -  T
The first example as a number looks like this -  84
The second example as a letter looks like this -  ?
The second example as a number looks like this - 63



Program 2.16, page no. 69¶

In [14]:
first = 'A'
second = 'B'
last = 'Z'
number = 40
ex1 = ord(first) + 2
ex2 = ord(second) - 1
ex3 = ord(last) + 2

print "Character values \t %-5c%-5c%-5c\n" %(chr(ex1), chr(ex2), chr(ex3))
print "Numerical equivalents\t %-5d%-5d%-5d\n" %(ex1, ex2, ex3)
print "The number %d is the code for the character %c" %(number, chr(number))

Character values 	 C    A    \

Numerical equivalents	 67   65   92

The number 40 is the code for the character (


Program 2.17 (combines 2.18 also), page no. 80¶

In [15]:
shorty = 0.0
lofty = 0.0
feet = 0.0
inches = 0.0
shorty_to_lofty = 0.0
lofty_to_tree = 0.0
inches_per_foot = 12.0

print "Enter Lofty's height to the top of his/her head, in whole feet: ",
feet = float(raw_input())
print "...and then inches: ",
inches = float(raw_input())
lofty = feet*inches_per_foot + inches

print "Enter Shorty's height up to his/her eyes, in whole feet: ",
feet = float(raw_input())
print "... and then inches: ",
inches = float(raw_input())
shorty = feet*inches_per_foot + inches

print "Enter the distance between Shorty and Lofty, in whole feet: ",
feet = float(raw_input())
print "... and then inches: ",
inches = float(raw_input())
shorty_to_lofty = feet*inches_per_foot + inches

print "Finally enter the distance from Lofty to the tree to the nearest foot: ",
feet = float(raw_input())
lofty_to_tree = feet*inches_per_foot

tree_height = shorty + (shorty_to_lofty + lofty_to_tree)*(lofty-shorty)/shorty_to_lofty

print "The height of the tree is %ld feet and %ld inches.\n" %(tree_height/inches_per_foot, tree_height% inches_per_foot)

Enter Lofty's height to the top of his/her head, in whole feet: 6
...and then inches: 2
Enter Shorty's height up to his/her eyes, in whole feet: 4
... and then inches: 6
Enter the distance between Shorty and Lofty, in whole feet: 5
... and then inches: 0
Finally enter the distance from Lofty to the tree to the nearest foot: 20
The height of the tree is 12 feet and 10 inches.