For this lab and the prelab, you may work with a partner. You must both be there whenever you are working on the assignment material.
This should execute the example program in the terminal window. If you ever want to exit or quit early, you can type Ctrl + c.
and then look at the first few things it prints out, you'll see that the set class has two "constructors">>> help(set)
that is, two ways for creating new set objects. The first is used to create a new empty set. The second we've used with lists and strings to create new sets with some initial values.set() -> new empty set object set(iterable) -> new set object
The definition for this second constructor says that it takes as a parameter an object that is iterable. Some classes/objects are iterable and some are not. iterable classes/objects contain functionality that allow us to iterate over the elements in the list. For example, we can iterate over the items in an iterable object using a for loop:
data could be any iterable item. strings, lists and sets are all iterable, so we could assign any of these to data above and the loop would work (try it out if you're curious). Similarly, since the second constructor to set takes something that is iterable we could use any of these to create a new set. This should explains why when we create a set from a string, as in set("abcd") we get a set consisting of the four characters in the string and not the string itself.for item in data: print(item)
Once you're comfortable with this idea, write a function called iterable_to_string that takes a single parameter, which is some iterable object, and returns a string consisting of each item in the iterable object converted to a string using str and concatenated together, separated by a space. For example, here are a few calls to this function:
Notice that all of the strings returned actually have a space at the end as well. Your function doesn't have to do this, however, it's fine if it does (an easy way to implement this function results in that behavior).>>> iterable_to_string("abcd") 'a b c d ' >>> iterable_to_string([4, 3, 2, 1]) '4 3 2 1 ' >>> iterable_to_string(set([4, 3, 2, 1])) '1 2 3 4 '
Turn in your code for this function on a piece of paper with your name on it for your prelab. (If you are working with a partner, only hand in one sheet with both names on it.) This is the only thing you are required to turn in for the prelab this time.