Skip to main content

Foundations of Python Programming: Functions First

Section 8.10 Chapter Assessment

Check your understanding

Checkpoint 8.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)
    
  • 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 8.10.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 8.10.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 8.10.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 8.10.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 8.10.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 8.10.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 8.10.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 8.10.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 8.10.10.

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

Checkpoint 8.10.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 8.10.12.

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

Checkpoint 8.10.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.
You have attempted of activities on this page.