Presentation is loading. Please wait.

Presentation is loading. Please wait.

Winter 2019 CISC101 4/16/2019 CISC101 Reminders

Similar presentations


Presentation on theme: "Winter 2019 CISC101 4/16/2019 CISC101 Reminders"— Presentation transcript:

1 Winter 2019 CISC101 4/16/2019 CISC101 Reminders You have been assigned to two groups in onQ – the “Lab” group and the “Grader” group. You must write the quiz in the lab time as indicated by the Lab group you are in. Quiz 1 next week, starting in the Tuesday lab. More info in Tuesday’s lecture. Today’s lecture material, up to but not including Conditionals, is on the quiz. Assignment 1 due next Friday. Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

2 Today Continue Python Syntax by looking at Expressions:
Winter 2019 CISC101 4/16/2019 Today Continue Python Syntax by looking at Expressions: Boolean Operators, Precedence again. Branching Algorithms – Using Conditionals. Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

3 CISC101 Boolean Operators There are a bunch of binary operators (and one unary operator) that yield a Boolean or a bool type result – a True or False. Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

4 Unary Boolean Operator
One unary boolean operator: not Sets True to False, and False to True (just like a NOT gate!) Winter 2019 CISC101 - Prof. McLeod

5 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 False bool value. and or a logical AND a logical OR Winter 2019 CISC101 - Prof. McLeod

6 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. and or a logical AND a logical OR These must have a bool value on both sides. Winter 2019 CISC101 - Prof. McLeod

7 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. Winter 2019 CISC101 - Prof. McLeod

8 Example How can you test a variable, aVal, to see if it is between two limits, inclusive? Call them low and high: Check: low <= aVal and aVal <= high Unfortunately, in Python, this also works sometimes: low <= aVal <= high This is bad form and will not work in most other programming languages! Winter 2019 CISC101 - Prof. McLeod

9 Example, Cont. If aVal is between the limits the expression on the previous slide will evaluate to a True. Suppose you want to see a True if aVal is outside the limits. How? Two ways: not( low <= aVal and aVal <= high ) Or: low > aVal or aVal > high Which way do you like better? Winter 2019 CISC101 - Prof. McLeod

10 Aside - Short-Circuit Evaluation
Python uses this to speed up the execution of Boolean expressions using and and or. For an and, if the LHS is already False, then the RHS is not evaluated. For an or, if the LHS is already True, the RHS is not evaluated. Winter 2019 CISC101 - Prof. McLeod

11 Expanded Precedence Rules
Unary operations (not and negation) first. Math operators in usual order: ** * / // % + - Binary boolean comparison operators: > >= < <= == != Logical operator: and Logical operator: or Assignment operator: = Winter 2019 CISC101 - Prof. McLeod

12 Expanded 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? Winter 2019 CISC101 - Prof. McLeod

13 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 Winter 2019 CISC101 - Prof. McLeod

14 Another Example – Evaluate:
not(5 * 4 > 3) or <= 5 and 6 == 2 not(20 > 3) or <= 5 and 6 == 2 not True or <= 5 and 6 == 2 False or <= 5 and 6 == 2 False or <= 5 and 6 == 2 False or True and 6 == 2 False or True and False False or False False Winter 2019 CISC101 - Prof. McLeod

15 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. Winter 2019 CISC101 - Prof. McLeod

16 So Far… Without conditionals programs are linear:
CISC101 So Far… Without conditionals programs are linear: Obtain data from user Calculations… Display results etc. Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

17 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. Winter 2019 CISC101 - Prof. McLeod

18 Conditionals or “Selection Statements”
Python has if, if-else and 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") Winter 2019 CISC101 - Prof. McLeod

19 if-else Statement Syntax of if-else statement: Example:
if boolean_expression : statement(s)_when_true else : statement(s)_when_false Example: if stress > maxStress / 1.5 : result = "failure" result = "pass" Winter 2019 CISC101 - Prof. McLeod

20 Aside - Indentation All of the statements inside the true part and inside the false part are indented once. As soon as code is “out-dented”, then it is outside the conditional. Just like code in a function: All the code in a function is indented once. Note that the semi-colon, :, tells the interpreter to get ready for some indented code. There always has to be at least one indented line of code after a :. Python uses indentation to indicate containment of code. Indentation is an important part of Python syntax! Winter 2019 CISC101 - Prof. McLeod

21 if Statement, Cont. You will often have to nest if statements: etc.
Winter 2019 CISC101 - Prof. McLeod

22 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 Winter 2019 CISC101 - Prof. McLeod

23 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 : Winter 2019 CISC101 - Prof. McLeod

24 if-elif Statements, Cont.
This leads to a common code structure often called a “chained if” construct: if condition1 : statement(s) elif condition2 : elif condition3 : else : You can have as many elifs as you want. The else part is optional. Winter 2019 CISC101 - Prof. McLeod

25 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. Winter 2019 CISC101 - Prof. McLeod

26 Nested if Statements - Demo
What to wear? Depends on time to destination and outdoor temperature: If time < 5 minutes, wear shorts & tshirt if time between 5 and 20 minutes: if temp >= 5 wear shorts & tshirt if temp between -10 and 5 wear pants & sweatshirt if temp less than -10 wear pants & jacket if time > 20 minutes: if temp >= 10 wear shorts & tshirt if temp between 0 and 10 wear shorts & sweatshirt if temp between – 10 and 0 wear pants & jacket if temp < -10 wear pants & parka Winter 2019 CISC101 - Prof. McLeod

27 Nested if Statements – Demo, Cont.
See: ClothesVersion0.py ClothesVersion1.py ClothesVersion2.py ClothesVersion3.py All work correctly and all do the same thing. Which one is “best” and why? Don’t forget that Python uses “short circuit” evaluation of logical operators – and & or. Winter 2019 CISC101 - Prof. McLeod

28 Building Conditionals
Nesting conditionals leads to the best structure for this example. If you have to nest conditionals, write the test with the fewest conditions for the outermost if statement. Make sure you have covered every possible combination of inputs. When testing, test every possible combination of inputs to make sure your logic is good. Writing nested conditionals can be tricky! Winter 2019 CISC101 - Prof. McLeod


Download ppt "Winter 2019 CISC101 4/16/2019 CISC101 Reminders"

Similar presentations


Ads by Google