Contents

  1. daysOfWeek.py
  2. fibs2.py
  3. fibs.py

daysOfWeek.py 1/3

[
top][prev][next]
# Example illustrating list operations: concatenation and iteration
# by Sara Sprenkle

weekDays = ["Mon", "Tue", "Wed", "Thu", "Fri"]
weekendDays = ["Sat", "Sun"]

# combine two lists into one
daysOfWeek = weekDays + weekendDays

print "The Days of the Week:"

# iterate through elements of list
for day in daysOfWeek:
    print day

print "\nAGAIN!"

# iterate through positions of list
for x in xrange(len(daysOfWeek)):
    print daysOfWeek[x]

fibs2.py 2/3

[
top][prev][next]
# Example of creating a list of the appropriate size
# Computes the first SIZE Fibonacci numbers
# Sara Sprenkle

SIZE = 15

print "This program generates the first", SIZE, "Fibonacci numbers"

# creates a list of size 15, containing elements 0 to 14
fibs = range(SIZE) 

fibs[0] = 1
fibs[1] = 1

for x in xrange(2,SIZE):
    newfib = fibs[x-1]+fibs[x-2]
    fibs[x] = newfib

#for num in fibs:
#    print num

print fibs

fibs.py 3/3

[
top][prev][next]
# Example of appending to a list
# Computes the first SIZE Fibonacci numbers
# Sara Sprenkle

SIZE = 15

print "This program generates the first", SIZE, "Fibonacci numbers"

# create an empty list
fibs = []

# append the first two Fibonacci numbers
fibs.append(1)
fibs.append(1)

# compute the next 13 Fibonacci numbers
for x in xrange(2,SIZE):
    newfib = fibs[x-1]+fibs[x-2]
    fibs.append(newfib)


# print the Fibonacci numbers as a list
print fibs


# Tradeoff of using more space (the list) for easier writing

Generated by GNU enscript 1.6.4.