For each word in the list
verbs, add an -ing ending. Overwrite the old list so that
verbshas the same words with
ingat 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,
lower. Assign each course in
classesto the correct list,
lower. 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
keyworddetermines if a string is a keyword. e.g.
sis a string will return either
False, depending on whether or not the string is a Python keyword. Import the
keywordmodule and test to see whether each of the words in list
testare keywords. Save the respective answers in a list,
stringmodule provides sequences of various types of Python characters. It has an attribute called
digitsthat 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, produce a list called
is_numthat 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
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_b below and store
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
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
+ to build the list
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.