The Friday exams are cumulative, like the skill you're trying to build. That means any of the previous homework problems could be part of it. What we've done so far: chapters 1, 2 from Dawson. Reading assignment for this week: chapter 3 from Dawson. We start with a review of chapters 1, 2 from Dawson. What we know: a) how to run IDLE and develop Python programs with it (F5, or double-click as a standalone) b) how to read input from the user (as a string of characters) with raw_input(...) c) how to convert strings to integers and floating-point numbers with int(...), float(...) d) it has been noted that int(...) also works on floating-point numbers, truncating them to integers e) we know how to write arithmetical expressions with numbers and operators like +, -, *, / and % f) we know that *, /, % have the same priority, higher than + and - which also have the same priority only lower overall g) in the absence of parens the expressions are evaluated left to right h) integer operands always yield an integer result, but if one of the operands is a float the result is a float i) variables are names for memory locations where we can store values j) assignment statements are used to evaluate expressions and store their values in variables k) the name of a variable is by and large made of letters and digits and possibly underscore, leading character not a digit l) print takes a sequence of expressions separated by commas and prints them one by one, then moves the cursor to the next line m) the length of a string can be calculated via len(...); other meaningful functions: min(...), max(...), abs(...), pow(..., ...) n) strings can be sliced: if a = "wonderful" then a[6] is "f", a[2:5] is "nde" and a[5:] is the same as a[5:len(a)-1] o) + means concatenation for strings, "2" + 3 won't work, but "2" * 3 evaluates to "222" (repeated concatenation) There are a few things I didn't mention from chapter 2, will definitely come up soon, later this week though. Not yet essential. Today we will discuss some of the more complicated aspects of Homework One and we will start on booleans. First off, how do you calculate the biggest of two numbers a and b? The answer is this: a) first figure out the midpoint: (a + b) / 2 b) then figure out the distance between the two: abs(a - b) c) the largest number is half that distance north of the midpoint. But writing that: (a + b) / 2 + abs(a - b) / 2 is not correct if a and b are integers. Why? And how do we need to write it to get the correct result? Try formula above with a = 3 and b = 5 then try it again with a = 3 and b = 5 see if anything bad happens. The correct formula is ( a + b + abs(a - b) ) / 2 can you tell why it's always working? Second, the military time problem. We know that the user enters two times, t1 and t2, as strings. The first time might be "0900" and the second "1730". But it can be the other way around, too. We also know that we'll be turning these into h1 = int(t1[0:2]) and m1 = int(t1[3:]) representing t1 as a pair of integers (h1 and m1, the hour and the minutes, respectively) and the same for t2. Now the basic approach to this problem (where we assume that t1 is "earlier" than t2) converts both t1 and t2 in minutes (by multiplying the hours by 60 and adding the number of minutes) and calculates the difference (in minutes) between the two times as d = (h2 * 60 + m2) - (h1 * 60 + m1) [1] It's easy to convert this difference in hours (d / 60) and minutes (d % 60) so the only question that remains is: what formula would work if t2 is "earlier" than t1. In that case we are being told to consider t2 as being part of the following day. (Example: "1730" to "0900" means from 5:30pm today to 9:00am tomorrow.) To calculate the difference we need to have the same referential so the second time would be calculated with respect to the beginning of today, making it: h2 * 60 + m2 + 24 * 60 The 24 * 60 terms adds a whole day of minutes. Thus in this second case the formula for d is: d = (h2 * 60 + m2 + 24 * 60) - (h1 * 60 + m1) [2] So we have two formulas. Can they be merged? The first formula [1] gives a negative answer in the second case. The second formula [2] has an excess 24 * 60 in the first case. Hey, this gives us an idea! We can take the excess out from [2] when used in the first case with %. d = (h2 * 60 + m2 + 24 * 60) % (24 * 60) - (h1 * 60 + m1) [2'] Does this change in any way the validity of the formula when used on times from the second case? No, because 3 % 5 is 3 in other words x % y is always x when x < y. Which means that [2'] is the final formula for this problem. Now we return to expressions with numbers. Can we create an expression with numbers whose result is not a number? How about 1 < 2 is that an expression? Is the answer a number? No: the answer is true, a boolean. There are only two booleans (truth values): true and false. Other operators for numbers yielding booleans: <, <=, >, >= and ==. Is boolean a type, like int and float and str? Yes. What operations are there defined on booleans? There are three: and, or and not. Let's look at how they're defined: p q p and q p or q not q true true true true false true false false true true false true false true false false false false In terms of priority (precedence) the operators behave as follows: a) not is like the unary minus, binds first b) and is to or like * is to + for numbers (so in the absence of parens gets executed first). So let's try evaluating some expressions: a) false && false || true b) false && (false || true) We will discuss these in detail and give many more examples in class. Despite the apparent simplicity things can become challenging quickly, so we need patience and a clear mind. For example, how could you prove that the following expressions are equivalent, and what does that mean? this expression is logically equivalent to not a a == false a a == true a and false false a and true true a or true true a or false a a and (not a) false a or (not a) true a and (b or c) (a and b) or (a and c) not (a and b) (not a) or (not b) not (a or b) (not a) and (not b) The last two are called de Morgan's laws. We can use booleans to change the course of action in a program. Note the indentation: +---------------------------------------------------------------+ | month = int(raw_input("Enter a month number: ")) | | | | if month >= 1 and month <= 12: | | print "That's a valid month number." | | else: | | print "Sorry, valid month numbers are between 1 and 12." | +---------------------------------------------------------------+ Note that the else branch is optional. Note also that each branch can include any number of statements including if statements. So tomorrow we will discuss nested if's, elif's and mention another interesting use of booleans. --