Exercises
○ Find out more about sequence objects using Python’s help facility. In the interpreter, type
help(str),help(list), andhelp(tuple). This will give you a full list of the functions supported by each type. Some functions have special names flanked with underscores; as the help documentation shows, each such function corresponds to something more familiar. For examplex.__getitem__(y)is just a long-winded way of sayingx[y].○ Identify three operations that can be performed on both tuples and lists. Identify three list operations that cannot be performed on tuples. Name a context where using a list instead of a tuple generates a Python error.
○ Find out how to create a tuple consisting of a single item. There are at least two ways to do this.
○ Create a list
words = ['is', 'NLP', 'fun', '?']. Use a series of assignment statements (e.g.,words[1] = words[2]) and a temporary variabletmpto transform this list into the list['NLP', 'is', 'fun', '!']. Now do the same transformation using tuple assignment.○ Read about the built-in comparison function
cmp, by typinghelp(cmp). How does it differ in behavior from the comparison operators?○ Does the method for creating a sliding window of n-grams behave correctly for the two limiting cases: n = 1 and n =
len(sent)?○ We pointed out that when empty strings and empty lists occur in the condition part of an
ifclause, they evaluate toFalse. In this case, they are said to be occurring in a Boolean context. Experiment with different ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access