Presentation is loading. Please wait.

Presentation is loading. Please wait.

Decision Making CMSC 201 Chang (rev. 2015-02-05).

Similar presentations


Presentation on theme: "Decision Making CMSC 201 Chang (rev. 2015-02-05)."— Presentation transcript:

1 Decision Making CMSC 201 Chang (rev. 2015-02-05)

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

3 Boolean Expressions Boolean expressions evaluate to True or False. Boolean variables have their own 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) Prints: False True True

6 Boolean Logic Boolean operators and or not combine boolean values

7 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.

8 or bool1 = ( a or b ) Value of aValue of bValue of bool1 True FalseTrue FalseTrue False For (a or b) to be true, at least one of a and b must be true.

9 not bool1 = (not a) Value of aValue of bool1 TrueFalse True not a is the opposite of a

10 Complex Expressions bool1 = a and (b or c) Value of aValue of bValue of cValue of bool1 True FalseTrue FalseTrue False True False TrueFalse TrueFalse

11 Short Circuit Evaluation Notice that in the expression: bool1 = a and (b or c) If a is False, the whole expression is False and does not depend on the value of (b or c). Python will realize this, and if a is False and will not evaluate (b or c). Try: bool1 = a and print("Hello")

12 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)

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

14 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.

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

16 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

17 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

18 Example number = int(input("Enter a number ")) if number > 0: print("You entered a positive number") print("This part always execute")

19 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.

20 Nested If Statements We can also "nest" if statements. line-1 if someCondition: if somethingElse: line-2 else: line-3 else: line-4

21 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"

22 Exercise a = int(input("Enter a number: ")) b = int(input("Enter another number: ")) if a == b: print("Equal") if a > b: print("Greater") if a < b: print("Less than")

23 Else a = int(input("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.

24 Else a = int(input("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.

25 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

26 Elif Cascading if statements: if a > 0: print("A is positive") else: if a < 0: print("A is negative") Elif lets us combine that if and that else.

27 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, and a < 0

28 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

29 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.

30 Exercise inputNum = int(input("Enter a number")) if inputNum < 0: if inputNum % 2 == 0: print("Number is even") else: print("Number is odd") elif inputNum > 0: print(inputNum ** 0.5)


Download ppt "Decision Making CMSC 201 Chang (rev. 2015-02-05)."

Similar presentations


Ads by Google