# Boolean Expressions

# Relational or Comparison ops.

(Assume variable a = 10, b = 20 )

OperatorDescriptionExample
and Logical ANDIf both the operands are true then condition becomes true.( a and b ) is true.
or Logical ORIf any of the two operands are non-zero then condition becomes true.( a or b ) is true.
not Logical NOTUsed to reverse the logical state of its operand.Not(a and b) is false.

# Conditional or Logical ops.

(Assume variable a = 10, b = 20 )

OperatorDescriptionExample
==If the values of two operands are equal, then the condition becomes true.( a == b ) is not true.
!=If values of two operands are not equal, then condition becomes true.( a != b ) is true.
<>If values of two operands are not equal, then condition becomes true.( a <> b ) is true. This is similar to != operator.
>If the value of left operand is greater than the value of right operand, then condition becomes true.( a > b ) is not true.
<If the value of left operand is less than the value of right operand, then condition becomes true.( a < b ) is true.
>=If the value of left operand is greater than or equal to the value of right operand, then condition becomes true.( a >= b ) is not true.
<=If the value of left operand is less than or equal to the value of right operand, then condition becomes true.( a <= b ) is true.

*Know short – circuit evaluations! And vs. Or

  • Short circuit evaluation: deciding the value of a compound Boolean expression after evaluating only one sub expression

# Reviews

  1. What is the result of the following Boolean expression, if x equals 5, y equals 3, and z equals 8?

    not (x < y or z > x) and y < z
    • true
    • false
    • 8
    • 5
  2. What are the values that the variable num contains through the iterations of the following for loop?

    for num in range(2, 9, 2)
    • 2, 3, 4, 5, 6, 7, 8, 9
    • 2, 5, 8
    • 2, 4, 6, 8
    • 1, 3, 5, 7, 9
  3. The first input operation is called the , and its purpose is to get the first input value that will be tested by the validation loop.

    • priming read
    • first input
    • loop set read
    • loop validation
  4. if x = 1 , y = 2 what would print given the following statement, given C is a true value? result is 1

    print(x if C else y)
  5. To quit the Python shell, you can either select the window’s close box or press the key combination.

    • Control + C
    • Control + Z
    • Control + D
    • Control + X
  6. In Python, you can write a print statement that includes two or more expressions separated by .

    • periods
    • colons
    • commas
    • semicolons
  7. The result of evaluating 45 % 0 is .

    • 0
    • 45
    • 1
    • an error
  8. A(n) function is a function with the same name as the data type to which it converts.

    • semantic
    • casting
    • abstraction
    • type conversion
  9. Mix and Match

    Loop Types:

    1. nested loops

    You can use one or more loop inside any another while, for or do..while loop.

    1. for loop

    Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.

    1. while loop

    Repeats a statement or group of statements while a given condition is TRUE. It tests the condition before executing the loop body.

  10. Mix and Match

    Statement choices:

    1. Pass statement

    The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute.

    1. Break statement

    Terminates the loop statement and transfers execution to the statement immediately following the loop.

    1. Continue statement

    Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.

  11. Name 4 built-in Python conversion type functions

    str()
    float()
    bool()
    int()
    
  12. Name at least 5 built-in functions

    input()
    print()
    round()
    ord()
    chr()
    
  13. Name at least 6 Python keywords

    if
    else
    elif
    while
    except
    finally
    
  14. What is the purpose of a sentinal value

    special value that marks the end of a sequence of items (terminates loop!)