Contents
- ./daysOfWeek.py
- ./fibs2.py
- ./fibs.py
- ./friends.py
- ./oldmac.py
./daysOfWeek.py 1/5
[top][prev][next]
# Example illustrating list operations:
# concatenation, subsequences and iteration
# by Sara Sprenkle
weekdays = ["Mon", "Tue", "Wed", "Thu", "Fri"]
weekend_days = ["Sat", "Sun"]
# combine two lists into one
days_of_week = weekdays + weekend_days
print("The Days of the Week:")
# iterate through elements of list
for day in days_of_week:
print(day)
print("\nAGAIN!")
# iterate through positions of list
for x in range(len(days_of_week)):
print(days_of_week[x])
print()
# reorganizing days of week to start on a Sunday
days_of_week = weekend_days[-1:] + weekdays + weekend_days[:1]
# weekdays:
extractedWeekdays = days_of_week[1:6]
print("Extracted weekdays:", extractedWeekdays)
extractWeekend = days_of_week[:1] + days_of_week[-1:]
print("Extracted weekend: ", extractWeekend)
extractWeekend2 = [days_of_week[0], days_of_week[-1]]
print("Extracted weekend: ", extractWeekend2)
extractWeekend3 = [days_of_week[0]] + [days_of_week[-1]]
print("Extracted weekend: ", extractWeekend3)
notExtractedWeekend = days_of_week[0] + days_of_week[-1]
print("Does not work: ", notExtractedWeekend)
./fibs2.py 2/5
[top][prev][next]
# Example of creating a list of the appropriate size
# Computes the first SIZE Fibonacci numbers
# Tradeoff of using more space (the list, rather than a few variables)
# for easier implementation
# Sara Sprenkle
SIZE = 20
print("This program generates the first", SIZE, "Fibonacci numbers")
# creates a list of size SIZE, containing all 0s
fibs = [0]*SIZE
#fibs[0] = 0
fibs[1] = 1
for x in range(2, len(fibs)):
newfib = fibs[x-1]+fibs[x-2]
fibs[x] = newfib
# print that list out, as a list
print(fibs)
# To print out the list, individually (instead of as a list)
print("Printing elements of list: ")
for num in fibs:
print(num, end=" ")
print()
./fibs.py 3/5
[top][prev][next]
# Example of appending to a list
# Computes the first SIZE Fibonacci numbers
# Tradeoff of using more space (the list, rather than a few variables)
# for easier implementation
# Sara Sprenkle
SIZE = 20
print("This program generates the first", SIZE, "Fibonacci numbers")
# create an empty list
fibs = []
# append the first two Fibonacci numbers
fibs.append(0)
fibs.append(1)
# compute the next Fibonacci numbers
for x in range(SIZE-2):
newfib = fibs[-1] + fibs[-2]
fibs.append(newfib)
# print the Fibonacci numbers as a list
print(fibs)
# To print out the list, individually (instead of as a list)
print("Printing elements of list: ")
for num in fibs:
print(num, end=" ")
print()
./friends.py 4/5
[top][prev][next]
# These are the people I know
# by Sara Sprenkle
friends = ["Alice", "Bjorn", "Casey", "Duane", "Elsa", "Farrah"]
for name in friends:
print("I know " + name + "!")
print(name, "is a friend of mine.")
print("Those are the people I know.")
# TODO: implement using a for loop that iterates over
# the _positions_ in the loop
./oldmac.py 5/5
[top][prev][next]
# Print out verses of the song Old MacDonald.
# Sara Sprenkle and CSCI-111
BEGIN_END = "Old McDonald had a farm"
EIEIO = ", E-I-E-I-O"
def main():
animals = ["cow", "pig", "duck", "dog"]
sounds = ["moo", "oink", "quack", "ruff"]
for i in range(len(animals)):
# consider ease of changing this code to print more verses
def print_verse(animal, sound):
"""
Prints a verse of Old MacDonald, plugging in the animal and sound
parameters (which are strings), as appropriate.
"""
print(BEGIN_END, EIEIO)
print("And on that farm he had a", animal, EIEIO)
print("With a", sound, ", ", sound, "here")
print("And a ", sound, ", ", sound, "there")
print("Here a", sound)
print("There a", sound)
print("Everywhere a", sound, ", ", sound)
print(BEGIN_END, EIEIO)
print()
if __name__ == "__main__":
main()
Generated by GNU Enscript 1.6.5.90.