Here is a quick outline of what will be covered on the Exam.
The solutions to the programming problems are available at the end of this document.
Sample Problems
Evaluate the following expressions. Note the data type that gets generated by each expression as well. You can check the solutions to these problems by running them to see how Python reacts.
Expression'a' + 'b''a' * 55 * 'a' + 5 * 'b'[1] + [2]['a', 'b'] + ['c', 'd'][2] * 4 + [4] * 2[1,2,3] + 3 * [4]99 % 35 + (10 % 3)100 > 5100 > 5 and 100 < 991 > 2 or 2 > 1not (1 > 2) and not (3 < 3)(True or False) and (False and True)x = 'giraffe'
x[2] # evaluate the result of this line of code'cat' + 'dog''cat' > 'dog'(99 * 2) < 200 or (100 > 50 * 2)True or False or False or TrueTrace the Output
Trace the output of the following programs (each shaded box below is a separate Python program.)
count = 0
while count < 10:
print ('Hello')
count += 1
x = 10
y = 0
while x > y:
print (x, y)
x = x - 1
y = y + 1
keepgoing = True
x = 100
while keepgoing:
print (x)
x = x - 10
if x < 50:
keepgoing = False
x = 45
while x < 50:
print (x)
for x in [1,2,3,4,5]:
print (x)
for p in range(1,10):
print (p)
for z in range(-500,500,100):
print (z)
x = 10
y = 5
for i in range(x-y*2):
print ("%", i)
for x in [1,2,3]:
for y in [4,5,6]:
print (x,y)
for x in range(3):
for y in range(4):
print (x,y,x+y)
c = 0
for x in range(10):
for y in range(5):
c += 1
print (c)
x = [1,2,3]
counter = 0
while counter < len(x):
print (x[counter] * '%')
for y in x:
print (y * '*')
counter += 1
for x in 'lamp':
print (str.upper(x))
x = 'one'
y = 'two'
counter = 0
while counter < len(x):
print (x[counter], y[counter])
counter+=1
y = ['apple','pear','peach','grapefruit']
for z in y:
if z < 'm':
print (str.lower(z))
else:
print (str.upper(z))
word = "pikachu"
for c in word:
print (c)
word = "pikachu"
for i in range(0, len(word)):
print (i, word[i])
word = "pikachu"
for i in range(len(word)-1, -1, -1):
print (i, word[i])
word = "pikachu"
for i in range(0, len(word), 2):
print (i, word[i])
word = "pikachu" print (word[0:len(word):2])
word = "hello, world" newword = word[0:2] + word[-2:] print (newword)
# Trace the output of the following program. Then rewrite the program using a “while”
# loop instead of a “for” loop. You don't need to re-write the list – just rewrite
# the looping portion of the program.
pokemon = ["squirtle", "pikachu", "charmander",
"bulbasaur", "meowth"]
for i in range(0, len(pokemon), 2):
print (i, pokemon[i])
##### solution
counter = 0
while counter < len(pokemon):
print (counter, pokemon[counter])
counter += 2
for name in ['apple', 'pear', 'peach', 'apricot', 'starfruit']:
if x >= 'a' and x <= 'm':
print (str.upper(name))
else:
print (str.lower(name))
Programming Challenges
Use the homework problems and programming challenges from the slides for practice as well.
Enter a number of days: 3 Day 1: Enter a weight: 180 Day 2: Enter a weight: 175 Day 3: Enter a weight: 170 Average weight for 3 days: 175.0
Enter a number of days: 3 Day 1: Enter a weight: 180 Day 2: Enter a weight: 0 Invalid, try again! Day 2: Enter a weight: -5 Invalid, try again! Day 2: Enter a weight: 175 Day 3: Enter a weight: 170 Average weight for 3 days: 175.0
N123 invalid N1234567890 invalid P12345678 invalid NXYZ!5678 invalid N12345678 valid
x = [1,2,3,4,5]
counter = 0
while counter < len(x):
print (x[counter])
counter += 1
x = ['a','b','c','d']
for y in x:
print (y)
x = 'python'
for y in x:
print (y)
Programming Problems – Solutions
total = 0
again = "yes"
while again == "yes":
p = float(input("Enter a price: "))
total += p
again = input("Enter another price? (yes or no): ")
total = 0
days = int(input("Enter a number of days: "))
for day in range(days):
prompt = "Day " + str(day) + ": Enter a weight: "
weight = input(prompt)
total += weight
print ("Average weight for", days, "days:", total/days)
n_number = intput("Enter N-number ")
if len(n_number) != 9:
print("invalid")
elif n_number[0] != "N":
print("invalid")
elif n_number[1:].isdigit() == False:
print("invalid")
else:
print("valid")
# create an empty list
colors = []
# prompt the user for 5 colors
counter = 0
while counter < 5:
# get a color from the user
color = input("Give me a color: ")
# put the color in the list
colors.append(color)
counter += 1
# sort the list
colors.sort()
print (colors)
# reverse the list
colors.reverse()
print (colors)
colors = []
# ask the user for an unlimited number of colors
keepgoing = True
while keepgoing == True:
# ask the user for a color
color = input('Give me a color: ')
# does the user want to exit?
if color == 'exit':
keepgoing = False
elif color in colors:
print ("Sorry, that's already in the list.")
else:
colors.append(color)
# sort the list
colors.sort()
print (colors)
x = [1,2,3,4,5]
for y in x:
print (y)
x = ['a','b','c','d']
counter = 0
while counter < len(x):
print (x[counter])
counter += 1
x = 'python'
counter = 0
while counter < len(x):
print (x[counter])
counter += 1
prices = []
# grab 5 values from the user ensuring that they are
# all greater than 0
keepgoing = True
# counter variable
total = 0
while keepgoing == True:
# grab a price
price = float(input("Give me a price: "))
# is the price greater than zero?
if price <= 0:
print ("Sorry, too low.")
else:
prices.append(price)
total += price
# see if we can get out of the loop
if len(prices) == 5:
keepgoing = False
print ("Average: ", total / 5)
print ("Highest: ", max(prices))
print ("Minimum: ", min(prices))
# special deal
if total > 100:
newtotal = total - min(prices)
print ("Congrats!")
print ("New total: ", newtotal)
print ("New average: ", newtotal / 4)