Contents

  1. average5.py
  2. function_example.py
  3. module_example.py
  4. new_for.py
  5. simple_for.py
  6. sum5.py
  7. temp_table.py
  8. using_xrange.py
  9. xrange_analysis.py

average5.py 1/9

[
top][prev][next]
# This program adds up 5 numbers from the user.
# By CS111, 01.24.2011


NUM_NUMBERS=2

print "This program will average", NUM_NUMBERS,"numbers given by the user."

# keep track of running total
total = 0

# Repeat: get the five numbers from the user, 
# keep running total
for n in xrange(1, NUM_NUMBERS+1):
    num = input("Enter number " + str(n) + ": ")
    # update the running total
    total = total + num

average = float(total)/NUM_NUMBERS

# display average
print "The average is", average

function_example.py 2/9

[
top][prev][next]
# Examples using built-in functions
# Sara Sprenkle

x = 6.817654321
#x = 5.6512542

print "We start with x having value", x

# Call the function round with input x
# Then, save output of function call in variable roundx
roundx = round(x)
print "x rounded to the nearest int:", roundx

round2 = round(x, 1)
print "x rounded to the nearest tenth:", round2

a = round(x, 2)
print "x rounded to the nearest hundredth:", a

roundx = round(x, 3)
print "x rounded to the nearest thousandth:", roundx

print round(x, 4)

print "-"*40
print "x is of", type(x)

module_example.py 3/9

[
top][prev][next]
# Example of importing a module
# by Sara Sprenkle

# Alternative: could import math
# Would then need to prepend all constants, functions with math.
from math import *

i = 1j

# The equation e^(i pi) + 1 = 0
shouldbezero =  e ** (i * pi) + 1

print "e^(i pi) + 1 equals", shouldbezero


# practice using functions from modules
print "100^(1/2) =", sqrt(100)

new_for.py 4/9

[
top][prev][next]
# More xrange examples
# Sara Sprenkle

print "-------------- xrange(1, 15, 3) ------------"
for a in xrange(1,15,3):
    print a

print "-------------- xrange(5, -15, -5) ------------"
for b in xrange(5, -15, -5):
    print b

# Demonstrate these after handout ... 
    
# Won't display anything
print "-------------- xrange(5, -15, 5) ------------"
for counter in xrange(5, -15, 5):
    print counter

# Won't display anything
print "-------------- xrange(-5, 15, -5) ------------"
for counter in xrange(-5, 15, -5):
    print counter

print "-------------- xrange(5, 15, 1.5) ------------"
# Note that xrange expects integer values
for counter in xrange(5.5, 15, 1):
    print counter

simple_for.py 5/9

[
top][prev][next]
# Examples of for loops using xrange
# by Sara Sprenkle

# The "chorus" gets repeated 5 times
for i in xrange(5):
    print "You say 'hello'"
    print "And, I say 'goodbye'..."

    
num_repetitions = 5
   
print

# for loop with only one statement that gets repeated
for x in xrange(num_repetitions): print "Repeat the chorus!"

sum5.py 6/9

[
top][prev][next]
# This program adds up 5 numbers from the user.
# By CS111, 01.24.2011


NUM_NUMBERS=10

print "This program will add up", NUM_NUMBERS,"numbers given by the user."

# keep track of running total
total = 0

# Repeat: get the five numbers from the user, 
# keep running total
for n in xrange(1, NUM_NUMBERS+1):
    num = input("Enter number " + str(n) + ": ")
    # update the running total
    total = total + num

# display total
print "The total is", total

temp_table.py 7/9

[
top][prev][next]
# Print out the table of temperatures
# By CS111

# print out the labels

underline = "-"*6

print "%10s %10s %10s" % ( "Temp F", "Temp C", "Temp K" )
print "%10s %10s %10s" % ( underline, underline, underline)

# Better to calculate these conversions, but that's a lab problem

ftemp = -459.67
ctemp = -273.15
ktemp = 0

print "%10.1f %10.1f %10.1f" % (  ftemp, ctemp, ktemp)

ftemp = 0
ctemp = -17.77778
ktemp = 255.222

print "%10.1f %10.1f %10.1f" % (  ftemp, ctemp, ktemp)

ftemp = 32
ctemp = 0
ktemp = 273.15

print "%10.1f %10.1f %10.1f" % (  ftemp, ctemp, ktemp)


using_xrange.py 8/9

[
top][prev][next]
# Examples of using xrange, with different numbers of parameters
# by Sara Sprenkle
#

# TODO: Use constants


print "------------ xrange(10) ------------"
for x in xrange(10):
    print x

print "----------- xrange(5,10) -----------"
for y in xrange(5, 10):
    print y

print "----------- xrange(1,10,1) -------------"
for x in xrange(1, 10, 1):
    print x

    
# What happens if step is negative?
# What happens if stop < start?

xrange_analysis.py 9/9

[
top][prev][next]
# Example of for loops using xrange
# by Sara Sprenkle

# Question: what does xrange do?
for i in xrange(10):
   squared = i * i
   print i , "^2 =\t", squared

print i

# QUESTION FOR CLASS:
# How is i changing each time through the loop?


Generated by GNU enscript 1.6.4.