CMSC 201
Chang (rev. 2015-02-05)
Today we will learn about:
•
Boolean expressions
•
Decision making
Boolean expressions evaluate to True or False .
Boolean variables have their own operators.
a = True b = False c = (10 > 4) d = (someVar == someOtherVar)
We can use the following mathematical operators when constructing boolean expressions:
>
>=
<
<=
Operator
==
!=
Meaning
Checks if two things are equal
Checks if two things are NOT equal
Greater than
Greater than or equal to
Less than
Less than or equal to
a = 4 b = 5 c = 3 bool1 = ( a == b ) bool2 = ( c < b ) bool3 = ( c != a ) print(bool1, bool2, bool3)
Prints:
False True True
Boolean operators and or not combine boolean values
bool1 = ( a and b )
Value of a
True
True
False
False
Value of b
True
False
True
False
Value of bool1
True
False
False
False
For (a and b) to be true, both a and b must be true.
bool1 = ( a or b )
Value of a
True
True
False
False
Value of b
True
False
True
False
Value of bool1
True
True
True
False
For (a or b) to be true, at least one of a and b must be true.
bool1 = (not a)
Value of a
True
False
Value of bool1
False
True not a is the opposite of a
Value of a
True
True
True
True
False
False
False
False
bool1 = a and (b or c)
Value of b
True
True
False
False
True
True
False
False
Value of c
True
False
True
False
True
False
True
False
Value of bool1
True
True
True
False
False
False
False
False
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")
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)
What about this?
a = 4 b = True c = a and b print(c)
Prints:
True
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.
Why do we care so much about booleans?
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
line-1 if booleanExpression: line-2 line-3 line-4 line-5
This code would produce the following flowchart structure:
Condition is true line-1 line-2 line-3 line-4 line-5
Condition is false
number = int(input("Enter a number ")) if number > 0: print("You entered a positive number") print("This part always execute")
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.
We can also "nest" if statements.
line-1 if someCondition: if somethingElse: line-2 else: line-3 else: line-4
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"
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")
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.
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.
line-1 if someBoolean: line-2 line-3 else: line-4 line-5 line-6
Condition is true line-1
line-2 line-3 line-4 line-5
Condition is false
Line-6
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.
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
line-1 if someBoolean: line-2 line-3 elif someOtherBoolean: line-4 line-5 line-6 someBoolean is true line-1 line-2 line-3 someBoolean is false AND someOtherBoolean is true line-4 line-5 someBoolean is false AND someOtherBoolean is false
Line-6
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.
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)