CS 108 Quiz 2
logical equivalence; ==
- the expression p == q is True only when p and q have the same truth value ex: >>>false == false true ex:>>>true ==false false ex: >>> true == true true
if/elif statements
-a generalized if-statement with more than one condition -used for making complex decisions
infinite loop
-a loop that never ends -the loop condition never becomes false -therefore does not execute false block
boolean logic
-all about manipulating so-called truth values
conditional execution
-allows a program to decide whether to do something, based on some condition
elif statement
-allows us to check another logical expression and also control expression if that expression evaluates to be true -always need exactly one if statement -optional to have any elif statements or can have as many as you want -can have zero or at most one else statement
range(start, stop)
-allows us to work with the range of integers from start to stop - 1
loops within loops
-also known as nested loops for row in range (1,10): for col in range(1,10): prod = row * col if prod < 10: print(' ', end =' ') print()
boolean expression
-an expression that evaluates to true or false
body
-code block underneath for loop -repeats the body exactly n times
if condition: true block
-condition is a statement that is true or false -true block is one or more statements that are indented the same amount
for-loop header
-first line of a for-loop -a for-loop always begins with the keyword for
flow of control for while-loop
-first, Python checks if the loop condition is True or False -if it's True it executes the body -if it's False, it skips over the body(jumps out of the loop) -as long as the condition is true, Python executes the loop
incrementer
-i = i + 1 -this line causes the i to be incremented by 1 -1 increases as the loop executes, which guarantees the the loop will eventually stop -job is to increment the loop variable
if-statements
-let you write programs that can decide, while the program is running, whether or not to run one block of code or another -always begin with the keyword if -it is then always followed by a boolean expression called if-condition -after the if-condition comes a colon(:); Python uses the : token to mark the end of conditions in if-statements, loops, and functions -everything from the if to the : is referred to as the if-statement header
logical negation; "not"
-not p is true when p is false -not p is false when p is true ex:>>> not True False ex:>>> not False True
flow of control
-order in which instructions are executed -by défault, instructions are executed sequentially from top to bottom
range
-produces a list of numbers
body of the loop
-repeated print statements -must be indented the same amount for i in [1, 2, 3]: print('Warning') print(i)
for -loop
-repeats a given block of code some specified number of times ex: for i in range (10): print(i)
!=
-the expression p != q tests if p and q are not the same -returns true only when they have different values ex:>>>false != false false ex:>>>true != false true >>> true != true false
logical "and"
-the expression p and q is true only when both p is true and q is true -every other cases is false ex:>>> false and false false ex:>>> false and true false ex:>>>true and false false ex:>>>true and true true
logical "or"
-the expression p or q is true exactly when p is true or q is true or when both are true ex:>>> false or false false ex:>>>false or true true ex:>>>true or false true ex:>>>true or true true
definite loop for variable in sequence: body of the loop
-the number of reputations is fixed even before the loop even begins # of repetitions = len(sequence)
comparing if and while
-the true block of an if statement is evaluated at most once -the body of a while statement can be evaluated multiple times, provided the test remains true
short-circuit evaluation
-the value of False and X does not depend on X; it is always false -the values of True or X are always True
code blocks and indentation
-to indicate a block of code in Python, you must indent each line of the block by the same amount -two blocks of code in our example if-statment are both indented four spaces -indentatin is required for indicating what block of code a statement belongs to
when to use indefinite loop
-use when # of repetitions you need is not as obvious, impossible to determine before the loop beings
evaluating larger Boolean expressions
-used to control both if-statements and loops -use brackets and operator preference to specify the order in which their subparts are evaluated
relational operators
-used to make comparisons < > == >= != -for simple conditions
loops
-used to repeatedly execute blocks of code -for-loops and while-loops
loop index variable
-variable for i in range (10): print(i) -in this case is i -each time the loop executes, the variable i is set to be the next value -by default the initial value of i is 0 and it goes up n - 1 by ones parameter for the range function should never be the same variable as the one for the loop index variable
logical operators
-what we use to combine boolean values -not -and -or -all decisions that can be made by Python can be made using these logical operators
ex: not True and False or True
1. evaluate operator with highest precedence ; not has highest; not True evaluates to False =false and false or true 2. and has second highest precedence in this case; false and false evaluates to false =false or true 3. false or true evaluates to true
indefinite loop while loop test: body of the loop
1. evaluate the loop test (a boolean expression) 2. if it's True, execute the statements in the body, and go back to step 1 3.if it's False, skip the statements in the body and go to the statement after the loop
ex: not (True and (False or True))
1. first evaluate false or true which is true = not (true and true) 2. evaluate true and true which is true =not true 3. not true evaluates to False
general structure of if/else statement
before_block if cond: true_block else: false_block after_block
accumulator design pattern
calculates a sum using repetition and arithmetic involves these three steps: -set up accumulator variable -use a loop to process inputs/values -after the loop, use the accumulated result
Executing for a loop
does the sequence have more values yes- assign the next value in sequence to variable yes - execute the statements in the body no -execute statement after the loop
for variable in sequence: body of the loop
ex: for i in [1, 2, 3]: print('Warning') print(i) For each value in the sequence: -the value is assigned to the variable -all statements in the body of the loop are executed using that value -once all values in the sequence have been processed, the program continues with the first statement after the loop
while loop
ex: i = 0 while i < 0: print(i) i = i + 1 -while loop header(while i<0) i<0 is the while loop condition -the indented code underneath is the while loop body -condition is a boolean expression that returns true or false
simple repetition loops
for i in range(N): body of the loop -repeats a loops body N times
initializer statement for while loop
i = 0 -very first line of the sample program -unlike with for-loops, which automatically initialize their loop variable, it is the programmer's responsibility to give initial values to any variables used by a while-loop
multi-way decisions
if condition1: true block for condition1 elif condition2: true block for condition2 elif condition3: true block for condition3 ... else: false block -the conditions are evaluated in order. the true block of the first conditions executed -if none of the conditions are executed the false block is executed
two way decisions
if condition: true block else: false block
accumulator pattern
the accumulator pattern calculates a sum(or another accumulation) using repetition and arithmetic def main(): # set up accumulator variable total = 0 n = int(input("How many items? ")) # go through n steps in the loop: for i in range(n): # collect a value as a float, add to running total price = float(input("Enter price: ")) total += price print("The total is %.2f" % total) main()
shortcuts where var is variables and expressions is an expression
var += expr is equivalent to var = var + (expr) var -= expr is equivalent to var = var - (expr) *the = must come after the other operator