CIS 111 Tests

Objectives

  • Use relational operators
  • Use logical operators
  • Evaluate expressions using relational and logical operators
  • Combine test expressions

Boolean values

  • Named after George Boole (1815 - 1864)
  • Boolean values are either true or false
  • Python has keywords for True and False

Relational operators

  • There are six relational operators. Assuming a and b are variables:
    • a < b means a less than b
    • a <= b means a less than or equal to b
    • a > b means a greater than b
    • a >= b means a greater than or equal to b
    • a == b means a equal to b
    • a != b means a not equal to b
  • Testing to see if someone passed a class (by percentage): percent >= 0.6
  • Testing to see if overtime should be paid: hours > 40
  • Testing for the letter Q: ch == 'Q'
  • Testing to see if two passwords are different: pass1 != pass2
  • Testing to see if it is below freezing: temp < 32
  • Testing to see if pressure is safely at or below 100 psi: psi <= 100

Logical operators

  • There are three logical operators. Assuming a and b are bool variables:
    • a and b means a AND b
    • a or b means a OR b
    • not a means NOT a
  • a and b is true only if a and b are both true, otherwise it is false
  • a or b is true if either a or b is true, otherwise it is false
  • not a is true if a was false, otherwise it is true
  • Note: a or b is true when both a and b are true
  • Note: a and b is false when both a and b are false
  • The order of precedence is not, then and, then or

Combining tests

  • Checking for a temp between 65 and 80: 65 <= temp <= 80
  • Checking for a temp between 65 and 80: (temp >= 65) and (temp <= 80)
  • Checking if a non-salaried worker is below forty hours: : (salaried == false) and (hours < 40)
  • Checking if a non-salaried worker is below forty hours: : not salaried and (hours < 40)
  • Checking if a worker is either salaried or below forty hours: : (salaried == true) or (hours < 40)
  • Checking if a worker is either salaried or below forty hours: : salaried or (hours < 40)
  • Checking to see if a letter grade is passing: (grade == 'A') or (grade == 'B') or (grade == 'C') or (grade == 'D')
  • Checking to see if a user entered an upper or lower case Y: (input == 'Y') or (input == 'y')
  • Checking to make sure gender is either M or F: (gender == 'M') or (gender == 'F')
  • Checking to see if gender is neither M nor F: (gender != 'M') and (gender != 'F')