Python Boot Camp Booleans While loops If statements Else clauses

Slides:



Advertisements
Similar presentations
CSC 110 Decision structures [Reading: chapter 7] CSC 110 H1.
Advertisements

1 Decision Structures Chapter 7 (Skip 7.1.3, 7.4) Adapted from the online slides provided by John Zelle (
C++ for Engineers and Scientists Third Edition
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
Chapter 3 Computing with Numbers
Chapter 7 Decision Structures
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 3 Computing with Numbers.
Python Programming: An Introduction to Computer Science
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Fall, 2006Selection1 Choices, Choices, Choices! Selection in FORTRAN Nathan Friedman Fall, 2006.
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
Topics: IF If statements Else clauses. IF Statement For the conditional expression, evaluating to True or False, the simple IF statement is if : x = 7.
Python Conditionals chapter 5
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
Variables, Types, Expressions Intro2CS – week 1b 1.
Function and Function call Functions name programs Functions can be defined: def myFunction( ): function body (indented) Functions can be called: myFunction(
CS 127 Exceptions and Decision Structures. Exception Handling This concept was created to allow a programmer to write code that catches and deals with.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
1 CS Review, iClicker -Questions Week 15. Announcements 2.
Announcements Reading Read Chapter 4 (functions).
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Python Programming, 3/e1 Python Programming: An Introduction to Computer Science Chapter 7 Decision Structures.
Basic concepts of C++ Presented by Prof. Satyajit De
Announcements Reading Project1 Codelab: Read Chapter 4 (functions)
Python Programming: An Introduction to Computer Science
Python Review 1.
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Introduction to Python
Python: Control Structures
CHAPTER 4 Selection CSEG1003 Introduction to Computing
Python Programming: An Introduction to Computer Science
EGR 2261 Unit 4 Control Structures I: Selection
Week 4 - Monday CS 121.
Primitive Data, Variables, Loops (Maybe)
The Selection Structure
Intro to C Tutorial 4: Arithmetic and Logical expressions
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Topics The if Statement The if-else Statement Comparing Strings
Ruth Anderson UW CSE 160 Winter 2017
Logical Operators and While Loops
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Control Structures I (Selection)
Arithmetic operations, decisions and looping
Class 12.
What does this do? def revList(L): if len(L) < = 1: return L x = L[0] LR = revList(L[1:]) return LR + x.
3. Decision Structures Rocky K. C. Chang 19 September 2018
Conditional and iterative statements
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
15-110: Principles of Computing
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Chapter 2 Programming Basics.
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Class 13 function example unstring if if else if elif else
Logical Operators and While Loops
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
CHAPTER 5: Control Flow Tools (if statement)
Chapter 3: Selection Structures: Making Decisions
Java LESSON 3 Loops.
月夜憶舍弟 戍鼓斷人行,邊秋一雁聲。 露從今夜白,月是故鄉明。 有弟皆分散,無家問死生。 寄書長不達,況乃未休兵。 杜甫
月夜憶舍弟 戍鼓斷人行,邊秋一雁聲。 露從今夜白,月是故鄉明。 有弟皆分散,無家問死生。 寄書長不達,況乃未休兵。 杜甫
Using C++ Arithmetic Operators and Control Structures
Review of Previous Lesson
Topic: Loops Loops Idea While Loop Introduction to ranges For Loop
Chapter 13 Control Structures
Presentation transcript:

Python Boot Camp Booleans While loops If statements Else clauses Logic Operators While loops If statements Else clauses

Boolean Values True, False Result of comparison, e.g. x < y Boolean operations: And: (x >= 5) and (x <= 10) Or: (x < 5) or (x > 10) Not: not (3 == 4)

Truth Tables: and and True False

Truth Tables: or or True False

Truth Tables: not not True False

Comparison Tests We also need a set of test operators to build useful conditions == Equality != Inequality > Greater Than >= Greater Than or Equal < Less Than <= Less Than or Equal

Precedence Multiplication and division take precedence over addition and subtraction Consider a*b+c*d; which of the three is it equal to? (a*b) + (c*d) a*(b+c)*d ((a*b)+c)*d What about comparison (<, <=, ==, >=, >) vs. Boolean ops (and, or, not)? Comparisons take precedence …

Example x >= 5 and y <= 10 is equivalent to (x>=5) and (y<=10) Python allows an unusual comparison chain: x<=y<=z is understood as (x<=y) and (y<=z)

Loops Loops allow for code to be run multiple times There are many kinds of loops the most basic kind is a while loop count = 0 while (count < 8): print 'The count is: ', count ) count = count + 1

count = 0 print ( 'The count is: ', count ) count = count + 1

Loops Note: The count variable is incremented after the last print statement. Is this the case with the loop code? Verify this! In addition to saving you from copying code over and over, it allows the condition to be a variable Suppose the number of times we want to print depends on some other code.

Example Revisited: motivation behind loops Intuition: we can leverage functions + arguments to get input, we can use the input to parameterize the loop! def printCountNTimes(n): count = 0 while (count < n): print ( 'The count is: ', count ) count = count + 1

Loop Types The two common types of loops you will use in python are the While loop For loop Which type you use depends on the particular situation

While Loop The while loop checks whether a condition is true or not. In this example “is count < 9” If this is true it executes the body of the loop If it is false it skips past the loop body to the next line of code count = 0 while (count < 9): print ('The count is: ', count ) count = count + 1

print ('The count is: ', count ) True False while count < 9 print ('The count is: ', count ) count = count + 1 End

CQ4.1 :Are these programs equivalent? def printCountNTimes(n): count = 0 while (count < n): print ('The count is: ', count ) count = count + 1 2 1 printCountNTimes(4) printCountNTimes(8) A: yes B: no

While Loop Dangers While loops do not require that the loop ever stop. In this example, count starts at 0 and gets smaller. Thus it will always be < 9 count = 0 while (count < 9): print ('The count is: ', count ) count = count - 1

Example Print every other character of a string stg = “hello there” ‘h’ pos = 0 ‘l’ while pos < len(stgr): ‘o’ print(stg[pos]) ‘t’ pos = pos+2 ‘e’ ‘e’

IF Statement x = 7 if x > 10: x = True print (x) if x: x = x + 1 For the conditional expression <exp>, evaluating to True or False, the simple IF statement is if <exp>: <body> x = 7 if x > 10: print (x) x = x + 1 x = True if x: print (x)

Conditionals x = 7 if x > 10: print (x) x = x + 1 x = 7 It is also possible to specify what to do if the condition is False. Contrast these two examples. x = 7 if x > 10: print (x) x = x + 1 x = 7 if x > 10: print (x) else: x = x + 1

x = 7 if x > 10: print (x) x = x + 1 x = 7 True False if x > 10 End

x = 7 if x > 10: print (x) else: x = x + 1 x = 7 True if x > 10 False print (x) x = x + 1 End

Conditions At the heart of a conditional is a condition evaluation The code after the if but before the : should evaluate to either True or False These are special keywords in Python and are case sensitive

Beyond True and False x = 0 x = 0 if 0: if 1: print (x) print (x) Python provides True and False, even for general values Numbers: zero ≡ False, not zero ≡ True Lists: [ ] ≡ False, any other list ≡ True Strings: “” ≡ False, any other string ≡ True Experiment with it! x = 0 if 0: print (x) x = 0 if 1: print (x)

CQ4.2: Do these programs print the same things? x = 7 if x > 10: print (x) x = x + 1 1 2 x = 7 if x > 10: print (x) else: x = x + 1 A: yes B: no

CQ4.3: Do these programs print the same things? x = 12 if x > 10: print (x) x = x + 1 1 2 x = 12 if x > 10: print(x) else: x = x + 1 A: yes B: no

Therefore Always ask what “equivalent” means… For us from now on: Two programs are equivalent if they give the same output for all inputs Any other meaning should be made clear by explicit wording

Some Interesting Observations Comparisons can be chained x < y <= z is equivalent to: x < y and y <= z The not operator has a lower order of precedence than comparisons not a == b is equivalent to: not( a== b)

4.4 Clicker Question A: yes B: no if x and y > 0: print( x , y ) Now we can start building useful conditions Does this check if x > 0? if x and y > 0: print( x , y ) A: yes B: no

Tests Continued if x > 0 and y > 0: print (x + y) We could write such a condition as follows: if x > 0 and y > 0: print (x + y)

4.5 Clicker Question: Are these programs equivalent? 1 2 if (x+y) < 10: print(x) if (x+y)>=10: print(y) if(x+y) < 10: print(x) else: print(y) A: yes B: no

Nested Conditionals if x < 10: print(x+y) if y < 100: print(x) else: print(y)

Why nested conditionals? We could just write many complex conditionals and avoid the nested conditionals This is inefficient (many redundant checks) and may lead to code duplication But is the nesting always easy to understand?

Example Revisited if x < 10: print(x+y) if y < 100: print(x) else: print(y) if x < 10 and y < 100: print(x+y) print(x) if x < 10 and y >= 100: print(y)

Defensive Coding Comment your code Use conditionals to check/verify assumptions Use variable names that have meaning Avoid reusing variable names Use parentheses when order matters Keep the code simple

Keeping it simple def minOfThree(a,b,c): if a<=b and a<=c: return a if b<=a and b<=c: return b if c<=a and c<=b: return c

Keeping it simple? def minOfThree(a,b,c): if a<=b: if a<=c: return a else: return c else: if b<=c: return b else: return c

Analysis def minOfThree(a,b,c): if a<=b: if a<=c: return a else: return c else: if b<=c: return b else: return c a<=b a<=c b<=c y n a b c

Analysis def minOfThree(a,b,c): if a<=b: if a<=c: return a else: return c else: if b<=c: return b else: return c Who can be min? a<=b a<=c b<=c y n not b! not a! a b c

4.6 Clicker Question 1 if “False”: print(“hi”) 2 if False: print(“hi”) A: 1 and 2 both print B: only 1 prints C: only 2 prints D: neither 1 nor 2 print

4.7 Clicker Question 3 if eval(“False”): print(“hi”) 2 if False: print(“hi”) A: 3 and 2 both print B: only 3 prints C: only 2 prints D: neither 3 nor 2 print

Announcements Review Chapter 4 (functions) Read Chapter 5 (conditionals) Work on Project 1, due Sat before midnight Project 2 to be posted Monday, Sep. 17 due Monday, Oct. 1 Text part 2 expected out next week

4.8 CQ explained 1 2 3 1: nonempty string means True 1 2 3 if “False”: print(“hi”) if False: print(“hi”) if eval(“False”): print(“hi”) 1: nonempty string means True 2: condition is False 3: eval(“False”) is False Therefore only 1 will print

Error Checking Revisited #This function expects two positive integers # Returns -1 as an error condition def sumOfTwo(a,b): if type(a) == int and type(b) == int : if a > 0 and b > 0: return a+b return -1

Error Checking Revisited #This function expects two positive integers # Returns -1 as an error condition def sumOfTwo(a,b): if type(a) == int and type(b) == int : if a > 0 and b > 0: return a+b else: return -1

Two-Way Decisions import math def main(): print("This program finds the real solutions to a quadratic”) a,b,c=eval(input("\nPlease enter coefficients (a, b, c): ")) discRoot = math.sqrt(b * b - 4 * a * c) root1 = (-b + discRoot) / (2 * a) root2 = (-b - discRoot) / (2 * a) print("\nThe solutions are:", root1, root2) main()

Two-Way Decisions As per the comment, when b2-4ac < 0, the program crashes. This program finds the real solutions to a quadratic Please enter coefficients (a, b, c): 1,1,2 Traceback (most recent call last): File "C:\Documents and Settings\Terry\My Documents\Teaching\W04\CS 120\Textbook\code\chapter3\quadratic.py", line 21, in -toplevel- main() File "C:\Documents and Settings\Terry\My Documents\Teaching\W04\CS 120\Textbook\code\chapter3\quadratic.py", line 14, in main discRoot = math.sqrt(b * b - 4 * a * c) ValueError: math domain error

Two-Way Decisions We can check for this situation. Here’s our first attempt. # quadratic2.py # A program that computes the real roots of a quadratic equation. # Bad version using a simple if to avoid program crash import math def main(): a, b, c = eval(input("Please enter the coefficients (a, b, c): ")) discrim = b * b - 4 * a * c if discrim >= 0: discRoot = math.sqrt(discrim) root1 = (-b + discRoot) / (2 * a) root2 = (-b - discRoot) / (2 * a) print("\nThe solutions are:", root1, root2)

Two-Way Decisions We first calculate the discriminant (b2-4ac) and then check to make sure it is not negative. If it is, the program proceeds and we calculate the roots. Look carefully at the program. What’s wrong with it? Hint: What happens when there are no real roots?

Two-Way Decisions This program finds the real solutions to a quadratic Please enter the coefficients (a, b, c): 1,1,1 >>> This is almost worse than the version that crashes, because we don’t know what went wrong!

Two-Way Decisions We could add another if to the end: if discrim < 0: print("The equation has no real roots!" ) This works, but feels wrong. We have two decisions, with mutually exclusive outcomes (if discrim >= 0 then discrim < 0 must be false, and vice versa).

Two-Way Decisions

Two-Way Decisions # quadratic3.py # A program that computes the real roots of a quadratic equation. # Illustrates use of a two-way decision import math def main(): print "This program finds the real solutions to a quadratic\n" a,b,c = eval(input("Please enter the coefficients (a, b, c): ")) discrim = b * b - 4 * a * c if discrim < 0: print("\nThe equation has no real roots!") else: discRoot = math.sqrt(b * b - 4 * a * c) root1 = (-b + discRoot) / (2 * a) root2 = (-b - discRoot) / (2 * a) print ("\nThe solutions are:", root1, root2 ) main()

Two-Way Decisions >>> This program finds the real solutions to a quadratic Please enter the coefficients (a, b, c): 1,1,2 The equation has no real roots! Please enter the coefficients (a, b, c): 2, 5, 2 The solutions are: -0.5 -2.0

Multi-Way Decisions The newest program is great, but it still has some quirks! This program finds the real solutions to a quadratic Please enter the coefficients (a, b, c): 1,2,1 The solutions are: -1.0 -1.0

Multi-Way Decisions While correct, this method might be confusing for some people. It looks like it has mistakenly printed the same number twice! Double roots occur when the discriminant is exactly 0, and then the roots are –b/2a. It looks like we need a three-way decision!

Multi-Way Decisions Check the value of discrim when < 0: handle the case of no roots when = 0: handle the case of a double root when > 0: handle the case of two distinct roots We can do this with two if-else statements, one inside the other. Putting one compound statement inside of another is called nesting.

Multi-Way Decisions if discrim < 0: print("Equation has no real roots") else: if discrim == 0: root = -b / (2 * a) print("There is a double root at", root) # Do stuff for two roots

Multi-Way Decisions

Multi-Way Decisions Imagine if we needed to make a five-way decision using nesting. The if-else statements would be nested four levels deep! There is a construct in Python that achieves this, combining an else followed immediately by an if into a single elif.

Multi-Way Decisions if <condition1>: <case1 statements> elif <condition2>: <case2 statements> elif <condition3>: <case3 statements> ... else: <default statements>

Multi-Way Decisions This form sets of any number of mutually exclusive code blocks. Python evaluates each condition in turn looking for the first one that is true. If a true condition is found, the statements indented under that condition are executed, and control passes to the next statement after the entire if-elif-else. If none are true, the statements under else are performed. The else is optional. If there is no else, it is possible no indented block would be executed.

Multi-Way Decisions Conditions need not be mutually exclusive. Therefore, changing the sequence may give different results! Consider the following two versions when a==0 and b==2 if b<0: x=1 elif b>0 and a<3: x=2 elif b>0: x=3 else: x=4 if b<0: x=1 elif b>0: x=3 elif b>0 and a<3: x=2 else: x=4

Multi-Way Decisions def main(): a, b, c = eval(input("Please enter the coefficients (a, b, c): ")) discrim = b * b - 4 * a * c if discrim < 0: print("\nThe equation has no real roots!") elif discrim == 0: root = -b / (2 * a) print("\nThere is a double root at", root) else: discRoot = math.sqrt(b * b - 4 * a * c) root1 = (-b + discRoot) / (2 * a) root2 = (-b - discRoot) / (2 * a) print("\nThe solutions are:", root1, root2 )

Which version is simpler? def sort3(a,b,c): if a<=b: if b<=c: return [a,b,c] elif a<=c: return [a,c,b] else: return [c,a,b] else: if a<=c: return [b,a,c] elif b<=c: return [b,c,a] else: return [c,b,a] def sort3(a,b,c): if a<=b: if b<=c: return [a,b,c] else: if a<=c: return [a,c,b] return [c,a,b] else: if a<=c: return [b,a,c] else if b<=c: return [b,c,a] return [c,b,a]

Lists Reviewed Syntax: [ <elt 0>, <elt 1>, …, <elt m> ] Example: [3, 9, “hi”, 2.9, False] Lists can be nested: [[1,2], 3, [4, [5,6]], 7] Lists can be empty: [ ]

4.8 CQ Is this list empty? [ [ ] ] A: This list is empty B: This list is not empty

Operations Let L = [[1,2], 3, [4, [5,6]], 7] Length function: len(L) is 4 Element selection: L[0] is [1,2] L[1] is 3 L[2] is [4, [5,6]] L[3] is 7 L[len(L)] evaluates to L[4] and throws an error Nesting: L[2][0] is 4 L[2][1][0] is 5

Appending etc. L.append(x) makes list L longer by one element, at the end, which is x: >>> L = [[1,2], 3, [4, [5,6]], 7] >>> x = 8 >>> L.append(x) >>> L [[1, 2], 3, [4, [5, 6]], 7, 8] L1 + L2 concatenates two lists >>> L1 = [1, 2, 3] >>> L2 = [4, 5] >>> L1 + L2 [1, 2, 3, 4, 5]