Assessment: Lists and Strings >> Python Basics
sequences-10-1: What will the output be for the following code?
let = "z"
let_two = "p"
c = let_two + let
m = c*5
print(m)
✔️ Yes, because let_two was put before let, c has “pz” and then that is repeated five times.
Score: 1.0 / 1
Write a program that extracts the last three items in the list sports
and assigns it to the variable last
. Make sure to write your code so that it works no matter how many items are in the list.
sports = ['cricket', 'football', 'volleyball', 'baseball', 'softball', 'track and field', 'curling', 'ping pong', 'hockey']
last = sports[-3:]
Expand DifferencesExpand Differences
Result | Actual Value | Expected Value | Notes |
---|---|---|---|
Pass | [‘cur…key’] | [‘cur…key’] | Testing that the value of last is the last three items in sports. |
Pass | <__ma…ject> | True | Hardcode check |
You passed: 100.0% of the tests
Score: 1.0 / 1
Write code that combines the following variables so that the sentence “You are doing a great job, keep it up!” is assigned to the variable message
. Do not edit the values assigned to by
, az
, io
, or qy
.
by = "You are"
az = "doing a great "
io = "job"
qy = "keep it up!"
message = by+" "+az+io+", "+qy
Expand DifferencesExpand Differences
Result | Actual Value | Expected Value | Notes |
---|---|---|---|
Pass | ‘You are’ | ‘You are’ | Testing original variables. |
Pass | ‘doing a great ‘ | ‘doing a great ‘ | Testing original variables. |
Pass | ‘job’ | ‘job’ | Testing original variables. |
Pass | ‘keep it up!’ | ‘keep it up!’ | Testing original variables. |
Pass | ‘You a…t up!’ | ‘You a…t up!’ | Testing that the value of message is what was expected. |
Pass | ‘You a…t up!’ | ‘by = …+qy\n\n’ | Testing for hardcoding (Don’t worry about actual and expected values). |
You passed: 100.0% of the tests
Score: 1.0 / 1
sequences-10-2: What will the output be for the following code?
ls = ['run', 'world', 'travel', 'lights', 'moon', 'baseball', 'sea']
new = ls[2:4]
print(new)
✔️ Yes, python is a zero-index based language and slices are inclusive of the first index and exclusive of the second.
Score: 1.0 / 1
sequences-10-3: What is the type of m
?
l = ['w', '7', 0, 9]
m = l[1:2]
✔️ Yes, a slice returns a list no matter how large the slice.
Score: 1.0 / 1
sequences-10-4: What is the type of m
?
l = ['w', '7', 0, 9]
m = l[1]
✔️ Yes, the quotes around the number mean that this is a string.
Score: 1.0 / 1
sequences-10-5: What is the type of x
?
b = "My, what a lovely day"
x = b.split(',')
✔️ Yes, the .split() method returns a list.
Score: 1.0 / 1
sequences-10-6: What is the type of a
?
b = "My, what a lovely day"
x = b.split(',')
z = "".join(x)
y = z.split()
a = "".join(y)
✔️ Yes, the string is split into a list, then joined back into a string, then split again, and finally joined back into a string.