9.15. Exercises¶
For each word in the list
verbs
, add an -ing ending. Overwrite the old list so thatverbs
has the same words withing
at the end of each one.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
andlower
. Assign each course inclasses
to the correct list,upper
orlower
. HINT: remember, you can convert some strings to different types!Starting with the list myList = [76, 92.3, ‘hello’, True, 4, 76], write Python statements to do the following:
Append “apple” and 76 to the list.
Insert the value “cat” at position 3.
Insert the value 99 at the start of the list.
Find the index of “hello”.
Count the number of 76s in the list.
Remove the first occurrence of 76 from the list.
Remove True from the list using
pop
andindex
.
The module
keyword
determines if a string is a keyword. e.g.keyword.iskeyword(s)
wheres
is a string will return eitherTrue
orFalse
, depending on whether or not the string is a Python keyword. Import thekeyword
module and test to see whether each of the words in listtest
are keywords. Save the respective answers in a list,keyword_test
.The
string
module provides sequences of various types of Python characters. It has an attribute calleddigits
that produces the string ‘0123456789’. Import the module and assign this string to the variablenums
. Below, we have provided a list of characters calledchars
. Usingnums
andchars
, produce a list calledis_num
that consists of tuples. The first element of each tuple should be the character fromchars
, 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
and will have a list
representation of
[[0, 1, 2], [3, 4, 5]]
Its transpose will be
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
All consonants at the start of the string are moved to the end. (Treat ‘y’ is a vowel here.)
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.
- ["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.
Q-1: What will be the value of a after the following code has executed?
a = ["holiday", "celebrate!"]
b = a
b.append("company")
You are supplied with a string s
below. Using python and not hard coding…Remove white space from the beginning and end, make the string lower case and replace all the words ‘cat’ with ‘dog’. This new string should be saved as newS
. The final string should look like newS=’the dog ate the dog food, it was yummy!’
Write a program that will accept words from a user, one at a time, until the user types the word ‘quit’. Start with an empty list, and append each new word to that list. After the user types quit, print out the list of words.
First copy your code from the previous problem. This problem will again ask the user for a bunch of words and print out the words. Then it will sort the words using the bubble sort technique we learned in class (no other sort methods allowed!) Print out the sorted list.
- [0] [0]
- nilai a[0] sudah diubah
- [0] [2]
- nilai a[0] sudah diubah
- [2] [0]
- a dan b sama lho
- [2] [2]
- tepat, karena a dan b sama mengubah isi dari a juga mengubah b
Q-1: Program berikut ini akan menghasilkan output:
a = [0]
b = a
a[0] = 2
print(a, b)
Test
Write a list myfriends with e friend names. Clone myfriends to mypeople. Add 2 family names to mypeople and print both lists.
Write a list named myfriends 3 friend names. Clone myfriends to a new list called mypeople. Add 2 new names of family members to mypeople. Print both myfriends and mypeople.
Write one line of code to combine the list below into a word and then print it.
Consider the list below. Using the del
Python statement, delete the “Yoda” element from the list. Do not create a new list. You’re supposed to directly modify the star wars
list.
Consider the list below. Do the following task:
Create a new list:
star_wars_copy
that is a copy ofstar_wars
(use Cloning, Section 9.6).Add a new value “Darth Vader” to
star_wars_copy
after the “Han Solo” entry (see Section 9.2). Leave the original liststar_wars
intact.
Consider the list below called words
. Using the accumulator pattern on the lists (Section 9.10), create a new list
called wordings
that adds the ending -ing to each of the words in the “words” list. So your resulting wordings
list should be:
[“reading”, “working”, “walking”, “watching”, “drinking”, “surfing”]
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.