250 likes | 383 Views
Today. We have a TA now – Nancy Hwang. Please attend the lab today to meet her. She is ready for your questions! Quiz next week in the lab. More detail on next slide. Boolean operators Conditional expressions. Quiz 1.
E N D
Today • We have a TA now – Nancy Hwang. Please attend the lab today to meet her. She is ready for your questions! • Quiz next week in the lab. More detail on next slide. • Boolean operators • Conditional expressions CISC101 - Prof. McLeod
Quiz 1 • A sample quiz 1 is linked to the grading page on the course web site. • Everything up to and including today’s lecture except that conditionals (“if” statements) are not on the quiz. Exercises 1 and 2 are fair game. You will need to understand conditionals for the assignment. • 45 minutes on paper – no aids. Pen or pencil. First part of lab. Tell Nancy if you cannot write in the first hour so she will expect you later in the lab. • T/F, short answer, read code (“what is the output”), write code (obtain something, calculate something else, display result). • Returned in class, marks in Moodle (eventually!). CISC101 - Prof. McLeod
Also… • Videos are now being streamed through links in our Moodle site. • Let me know if you have any problems! CISC101 - Prof. McLeod
Unary Boolean Operator • One unary boolean operator: not • Sets True to False, and False to True (just like a NOT gate!) CISC101 - Prof. McLeod
Binary Boolean Operators > < >= <= == != greater than less than greater than or equal to less than or equal to equals not equals All of these result in a True or Falsebool value. and or a logical AND a logical OR CISC101 - Prof. McLeod
Binary Boolean Operators, Cont. > < >= <= == != greater than less than greater than or equal to less than or equal to equals not equals All of these must have a number on both sides or a string on both sides. These must have a bool value on both sides. and or a logical AND a logical OR CISC101 - Prof. McLeod
Binary Boolean Operators, Cont. • The interpreter is going to want both sides to be either numeric or both sides to be strings. • You will get an error if you try to compare a string to a number. CISC101 - Prof. McLeod
Precedence Rules • Precedence rules are expanded! • Unary operations (not and negation) first. • Math operators in usual order: ** * / // % + - • Binary boolean comparison operators: > >= < <= == != • Logical operator: and • Logical operator: or • Assignment operator: = CISC101 - Prof. McLeod
Precedence Rules, Cont. • Round brackets are still used to control precedence. • And, function invocations and variable substitution take place before all of the operators listed on the previous slide. • Can brackets force the assignment operator to go first? CISC101 - Prof. McLeod
Boolean Expression Examples 5 > 2 4 < 3 or 7 > 1 7 != 8 6 + 2 > 9 or 4 == 3 + 1 7 == 7 and 5 > 2 and 6 != 3 5 * 5 >= 5 ** 2 128 % 2 == 0 CISC101 - Prof. McLeod
Another Example – Evaluate: not(5 * 4 > 3) or 2 + 3 <= 5 and 6 == 2 not(20 > 3) or 2 + 3 <= 5 and 6 == 2 not True or 2 + 3 <= 5 and 6 == 2 False or 2 + 3 <= 5 and 6 == 2 False or 5 <= 5 and 6 == 2 False or True and 6 == 2 False or True and False False or False False CISC101 - Prof. McLeod
Expressions, Cont. • We’ve discussed literals, variables and operators. • We still have function/method calls, keywords and punctuation to discuss. • I’ll talk about conditionals first – so you can work on the assignment and Exercise 3. CISC101 - Prof. McLeod
So Far… • Without conditionals programs are linear: Obtain data from user Calculations… Display results etc. CISC101 - Prof. McLeod
Branching Algorithms • If a program could test a condition, then it would have a choice as to its path of execution: if true if false etc. CISC101 - Prof. McLeod
Conditionals or “Selection Statements” • Python has if, if-elseand chained if (if-elif-else) statements. • if statement syntax: if boolean_expression: statement1_when_true statement2_when_true statement3_when_true … • Example: if capacitance < 0 : print("Illegal capacitance") CISC101 - Prof. McLeod
if-else Statement • Syntax of if-elsestatement: if boolean_expression: statement(s)_when_true else : statement(s)_when_false • Example: if stress > maxStress / 1.5 : result = "failure" else : result = "pass" CISC101 - Prof. McLeod
if Statement, Cont. • You will often have to nest if statements: etc. CISC101 - Prof. McLeod
Nested if Statements • For example, if you have three numbers, a, b, & c and you want to print them to the screen in order: T F a < b T F T F b < c b < c T F T F a < c a < c abc cba acb cab bac bca See SortThree.py CISC101 - Prof. McLeod
if-elif Statements • In the code in SortThree.py, you might have noticed this construct: else : if a < c : • This kind of thing occurs so often that it can be shortened to: elif a < c : CISC101 - Prof. McLeod
if-elif Statements, Cont. • This leads to a common code structure often called a “chained if” construct: if condition1 : statement(s) elifcondition2 : statement(s) elifcondition3 : statement(s) else : statement(s) You can have as many elifs as you want. The else part is optional. CISC101 - Prof. McLeod
if-elif Statements, Cont. • There is nothing in this construct that you could not make with normal if-else statements. • For some kinds of conditionals, the if-elif might be easier to put together. • See Part B of assignment 1, for example. CISC101 - Prof. McLeod
“Linear” Program Demo • Write a program that uses just input, output and simple calculations. • How robust is the resulting program? CISC101 - Prof. McLeod
Area of a Triangle • Algorithm: • Obtain three side lengths from the user – side1, side2, side3. • Calculate the area of the triangle. • Display the result. CISC101 - Prof. McLeod
Area of a Triangle, Cont. • The area can be calculated using what is commonly called "Heron's Rule" after Heron of Alexandria, even though the theorem was first developed by Archimedes. Given the three sides, a, b and c: CISC101 - Prof. McLeod
Area of a Triangle, Cont. • See TriangleArea.py CISC101 - Prof. McLeod