Contents
- average5.py
- function_example.py
- module_example.py
- new_for.py
- simple_for.py
- sum5.mod.py
- sum5.py
- using_xrange.py
- xrange_analysis.py
average5.py 1/9
[top][prev][next]
# Example of Implementation of "Accumulator" Design Pattern
# Computes the average of 5 numbers, given by the user
# by CSCI 111
# A constant for how many numbers to input
NUM_TO_INPUT = 5
# initialize the accumulator variable
total = 0
# number of times to loop
for i in xrange(NUM_TO_INPUT):
num = input("Enter number: ")
# Update the accumulator
total += num
print "You inputted", num
print "\tRunning total: ", total
# Compute the average, protect against integer division
average = float(total) / NUM_TO_INPUT
print "The total of the inputted numbers", total
print "The average of the inputted numbers", 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.mod.py 6/9
[top][prev][next]
# keep track of running total
total = 0
# repeat: get user input for 5 numbers,
# keep running total
for i in xrange(1, 6):
userNum = input("Enter number " + str(i) + ": ")
# update running total
total = total + userNum
# Alternative: total += userNum
# display total
print "The total is", total
sum5.py 7/9
[top][prev][next]
# Example of Implementation of "Accumulator" Design Pattern
# Computes the total of 5 numbers, given by the user
# by CSCI 111
# A constant for how many numbers to input
NUM_TO_INPUT = 5
# initialize the accumulator variable
total = 0
# Number of times to loop
for i in xrange(NUM_TO_INPUT):
num = input("Enter number: ")
# update the accumulator variable
total += num
print "You inputted", num
print "\tRunning total: ", total
# display the result
print "The total of the inputted numbers", total
using_xrange.py 8/9
[top][prev][next]
# Examples of using xrange, with different numbers of parameters
# by Sara Sprenkle
#
# Part 2: 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.