Winter 2019 CISC101 4/16/2019 CISC101 Reminders

Slides:



Advertisements
Similar presentations
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Advertisements

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
Chapter Making Decisions 4. Relational Operators 4.1.
Controlling Program Flow with Decision Structures.
The If Statement There are no switch statements in Python. You need to use just if statements. There are no switch statements in Python. You need to use.
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
Next Week… Quiz 2 next week: –All Python –Up to this Friday’s lecture: Expressions Console I/O Conditionals while Loops Assignment 2 (due Feb. 12) topics:
 Type Called bool  Bool has only two possible values: True and False.
Next Week… Quiz 2 next week: –All Python –Up to this Friday’s lecture: Expressions Console I/O Conditionals while Loops Assignment 2 (due Feb. 12) topics:
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Introduction to Decision Structures and Boolean Variables
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
The Ohio State University
Last Time Unary operators: Other Assignment operators
Introduction to Python
Making Choices with if Statements
The switch Statement, and Introduction to Looping
Chapter 4: Making Decisions.
Lecture 3- Decision Structures
EGR 2261 Unit 4 Control Structures I: Selection
Topics The if Statement The if-else Statement Comparing Strings
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Winter 2018 CISC101 11/9/2018 CISC101 Reminders
Winter 2018 CISC101 11/9/2018 CISC101 Reminders
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
Topics The if Statement The if-else Statement Comparing Strings
Selection CIS 40 – Introduction to Programming in Python
And now for something completely different . . .
Conditions and Ifs BIS1523 – Lecture 8.
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
Winter 2018 CISC101 11/27/2018 CISC101 Reminders
Winter 2018 CISC101 11/29/2018 CISC101 Reminders
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
CISC101 Reminders Assn 3 due tomorrow, 7pm.
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
CISC101 Reminders Quiz 2 this week.
Introduction to Decision Structures and Boolean Variables
CISC124 TA names and s have been added to the course web site.
CISC124 Labs start this week in JEFF 155. Fall 2018
Adding Intelligence Check for the presence or absence of a condition in the environment Take the appropriate actions Involves making a choice (to do or.
CISC/CMPE320 - Prof. McLeod
CMPE212 – Reminders The other four assignments are now posted.
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Winter 2019 CISC101 4/8/2019 CISC101 Reminders
CISC101 Reminders All assignments are now posted.
CISC101 Reminders Labs start this week. Meet your TA! Get help with:
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Winter 2019 CISC101 4/14/2019 CISC101 Reminders
Chapter 3: Selection Structures: Making Decisions
CISC101 Reminders Quiz 1 marking underway.
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
CISC101 Reminders Assignment 3 due today.
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

if Statement, Cont. You will often have to nest if statements: etc. Winter 2019 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 Winter 2019 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 : Winter 2019 CISC101 - Prof. McLeod

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

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

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

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

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