Test Yourself
- What are the two values of the Boolean data type? How do you write them?
- What are the three Boolean operators?
- 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).
- 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)
- What are the six comparison operators?
- What is the difference between the equal to operator and the assignment operator?
- Explain what a condition is and where you would use one.
- 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')
- 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.
- What can you press if your program is stuck in an infinite loop?
- What is the difference between break and continue?
- What is the difference between range(10), range(0, 10), and range(0, 10, 1) in a for loop?
- 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.
If you had a function named bacon() inside a module named spam, how would you call it after importing spam?
Look up the round() and abs() functions on the Internet, and find out what they do. Experiment with them in the interactive shell.