CSc 250: Lab 8

This lab was written and designed to be completed during lab-time for cs250. This lab is not graded for correctness. However, you should work through all of the problems, as it will help you on assignments and exams. If you cannot complete the lab in the alloted time, feel free to continue at home.

This particular lab was designed to be completed on-paper!

Problem 1 - Lists

This problem has several sequences of python commands. Below each sequence of commands, write what will be printed to stdout.

(A)

names = ['Russ', 'Kevin', 'Kyrie', 'Klay', 'Eric']
print(names[0] + ' ' + names[2])




(B)

names = ['Russ', 'Kevin', 'Kyrie', 'Klay', 'Eric']
print(names[0][0] + names[1][1] + names[2][2] + names[3][3])




(C)

names = ['Russ', 'Kevin', 'Kyrie', 'Klay', 'Eric', 'Bron', 'Devin']
index = len(names) - 1
for i in range(len(names)):
    print( names[i] + ' | ' + names[index] )
    index = index - 1





(D)

names_a = ['Russ', 'Kevin', 'Kyrie', 'Dray']
names_b = ['Klay', 'Eric', 'Bron', 'Devin']
index = 0
len_a = len(names_a)
while index < len_a:
    names_a.insert( index, names_b[index] )
    index = index + 1
print(names_a)





Problem 2 - Dictionaries

This problem has several sequences of python commands. Below each sequence of commands, write what will be printed to stdout.

(A)

names = { 0:'Russ', 1:'Lebron', 2:'Steph', 3:'Klay' }
print( names )




(B)

names = { 0:'Russ', 1:'Lebron', 2:'Steph', 3:'Klay' }
print( list( names.keys() ) )




(C)

names = { 0:'Russ', 1:'Lebron', 2:'Steph', 3:'Klay' }
print( list( names.values() ) )




(D)

names = { 'UCLA':'Russ', 'UA':'Gordon', 'WSU':'Klay', 'UK':'Eric' }
name_list = [ names['UCLA'] , names['UK'] ]
print(name_list)




(E)

names = { 'UCLA':'Russ', 'UA':'Gordon', 'WSU':'Klay', 'UK':'Eric' }
val_list = list(names.values())
val_list.sort()
val_list.append('Durant')
print(val_list)




(F)

heroes = { 'Batman':'Christian Bale', 'Cordell Walker':'Chuck Norris', 'Thor':'Chris Hemsworth', 'Chuck Norris':'Chuck Norris' }
print( heroes[ 'Cordell Walker' ] )




(G)

heroes = { 'Batman':'Christian Bale', 'Cordell Walker':'Chuck Norris', 'Thor':'Chris Hemsworth', 'Chuck Norris':'Chuck Norris' }
print( heroes[ heroes[ 'Cordell Walker' ] ] )




(H)

heroes = { 'Batman':'Christian Bale', 'Cordell Walker':'Chuck Norris', 'Thor':'Chris Hemsworth', 'Chuck Norris':'Chuck Norris' }
chars = heroes['Batman'][5] + heroes[heroes['Cordell Walker']][7] + heroes['Thor'][8]
print(chars)




(I)

names = { 'UCLA':'Russ', 'UA':'Gordon', 'WSU':'Klay', 'UK':'Eric' }
for key,val in names.items():
    if key.isupper():
        if val.find('r') != -1:
            print(key + ' --> ' + val)




Solutions will not be posted to the site - you can check them yourself!