Presentation is loading. Please wait.

Presentation is loading. Please wait.

Decision Making CMSC 201. Overview Today we will learn about: Boolean expressions Decision making.

Similar presentations


Presentation on theme: "Decision Making CMSC 201. Overview Today we will learn about: Boolean expressions Decision making."— Presentation transcript:

1 Decision Making CMSC 201

2 Overview Today we will learn about: Boolean expressions Decision making

3 Boolean Expressions Last class, we discussed expressions that evaluate to a number. But we’re also aware of values that evaluate to true or false, called booleans. Boolean variables have their own series of operators. a = True b = False c = 10 > 4 d = someVar == someOtherVar

4 Boolean Math Operators We can use the following mathematical operators when constructing boolean expressions:

5 Example a = 4 b = 5 c = 3 bool1 = a == b bool2 = c < b bool3 = c != a print(bool1, bool2, bool3)

6 Example a = 4 b = 5 c = 3 bool1 = a == b bool2 = c < b bool3 = c != a print(bool1, bool2, bool3) Prints: False True True

7 Boolean Logic There are also three boolean operators, and, or, and not. These are ways of combining different boolean values.

8 And bool1 = a and b Value of aValue of bValue of bool1 True False TrueFalse For “a and b” to be true, both a and b must be true.

9 Or bool1 = a or b Value of aValue of bValue of bool1 True FalseTrue FalseTrue False For “a or b” to be true, either a OR b must be true.

10 Not bool1 = not a Value of aValue of bool1 TrueFalse True Not a returns the opposite boolean from a

11 Complex Expressions We can combine these operators however we like! bool1 = a and (b or c) Python first computes (b or c) then ands the result with a. Value of aValue of bValue of cValue of bool1 True FalseTrue FalseTrue False True False TrueFalse TrueFalse

12 Short Circuit Evaluation Notice that in the expression: bool1 = a and (b or c) If a is false, the rest of the expression doesn’t matter. Python will realize this, and if a is false won’t bother with the rest of the expression.

13 Practice a = 4 b = 5 c = 6 d = True e = False bool1 = d and (a > b) bool2 = (not d) or (b != c) bool3 = (d and (not e)) or (a > b) bool4 = (a % b == 2) and ((not d) or e)

14 Numbers and Booleans What about this? a = 4 b = True c = a and b print(c)

15 Numbers and Booleans Python accepts anything that is non-zero as True (there are some exceptions, but we’ll get into those later). So technically you can use any integer as a boolean expression.

16 Decision Making Why do we care so much about booleans?

17 If Statements An if statement only executes if a given boolean expression evaluates to True. if booleanExpression: line-1 line-2 line-3 line-4 Anything indented in after the if statement executes if and only if booleanExpression == True

18 If Statements line-1 if booleanExpression: line-2 line-3 line-4 line-5 This code would produce the following flowchart structure: line-1 Condition is true line-2 line-3 Condition is false line-4 line-5

19 Example number = int(input(“Please enter a number”)) if number > 0: print(“You entered a positive number”) print(“This part always executes”)

20 Vocab A block is an indented section of your code. A conditional is the boolean expression in an if statement. If statements are a type of control structure, since it controls the flow of your code.

21 Nested If Statements We can also “nest” if statements. line-1 if someCondition: if somethingElse: line-2 line-3 Here, line-2 only gets executed if someCondition and somethingElse are True, while line-3 gets executed only when someCondition is True.

22 Exercise Write a code snippet that asks for two numbers for the user. If they are equal, it should print out “Equal”, if the first is greater than the second, it should print out “Greater”, and if the second is greater than the first it should print out “Less than”

23 Exercise a = int(input(“Please enter a number: ”)) b = int(input(“Please enter another number: ”)) if a == b: print(“Equal”) if a > b: print(“Greater”) if a < b: print(“Less than”)

24 Else a = int(input(“Please enter a number: “) if a > 0: print(“a is greater than zero!”) if a <= 0: print(“a is less than or equal to zero!”) This pattern, where you have an if statement, followed by an if statement that is the complete opposite, happens so often it has a special keyword.

25 Else a = int(input(“Please enter a number: “) if a > 0: print(“a is greater than zero!”) else: print(“a is less than or equal to zero!”) The “else” keyword says that if the first if statement doesn’t execute, the else will.

26 Else line-1 if someBoolean: line-2 line-3 else: line-4 line-5 line-6 line-1 Condition is true line-2 line-3 Condition is false line-4 line-5 Line-6

27 Elif Imagine we have the following situation: we have an if statement, and if that if statement DOESN’T execute, we want another if statement to be evaluated. if a > 0: print(“A is positive”) else: if a < 0: print(“A is negative”) Elif lets us combine that if and that else.

28 Elif if a > 0: print(“A is positive”) elif a < 0: print(“A is negative”) Now the elif statement will only execute if: The first statement DOES NOT execute a < 0

29 Elif line-1 if someBoolean: line-2 line-3 elif someOtherBoolean: line-4 line-5 line-6 line-1 someBoolean is true line-2 line-3 someBoolean is false AND someOtherBoolean is true line-4 line-5 Line-6 someBoolean is false AND someOtherBoolean is false

30 Exercise Request an input from the user. If it’s positive, print out the square root. If it’s negative, print out whether it’s even or odd.

31 Exercise inputNum = int(input(“Please enter a number”)) if inputNum < 0: if inputNum % 2 == 0: print(“Number is even”) else: print(“Number is odd”) elif inputNum > 0: print(inputNum **.5)


Download ppt "Decision Making CMSC 201. Overview Today we will learn about: Boolean expressions Decision making."

Similar presentations


Ads by Google