9.15. Exercises

  1. For each word in the list verbs, add an -ing ending. Overwrite the old list so that verbs has the same words with ing at the end of each one.

  2. In XYZ University, upper level math classes are numbered 300 and up. Upper level English classes are numbered 200 and up. Upper level Psychology classes are 400 and up. Create two lists, upper and lower. Assign each course in classes to the correct list, upper or lower. HINT: remember, you can convert some strings to different types!

  3. Starting with the list myList = [76, 92.3, ‘hello’, True, 4, 76], write Python statements to do the following:

    1. Append “apple” and 76 to the list.

    2. Insert the value “cat” at position 3.

    3. Insert the value 99 at the start of the list.

    4. Find the index of “hello”.

    5. Count the number of 76s in the list.

    6. Remove the first occurrence of 76 from the list.

    7. Remove True from the list using pop and index.

    Show Comments
  4. The module keyword determines if a string is a keyword. e.g. keyword.iskeyword(s) where s is a string will return either True or False, depending on whether or not the string is a Python keyword. Import the keyword module and test to see whether each of the words in list test are keywords. Save the respective answers in a list, keyword_test.

  5. The string module provides sequences of various types of Python characters. It has an attribute called digits that produces the string ‘0123456789’. Import the module and assign this string to the variable nums. Below, we have provided a list of characters called chars. Using nums and chars, produce a list called is_num that consists of tuples. The first element of each tuple should be the character from chars, and the second element should be a Boolean that reflects whether or not it is a Python digit.

9.15.1. Contributed Exercises

The transpose of a matrix is flipped along its diagonal. E.g., an input matrix that looks like

\[\begin{split}\left[\begin{array}{ccc} 0 & 1 & 2\\ 3 & 4 & 5 \end{array}\right]\end{split}\]

and will have a list representation of

[[0, 1, 2], [3, 4, 5]]

Its transpose will be

\[\begin{split}\left[\begin{array}{ccc} 0 & 3\\ 1 & 4\\ 2 & 5 \end{array}\right]\end{split}\]

and will have a list representation of

[[0, 3], [1, 4], [2, 5]]

Compute the transpose of mat_a and mat_b below and store them in mat_at and mat_bt respectively.

Create a new list called sqrts that contains the square root of each number in the list numbers. Your solution should work for any number of elements in the list.

First, create an empty list, then add each value using append().

Pig latin is a language game or argot. The rules are

  1. All consonants at the start of the string are moved to the end. (Treat ‘y’ is a vowel here.)

  2. An ‘a’ is added to the end of the string.

For example, ‘string’ becomes ‘ingstra’.

Copy the list english into a new list called pig_latin and translate each word into pig latin. You may use + to create new strings but do not use append or + to build the list pig_latin.

Create an alias named cities that points to the same list as the existing municipalities variable. Then, create a list named towns that is a clone of cities. towns and cities should be independent objects and not aliases.

Once you have created the lists, change “Blacksville” in municipalities to “Town of Blacksville”. Print out the content of element 4 in each list.

Consider the code given to you below. It iterates through a list of 5 integers, and appends them to a list. Run the code first and see the result. Now, by only modifying line 3 change the code to instead of just adding an element to the list, it adds each number multiplied by two.

    Q-1: What will be the value of a after the following code has executed?

    a = ["holiday", "celebrate!"]
    b = a
    b.append("company")
    
  • ["holiday", "celebrate", "company"]
  • This is true. It is called aliasing (Section 9.5).
  • Nothing. There's an error.
  • There is no error in this code. Please check Section 9.5 (Aliasing) to understand why not.
  • ["holiday", "celebrate"]
  • That's incorrect. What we used here is something called aliasing. Both a and b variables refer to the same list. So either you append to a or b, you append to the same list.
You have attempted of activities on this page
Next Section - 9.16. Chapter Assessment - List Methods