Chapter 4 - Arithmetic Operators

example 4.1, page no. 82

In [1]:
print "Enter number of preregistered students: ",
total = int(raw_input())
print "Enter number of students adding the course: ",
added = int(raw_input())
total = total + added
print "Total number of students: ", total
Enter number of preregistered students: 35
 Enter number of students adding the course: 3
 Total number of students:  38

example 4.2, page no. 84

In [2]:
firstName = "Jeff"
lastName = "Kent"
print "Your name is ", firstName + lastName
Your name is  JeffKent

example 4.3, page no. 84

In [3]:
print "Enter number of preregistered students: ",
total = int(raw_input())
print "Enter number of students adding the course: ",
added = int(raw_input())
total = total + added
print "How many students dropped? ",
dropped = int(raw_input())
total -= dropped
print "Total number of students: ", total
Enter number of preregistered students: 30
 Enter number of students adding the course: 3
 How many students dropped? 5
 Total number of students:  28

exampe 4.4, page no. 85

In [5]:
print "Enter number of preregistered students: ",
total = int(raw_input())
print "Enter number of students adding the course: ",
added = int(raw_input())
total = total + added
print "How many students dropped? ",
dropped = int(raw_input())
total -= dropped
print "Total number of students: ", total
print "Total tuition owed: $",((total + dropped) * 72)
Enter number of preregistered students: 30
 Enter number of students adding the course: 3
 How many students dropped? 5
 Total number of students:  28
Total tuition owed: $ 2376

example 4.5, page no. 88

In [6]:
firstOp = 10
secondOp = 4
result = firstOp / secondOp
print firstOp, " / ", secondOp, " = ", result
10  /  4  =  2

example 4.6, page no. 88

In [7]:
firstOp = 10.0
secondOp = 4.0
result = firstOp / secondOp
print firstOp, " / ", secondOp, " = ", result
10.0  /  4.0  =  2.5

example 4.7, page no. 89

In [8]:
firstOp = 10
secondOp = 4
result = float(firstOp) / float(secondOp) #converting both the variables to float
print firstOp, " / ", secondOp, " = ", result
10  /  4  =  2.5

example 4.8, page no. 89

In [9]:
print "Enter number of preregistered students: ",
total = int(raw_input())
print "Enter number of students adding the course: ",
added = int(raw_input())
total = total + added
print "How many students dropped? ",
dropped = int(raw_input())
total -= dropped
print "Total number of students: ", total
tuition = (total + dropped) * 72
print "Total tuition owed: $",tuition
print "Average tuition per enrolled student: $", (float(tuition)/float(total))
Enter number of preregistered students: 30
 Enter number of students adding the course: 3
 How many students dropped? 5
 Total number of students:  28
Total tuition owed: $ 2376
Average tuition per enrolled student: $ 84.8571428571

example 4.9, page no. 91

In [11]:
print "Enter radius of circle: ",
radius = float(raw_input())
area = 3.14159 * pow(radius, 2);
print "The area is ", area
Enter radius of circle: 6
 The area is  113.09724

example 4.10, page no. 92

In [12]:
print "Enter number of pennies to make change for: ",
total = int(raw_input())
dollars = total / 100
leftover = total % 100
quarters = leftover / 25
leftover %= 25
dimes = leftover / 10
leftover %= 10
nickels = leftover / 5
leftover %= 5
print "Dollars: ", dollars
print "Quarters: ", quarters
print "Dimes: ", dimes
print "Nickels: ", nickels
print "Pennies: ", leftover
Enter number of pennies to make change for: 387
 Dollars:  3
Quarters:  3
Dimes:  1
Nickels:  0
Pennies:  2
In [ ]: