Test Yourself

  1. What are the two values of the Boolean data type? How do you write them?
  1. What are the three Boolean operators?
  1. Write out the truth tables of each Boolean operator (that is, every possible combination of Boolean values for the operator and what they evaluate to).
  1. What do the following expressions evaluate to?
(5 > 4) and (3 == 5)
not (5 > 4)
(5 > 4) or (3 == 5)
not ((5 > 4) or (3 == 5))
(True and True) and (True == False)
(not False) or (not True)
  1. What are the six comparison operators?
  1. What is the difference between the equal to operator and the assignment operator?
  1. Explain what a condition is and where you would use one.
  1. Identify the three blocks in this code:
spam = 0
if spam == 10:
    print('eggs')
    if spam > 5:
        print('bacon')
    else:
        print('ham')
    print('spam')
print('spam')
  1. Write code that prints Hello if 1 is stored in spam, prints Howdy if 2 is stored in spam, and prints Greetings! if anything else is stored in spam.
  1. What can you press if your program is stuck in an infinite loop?
  1. What is the difference between break and continue?
  1. What is the difference between range(10), range(0, 10), and range(0, 10, 1) in a for loop?
  1. Write a short program that prints the numbers 1 to 10 using a for loop. Then write an equivalent program that prints the numbers 1 to 10 using a while loop.
  1. If you had a function named bacon() inside a module named spam, how would you call it after importing spam?

  2. Look up the round() and abs() functions on the Internet, and find out what they do. Experiment with them in the interactive shell.

results matching ""

    No results matching ""