The main topics on this worksheets are strings, integers, and list comprehensions.
Collaboration on worksheets is strongly encouraged.
The main course materials to refer to for this worksheet are:
(Lecture videos are not linked on worksheets, but are also useful to review while working on worksheets. Video links can be found in the course course Blackboard site.)
Here is a list of lists:
Q = [ ["Elizabeth","Victoria","Elizabeth"],
["Eleanor","Blanche","Margaret","Isabella","Anne","Catherine","Marguerite","Mary","Anne"],
["Padmini","Chennamma","Sultana","Holkar"],
["Artemisia","Kratesipolis","Nikaia","Olympias"]
]
In the list Q
, each item is itself a list. So Q[0]
refers to the item at index 0,
Q[0]
And these items that are lists can be further indexed. Here we take the last element of Q[0]
:
Q[0][-1]
Determine indexing or slicing expressions like this that match the following descriptions, and evaluate them to make sure they give the expected values:
Q
Q
with its first and last elements droppedQ
(index 1)"Anne"
"Anne"
. Rather, this problem is checking whether you can find it yourself and then translate what you see into a set of numerical indices to use.Q
We continue working with the list of lists Q
from problem 1.
Write a list comprehension to do each of the following:
Q
, which should be [3,9,4,4]
Q[1]
(use integer division to select the index, which selects the earlier of the two possibilites if a string has no middle letter)Q[1]
that contain the letter n
.Q[1]
that contain the letter n
at least two times.Q[3]
, but where every word has been replaced by the same number of stars, i.e. ['*********', '************', '******', '********']
Q[2]
but with any entry equal to "Sultana"
dropped and consecutive number labels added to the remaining ones, i.e.
["'1. Padmini", "2. Chennamma", "3. Holkar"]
Note that you should be writing a single line of code consisting of a list comprehension for each answer. There are other ways you could solve these problems with the Python we've learned, but this exercise is testing your ability to solve them with list comprehensions.
Look up code point numbers from the charts at https://www.compart.com/en/unicode/block or https://unicode.org/charts/ and use them to make Python code that uses \uXXXX
escape sequences to print the following strings. This means your code should only contain characters that appear on a standard US keyboard, but should produce the accented or non-latin characters shown here when it runs:
Pudín de limón
That's the "φ" model
Mathematicians often use the letter-like mathematical symbol ℂ for the set of complex numbers
Accented i and o are in the "Latin-1 supplement", while the letter phi is in the "Greek and Coptic" block.
Note: The default font used by Windows Powershell doesn't include ℂ, and instead of looking for another font which includes that character, it will just produce a symbol that looks like a square with a question mark inside. (That's a sign that a character is present, but can't be shown in the current font.) To confirm you've actually produced ℂ, you can copy the mystery character into the clipboard and paste it into another application (e.g. Discord, Word, the search bar in your web browser, ...).
Write a Python program indexer.py
that lets a user enter any number of strings, and which remembers each distinct string it has seen in a list. If it receives a string it has seen before, it tells the user the 0-based index where that string appears in the list.
More precisely, when the program is run, it starts with an empty list. It then enters an endless loop of doing the following:
->
and wait for the user to type a stringHere's a sample session of using the program:
->David
David = 0 (new)
->Grogu
Grogu = 1 (new)
->Groudon
Groudon = 2 (new)
->Lisa
Lisa = 3 (new)
->David
David = 0
->Tina
Tina = 4 (new)
->Chin-Sun
Chin-Sun = 5 (new)
->Grogu
Grogu = 1
->Chin-Sun
Chin-Sun = 5
->David
David = 0
Notice that the first time David
is entered, it is new and gets the index 0, but the next time it is entered, the program recognizes that it was seen before and again reports the index as 0.
Work on these if you finish the problems above.
?
as the string, instead of having that added to the list, the program prints the entire list (and then continues as usual, returning to the ->
prompt)-
, then the program interprets the rest of the string as one that should be removed from the list. So, for example, entering David
means to add David to the list or report its index if it is already there, while -David
means to remove David from the list.3
, how many contain the digit 7
? Can you write a single line of code that evaluates to the answer, using a list comprehension?