Skip to main content

Section 9.13 Chapter Assessment

Check your understanding

Checkpoint 9.13.1.

    What will the output be for the following code?
    let = "z"
    let_two = "p"
    c = let_two + let
    m = c*5
    print(m)
    
  • zpzpzpzpzp
  • The order of concatenation matters.
  • zzzzzppppp
  • Think about the order that the program is executed in, what occurs first?
  • pzpzpzpzpz
  • Yes, because let_two was put before let, c has "pz" and then that is repeated five times.
  • pppppzzzzz
  • Think about the order that the program is executed in, what occurs first?
  • None of the above, an error will occur.
  • This is correct syntax and no errors will occur.

Checkpoint 9.13.2.

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.

Checkpoint 9.13.3.

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.

Checkpoint 9.13.4.

    What will the output be for the following code?
    ls = ['run', 'world', 'travel', 'lights', 'moon', 'baseball', 'sea']
    new = ls[2:4]
    print(new)
    
  • [’travel’, ’lights’, ’moon’]
  • When we take a slice of something, it includes the item at the first index and excludes the item at the second index.
  • [’world’, ’travel’, ’lights’]
  • When we take a slice of something, it includes the item at the first index and excludes the item at the second index. Additionally, Python is a zero-index based language.
  • [’travel’, ’lights’]
  • Yes, python is a zero-index based language and slices are inclusive of the first index and exclusive of the second.
  • [’world’, ’travel’]
  • Python is a zero-index based language.

Checkpoint 9.13.5.

    What is the type of m?
    l = ['w', '7', 0, 9]
    m = l[1:2]
    
  • string
  • Not quite, is it slicing or accessing an element?
  • integer
  • What is happening in the assignment statement for m?
  • float
  • What is happening in the assignment statement for m?
  • list
  • Yes, a slice returns a list no matter how large the slice.

Checkpoint 9.13.6.

    What is the type of m?
    l = ['w', '7', 0, 9]
    m = l[1]
    
  • string
  • Yes, the quotes around the number mean that this is a string.
  • integer
  • Not quite, look again at what is being extracted.
  • float
  • Not quite, look again at what is being extracted.
  • list
  • Not quite, is it slicing or accessing an element?

Checkpoint 9.13.7.

    What is the type of x?
    b = "My, what a lovely day"
    x = b.split(',')
    
  • string
  • Not quite; .split() returns a list, each of whose elements is a string.
  • integer
  • Not quite, look again at what types are present and what the result of .split() is.
  • float
  • Not quite, look again at what types are present and what the result of .split() is.
  • list
  • Yes, the .split() method returns a list.

Checkpoint 9.13.8.

    What is the type of a?
    b = "My, what a lovely day"
    x = b.split(',')
    z = "".join(x)
    y = z.split()
    a = "".join(y)
    
  • string
  • Yes, the string is split into a list, then joined back into a string, then split again, and finally joined back into a string.
  • integer
  • Not quite, look again at what types are present and what the result of .split() is.
  • float
  • Not quite, look again at what types are present and what the result of .split() is.
  • list
  • Not quite, think about what .split() and .join() return.

Checkpoint 9.13.9.

Write code to determine how many 9’s are in the list nums and assign that value to the variable how_many. Do not use a for loop to do this.

Checkpoint 9.13.10.

Write code that uses slicing to get rid of the the second 8 so that there are only two 8’s in the list bound to the variable nums.

Checkpoint 9.13.11.

Assign the last element of lst to the variable end_elem. Do this so that it works no matter how long lst is.

Checkpoint 9.13.12.

Assign the number of elements in lst to the variable num_lst.

Checkpoint 9.13.13.

Create a variable called wrds and assign to it a list whose elements are the words in the string sent. Do not worry about punctuation.

Checkpoint 9.13.14.

    Which one of the following print statements would display the above output in Codelens?
  • print(str1[0], str2[4], str3[9], str1[11])
  • Incorrect, this print statement gives the following output: G s y g
  • print(str3[0], str1[6], str2[1], str1[11])
  • Incorrect, this print statement gives the following output: H o t g
  • print(str2[1], str3[3], str1[1], str2[12])
  • Incorrect, this print statement gives the following output: t e o t
  • print(str3[0], str2[9], str3[9], str1[12])
  • Yes, this print statement gives the output displayed in Codelens: H e y !
  • print(str1[0], str2[4], str1[1], str2[11])
  • Incorrect, this print statement gives the following output: G s o u
  • print(str3[1], str3[5], str2[1], str1[12])
  • Incorrect, this print statement gives the following output: o e t !

Checkpoint 9.13.15.

The above program currently adds elements from one list to another char by char. We want to change this program so our output matches the below Codelens screenshot.
What function would we use to do this?
What line should we use this function on?

Checkpoint 9.13.16.

Consider the above Codelens screenshot. The program that created this output split the string into a list of tokens.
The function split() used as the delimiter to separate the elements and it resulted in elements.
You have attempted of activities on this page.