Assessment: Methods: Lists and Strings >> Python Basics
seqmut-1-2: Which method would you use to figure out the position of an item in a list?
Multiple Choice (assess_question4_1_1_2)
Score: 1.0 / 1
seqmut-1-3: Which method is best to use when adding an item to the end of a list?
Multiple Choice (assess_question4_1_1_3)
Score: 1.0 / 1
Write code to add ‘horseback riding’ to the third position (i.e., right before volleyball) in the list sports
.
sports = ['cricket', 'football', 'volleyball', 'baseball', 'softball', 'track and field', 'curling', 'ping pong', 'hockey']
sports.insert(2,'horseback riding')
ActiveCode (assess_ac4_1_1_4)
Result | Actual Value | Expected Value | Notes |
---|---|---|---|
Pass | [‘cri…key’] | [‘cri…key’] | Testing that sports is set correctly. |
Pass | ‘.insert(‘ | “sport…ing’)” | Testing that insert was used in your code. |
You passed: 100.0% of the tests
Score: 1.0 / 1
Write code to take ‘London’ out of the list trav_dest
.
trav_dest = ['Beirut', 'Milan', 'Pittsburgh', 'Buenos Aires', 'Nairobi', 'Kathmandu', 'Osaka', 'London', 'Melbourne']
trav_dest.remove('London')
ActiveCode (assess_ac4_1_1_5)
Result | Actual Value | Expected Value | Notes |
---|---|---|---|
Pass | [‘Bei…rne’] | [‘Bei…rne’] | Testing that trav_dest is set correctly. |
Pass | True | True | Testing that a method invocation was used in your code. |
You passed: 100.0% of the tests
Score: 1.0 / 1
Write code to add ‘Guadalajara’ to the end of the list trav_dest
using a list method.
trav_dest = ['Beirut', 'Milan', 'Pittsburgh', 'Buenos Aires', 'Nairobi', 'Kathmandu', 'Osaka', 'Melbourne']
trav_dest.append("Guadalajara")
ActiveCode (assess_ac4_1_1_6)
Result | Actual Value | Expected Value | Notes |
---|---|---|---|
Pass | [‘Bei…ara’] | [‘Bei…ara’] | Testing that trav_dest is set correctly. |
Pass | ‘+’ | ‘trav_…ara”)’ | Testing that you are not using concatenation (+). |
Pass | ‘.’ | ‘trav_…ara”)’ | Testing that a method invocation was used in your code. |
You passed: 100.0% of the tests
Score: 1.0 / 1
winners
so that they are in alphabetical order from A to Z.
winners = ['Kazuo Ishiguro', 'Rainer Weiss', 'Youyou Tu', 'Malala Yousafzai', 'Alice Munro', 'Alvin E. Roth']
winners.sort()
ActiveCode (assess_ac4_1_1_7)
Result | Actual Value | Expected Value | Notes |
---|---|---|---|
Pass | [‘Ali… Tu’] | [‘Ali… Tu’] | Testing that winners is set correctly (Don’t worry about actual and expected values). |
You passed: 100.0% of the tests
Score: 1.0 / 1
winners
list so that it is now Z to A. Assign this list to the variable z_winners
.
winners = ['Alice Munro', 'Alvin E. Roth', 'Kazuo Ishiguro', 'Malala Yousafzai', 'Rainer Weiss', 'Youyou Tu']
winners.sort(reverse=True)
z_winners = winners
ActiveCode (assess_ac4_1_1_8)
Result | Actual Value | Expected Value | Notes |
---|---|---|---|
Pass | [‘You…nro’] | [‘You…nro’] | Testing that z_winners is set correctly (Don’t worry about actual and expected values). |
You passed: 100.0% of the tests