Chapter 7, The for loop

Program 7-1 , Page number: 119

In [1]:
# A simple illustration of for loop

for i in range(1,11,1):		# The loop starts with i=1 and continues until i<11 with its value increasing by 1 each time 
	print "This is line number %d"%i
This is line number 1
This is line number 2
This is line number 3
This is line number 4
This is line number 5
This is line number 6
This is line number 7
This is line number 8
This is line number 9
This is line number 10

Program 7-2 , Page number: 120

In [2]:
# Another simple illustration of a for loop

print "Countdown"

for i in range(10,-1,-1):
	print i
print "\n\nBlastoff!"
Countdown
10
9
8
7
6
5
4
3
2
1
0


Blastoff!

Program 7-3 , Page number: 121

In [3]:
# Calculating the squares of integers 1 through 10

for i in range (1,11,1):
	print "%d squared = %d"%(i,i*i)
1 squared = 1
2 squared = 4
3 squared = 9
4 squared = 16
5 squared = 25
6 squared = 36
7 squared = 49
8 squared = 64
9 squared = 81
10 squared = 100

Program 7-4 , Page number: 121

In [4]:
# A better looking program to calculate squares

print "Table of Results\n"
print "   i    i squared"
print "   -    ---------"

for i in range(1,11,1):
	print "%4d%13d"%(i,i*i)
Table of Results

   i    i squared
   -    ---------
   1            1
   2            4
   3            9
   4           16
   5           25
   6           36
   7           49
   8           64
   9           81
  10          100

Program 7-5 , Page number: 122

In [5]:
# Finding the sum of integers 1 through 100

sum = 0
n = 100

for i in range(1,n+1,1):		# The loop starts with i=1 and continues until i<11 with its value increasing by 1 each time 
	sum += i	

print "The sum of integers from  1 to %d is %d"%(n,sum)
print "\n\nThis is confirmed by the Gauss Method: %d"%(n*(n+1)/2)
The sum of integers from  1 to 100 is 5050


This is confirmed by the Gauss Method: 5050

Program 7-6 , Page number: 124

In [6]:
# Finding the sum of the integers 1 through MAX_VAL

# There is no #define command for defining constants
# We can use a function that returns the value (although it is not very efficient to do so)

def MAX_VAL():
	return 100			# The value of MAX_VAL

sum = 0

for i in range (0,MAX_VAL()+1,1):
	sum += i

print "The sum of the integers from 1 to %d is %d"%(MAX_VAL(),sum)
print "\n\nThis is confirmed by the Gauss Method : %d"%(MAX_VAL()*(MAX_VAL()+1)/2)
The sum of the integers from 1 to 100 is 5050


This is confirmed by the Gauss Method : 5050

Program 7-7 , Page number: 125

In [1]:
# Calculating the sum of integers 1 through n

sum = 0

print "Please type in a positive integer:",
n = 50
print n

for i in range (1,n+1,1):
	sum += i

print "\nThe sum of the integers from 1 to %d is: %d"%(n,sum)
Please type in a positive integer: 50

The sum of the integers from 1 to 50 is: 1275

Program 7-8 , Page number: 125

In [2]:
# Calculating the sum of integers low through high

sum = 0

print "Please type in the low bound:",
low = 3
print low
print "Please type in the high bound:",
high = 9
print high

for i in range(low,high+1,1):
	sum += i

print "\nThe sum of integers from %d to %d is: %d"%(low,high,sum)
Please type in the low bound: 3
Please type in the high bound: 9

The sum of integers from 3 to 9 is: 42

Program 7-9 , Page number: 127

In [3]:
# Calculating the sum of integers low through high with an exchange if necessary

sum = 0

print "Please type in the low bound:",
low = 30
print low
print "Please type in the high bound:",
high = 15
print high

if high < low :
	print "Oops! Wrong order."
	print "Never mind, I'll switch them."
	# Code for swapping using temporary variable
	temp = low
	low = high
	high = temp

for i in range(low,high+1,1):
	sum += i

print "\nThe sum of integers from %d to %d is: %d"%(low,high,sum)
Please type in the low bound: 30
Please type in the high bound: 15
Oops! Wrong order.
Never mind, I'll switch them.

The sum of integers from 15 to 30 is: 360

Program 7-10 ,Page number: 128

In [4]:
# Illustration of a nest of for loops

n = 5
for i in range(1,7,1):
	print "\n\nPlease type in a positive integer: ",
	print n

	sum = 0
	for j in range(1,n+1,1):
		sum += j
	print "\nThe sum of integers from 1 to %d is %d"%(n,sum)
	
	n += 5			# Just to match the exercise's values

Please type in a positive integer:  5

The sum of integers from 1 to 5 is 15


Please type in a positive integer:  10

The sum of integers from 1 to 10 is 55


Please type in a positive integer:  15

The sum of integers from 1 to 15 is 120


Please type in a positive integer:  20

The sum of integers from 1 to 20 is 210


Please type in a positive integer:  25

The sum of integers from 1 to 25 is 325


Please type in a positive integer:  30

The sum of integers from 1 to 30 is 465

Program 7-11 , Page number: 130

In [5]:
# Another illustration of a nest of for loops

print "How many values of n do you wish to enter? ",
runs = 3
print runs
print "\n\n"

n = 9

for i in range(1,runs+1,1) :
	print "\n\nPLease type in a positive integer: ",
	print n

	sum = 0
	for j in range(1,n+1,1) :
		sum += j
	print "\nThe sum of the integers from 1 to %d is: %d"%(n,sum)
	n *= 3			# Just a random increment
How many values of n do you wish to enter?  3





PLease type in a positive integer:  9

The sum of the integers from 1 to 9 is: 45


PLease type in a positive integer:  27

The sum of the integers from 1 to 27 is: 378


PLease type in a positive integer:  81

The sum of the integers from 1 to 81 is: 3321

Program 7-12 , Page number: 131

In [6]:
# Newton-Rapshon method for calculating square roots

def EPSILON() :
	""" USING AS A CONSTANT """
	return 0.0001

print "What number do you want to kate the square root of?",
n = 17.0
print n

while n < 0.0 :
	print "Please enter a non-negative number!"
	n = 3 					# Just a random positive number

# The for loop statement given in the example(from the book) is not possible to code in python
# So, breaking it down to while loop

guess = n / 2.0
while (guess * guess - n > EPSILON() or guess * guess - n < -EPSILON()) :
	guess = (guess + n/guess) / 2.0
	
print "I calculate the square root of %f to be %.4f"%(n,guess)
What number do you want to kate the square root of? 17.0
I calculate the square root of 17.000000 to be 4.1231

Program 7-13 , Page number: 133

In [7]:
# General addition table for a number n

print "What number do you want a table for?",
n = 10
print n

j = n
for i in range (0,n+1,1) :
	print "%3d + %3d = %3d"%(i,j,n)
	j -= 1
What number do you want a table for? 10
  0 +  10 =  10
  1 +   9 =  10
  2 +   8 =  10
  3 +   7 =  10
  4 +   6 =  10
  5 +   5 =  10
  6 +   4 =  10
  7 +   3 =  10
  8 +   2 =  10
  9 +   1 =  10
 10 +   0 =  10

Program 7-14 , Page number: 135

In [8]:
# Generate addition table for a number n, shorter loop

print "What number do you want a table for?",
n = 10
print n

# Since the range() function supports at max 3 arguments, no possible shortening is possilbe in python
j = n
for i in range (0,n+1,1):
	print "%3d + %3d = %3d"%(i,j,n)
	j -= 1
What number do you want a table for? 10
  0 +  10 =  10
  1 +   9 =  10
  2 +   8 =  10
  3 +   7 =  10
  4 +   6 =  10
  5 +   5 =  10
  6 +   4 =  10
  7 +   3 =  10
  8 +   2 =  10
  9 +   1 =  10
 10 +   0 =  10