# 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
# Another simple illustration of a for loop
print "Countdown"
for i in range(10,-1,-1):
print i
print "\n\nBlastoff!"
# Calculating the squares of integers 1 through 10
for i in range (1,11,1):
print "%d squared = %d"%(i,i*i)
# 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)
# 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)
# 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)
# 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)
# 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)
# 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)
# 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
# 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
# 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)
# 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
# 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