Today I saw: Velocity, Asaf, Muhammad, Echo, Sungho, Myung-Hoo, Korey, Dan, Yi, Linwood, Robin, Leo and Rick in class. Missing (for various reasons) were: Helen, Joe, Nate, Mike (Lucero), Mike (Randall), Hiu Lam, Chaitanya and Henry. We started by reviewing the goal of the course: problem solving, express solutions clearly in a programming language. We reminded ourselves life is hard, mainly because it's full of problems that need to be solved. We looked at some of these slides: http://www.cs.indiana.edu/~dgerman/dp02.pptx (Mainly the Mr. Bean video and some of foreword to Scott Peck's "The Road Less Traveled") I've used this in class since about the Summer of 2001, when A201/A597 was taught in Java: http://www.cs.indiana.edu/classes/a201-dger/sum2001/roadless.html Regardless, it's important to understand the following: a) discipline is the basic set of tools required to solve life's problems; b) the tools of discipline are: * delaying of gratification * acceptance of responsibility * dedication to truth, and * balancing Solving problems is a painful process. The tools of discipline are "techniques of suffering". With them we can optimize the amount of pain. As they say: no pain, no gain; so, there is no problem solving without a bit of suffering. We can only hope to optimize, minimize the amount of suffering, through discipline. Next I asked you to provide feedback (anonymously) on how you feel about what we learned so far. You were to list the main things we covered and you learned and circle the emoticon that closest matched the summary you made. We then moved to the next topic: sequences. Sequences: elements in order. There are three types of sequences in Python: strings, tuples, lists. Strings are characters between " (double quote) or ' (single quote) delimiters. The empty string is: "" The function len(...) can determine the length of any sequence (string, tuple or list). At this point Asaf asked: are sets sequences? The answer was: no really. a) sequences pay attention to the order of their elements: "ab" is not the same as "ba" Sets don't care about that, so {a, b} is the same set as {b, a} so this was one difference. b) sequences don't require that the elements be unique: "bubble" is a perfectly adequate strings, whereas {'b', 'u', 'b', 'b', 'l', 'e'} as a set must really written as: {'b', 'u', 'l', 'e'}. However: there are set-like operators for sequences, for example the operator "in". "b" in "bubble" evaluates to True We also said we can and should implement sets with dictionaries (chapter 5). Meanwhile back to sequences we defined: a) tuples are sequences of elements separated by commas and grouped together with a pair of round parentheses (, and ). The term tuple originated as an abstraction of the sequence: single, double, triple, quadruple, quintuple, n-tuple. A 2-tuple is called a pair; a 3-tuple is a triple or triplet. The n can be any nonnegative integer. So the name is essentially a historical consequence, Python didn't invent it. Furthermore, by element we mean: -- a number (integer or floating-point) -- a boolean, or -- a sequence (string, tuple or list). So this definition is recursive (self-referential) and tuples can get quite complex. Here are some examples of tuples: () the empty tuple (3) a tuple with just one element, an integer (3, 4) a tuple with two elements ("bear", 3.14, (1, "baloo")) a tuple with three elements: a string, a floating-point number and another tuple b) lists are like tuples only the elements are grouped with a pair of square brackets [, and ]. So Velocity asked: what's the difference between lists and tuples? The answer was: a) lists are mutable (can be changed) b) tuples (like strings) are immutable Asaf asked: what's the need for tuples, then? The answer was: they're read-only lists. Sometimes read-only things are useful (protected against accidental misuse). Next we discussed slicing and indexing of sequences. For any sequence (string, tuple or list) a the following operations are defined: 1. a[i] is the element at index i in the list 2. a[i:j] is the subsequence starting at index i and ending (and including) index j-1, and a[i:] is the same as a[i:len(a)], that is, the subsequence that starts at index i to the end We mentioned that every sequence has its elements indexed (in two ways): a) from left to right, we have a zero-based indexing that goes up: 0, 1, 2, 3, ..., len(a)-3, len(a)-2, len(a)-1 b) from right to left we have a negative indexing starting at -1 and going down: -(len(a)), -(len(a)-1), -(len(a)-2), ..., -3, -2, -1 So if a = "bear" then a[1] is "e" and a[-2] is the exact same "e" Some examples: a = ("apple", 3.14, (3, 1, 2)) Then a[0] is "apple" and a[0][0] is "a". We notice the two-dimensional character of the structure. How can we even attempt to change a sequence's elements? Like this: if a is a sequence, then any attempt to assign a value to a[i] for a valid index i won't work if a is a string or a tuple but will work if a is a list. Some examples: a = [1, 2, 3] a[1] = (6, -1) --> now a is [1, (6, -1), 3] a = "bear" a[0] = "p" --> does not work! a = "p" + a[1:] --> now a is "pear" instead of "bear" More examples can be found in the reading assignment and later in lab and lecture notes.