Flow Control So Far: Writing simple statements that get executed one after another.
Flow Control So Far: Writing simple statements that get executed one after another. Flow control allows the programmer to specify how the program should act depending on some criteria.
Booleans A boolean is a value that is either True or False.
Booleans A boolean is a value that is either True or False. Can you think of an expression that would use booleans?
Booleans A boolean is a value that is either True or False. Can you think of an expression that would use booleans? Python has a data type for boolean values.
Booleans A boolean is a value that is either True or False. Can you think of an expression that would use booleans? Python has a date type for boolean values. Python boolean values are written as True or False (notice the capitalization)
Booleans >>> file_exists = True >>> file_exists True >>> file_exists = False >>> file_exists False
Comparison Operators Allow us to compare values of variables, literals, or expressions and return a boolean value (lower precedence than arithmetic operators). == equals!= not equals < less than > greater than <= less than or equal >= greater than or equal
Comparison (equals) p == q returns True - if operands (variables p and q in these examples) are equal False - if operands are not equal Think of it as a question... (is p equal to q?) Do not confuse this with assignment (single equals '=') where we are setting values equal to eachother p = q, is not the same as p == q p = q sets the variable p to the variable q p == q returns whether p is equal to q <see example3-0.py>
Comparison (others) Nothing worth mentioning for the other standard types. We will learn about two more later (is/in). Multiple comparisons: x < y < z Is expanded by python to match standard arithmetic operation: x < y and y < z So let's talk about and.
Boolean Logic Operators We've talked about arithmetic operators, and comparison operators, but python provides boolean logic operators as well. Boolean logic operators return...
Boolean Logic Operators We've talked about arithmetic operators, and comparison operators, but python provides boolean logic operators as well. Boolean logic operators return... booleans!
Boolean Logic Operators Negation: not And: and Or: or Examples: p and q P or q not p
Negation not p returns the opposite of p True - if p is False False - if p is True <see example3-0.py>
And p and q returns True - if both operands are true False - if either operand is false <see example3-0.py>
Or p or q returns True - if either operand is true False - if neither operand is true <see example3-0.py>
Truth Tables p q p and q p or q not p not q p and not q True True True False False True False False
Truth Tables p q p and q p or q not p not q p and not q True True True True False False True False False
Truth Tables p q p and q p or q not p not q True True True True False False False True False False False
Truth Tables p q p and q p or q not p not q (p and q) or (p and not q) True True True True False False False True False False False False
Truth Tables p q p and q p or q not p not q (p and q) or (p and not q) True True True True False False False True False False False False
Truth Tables p q p and q p or q not p not q (p and q) or (p and not q) True True True True True False False False True False False False False
Truth Tables p q p and q p or q not p not q (p and q) or (p and not q) True True True True True False False True False True False False False False
Truth Tables p q p and q p or q not p not q (p and q) or (p and not q) True True True True True False False True False True False True False False False
Truth Tables p q p and q p or q not p not q (p and q) or (p and not q) True True True True True False False True False True False True False False False False
Truth Tables p q p and q p or q not p not q p and not q (p and q) or (p and not q) True True True True False True False False True False True False True False False False False
Truth Tables p q p and q p or q not p not q p and not q (p and q) or (p and not q) True True True True False False True False False True False True False True False True True False False False False False True True
Truth Tables p q p and q p or q not p not q p and not q (p and q) or (p and not q) True True True True False False False True False False True False True False True False True True False False False False False True True
Truth Tables p q p and q p or q not p not q p and not q (p and q) or (p and not q) True True True True False False False True False False True False True True False True False True True False False False False False True True
Truth Tables p q p and q p or q not p not q p and not q (p and q) or (p and not q) True True True True False False False True False False True False True True False True False True True False False False False False False True True
Truth Tables p q p and q p or q not p not q p and not q (p and q) or (p and not q) True True True True False False False True False False True False True True False True False True True False False False False False False True True False
Truth Tables p q p and q p or q not p not q p and not q (p and q) or (p and not q) True True True True False False False True True False False True False True True False True False True True False False False False False False True True False
Truth Tables p q p and q p or q not p not q p and not q (p and q) or (p and not q) True True True True False False False True True False False True False True True True False True False True True False False False False False False True True False
Truth Tables p q p and q p or q not p not q p and not q (p and q) or (p and not q) True True True True False False False True True False False True False True True True False True False True True False False False False False False False True True False
Truth Tables p q p and q p or q not p not q p and not q (p and q) or (p and not q) True True True True False False False True True False False True False True True True False True False True True False False False False False False False True True False False
Complex Boolean Expressions Just as you can have multiple arithmetic operators in a given expression (3+4-3*2), you can have multiple boolean operators in an expression (False and not False or True) Order of operations (comparison operations are done before any of these), highest priority first: 1st: not 2nd: and 3rd: or <see example3-0.py>
Complex Boolean Expressions Style: Use parenthesis to show order of operations p and q or p and not q (p and q) or (p and not q) Never have to remember order of operations Makes switching languages easier Always use parenthesis around equivalence operators Exception: avoid parenthesis when using same operator over and over: True or False or False or True <see example3-0.py>
Short-Circuit Evaluation As soon as python can determine the outcome of a boolean logic operator (not comparison operators) it returns the result Faster code False and Anything: is always False True or Anything: is always True Python will not execute the code in 'Anything' True or (p and q) (p and q) is never run False and (p or q) (p or q) is never run Can lead to bugs if you rely on code in 'Anything' being run
if statements We can use all these boolean operators to control the flow of the program. In other words, we can have the program make decisions based the current state of the program The 'if' statement looks like: if <condition>: <statement>... <statement> <See example3-1.py>
Indention and Code Blocks You'll notice that the lines after the if statement are indented: if <condition>: <statement>... <statement> The indented code is called a 'block' of code. This block of code is run only if <condition> is True. Otherwise this entire 'block' of code is skipped.
if-else statements We can use all these boolean operators to control the flow of the program. In other words, we can have the program make decisions based on these operators. The if-else statement looks like: if <condition>: <statements> else: <statements> <See example3-1.py>
if-elif statements We can use all these boolean operators to control the flow of the program. In other words, we can have the program make decisions based on these operators. The if-elif statement looks like: if <condition>: <statements> elif <condition>: # repeat as many time as needed <statements> else: # this block is optional <statements> <See example3-1.py>
Conditional expressions We can also do a shorthand version of this that allows assignment: even = True if (x%2 == 0) else False Sets even to True if x is divisible by 2 otherwise, sets even to False.