How to Automate the Boring Stuff-Ch. 2 Practice Questions
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)
(5 > 4) and (3 == 5)/FALSE not (5 > 4)/FALSE (5 > 4) or (3 == 5)/TRUE not ((5 > 4) or (3 == 5))/FALSE (True and True) and (True == False)/FALSE (not False) or (not True)/TRUE
What is the difference between the equal to operator and the assignment operator?
== Equal To =Assignment
What are the six comparison operators?
==,!=,<,>,<=,and>=
Explain what a condition is and where you would use one
A condition is just a more specific name in the context of flow control statements. Conditions always evaluate down to a Boolean value, True or False.
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).
And True and True - True True and False - False False and True -False False and False-False Or True or True-True True or False-True False or True-True False or False-False Not Not True-False Not False-True
What keys can you press if your program is stuck in an infinite loop?
Ctrl+C
What is the abs() function?
The abs() function returns the absolute value of the specified number.
What is the difference between break and continue?
The break statement will move the execution outside and just after a loop. The continue statement will move the execution to the start of the loop.
What is the round() function?
The round() function returns a floating point number that is a rounded version of the specified number, with the specified number of decimals. The default number of decimals is 0, meaning that the function will return the nearest integer.
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')
The three blocks are everything inside the if statement and the lines print('bacon') and print('ham').
What is the difference between range(10), range(0, 10), and range(0, 10, 1) in a for loop?
They all do the same thing. The range(10) call ranges from 0 up to (but not including) 10, range(0, 10) explicitly tells the loop to start at 0, and range(0, 10, 1) explicitly tells the loop to increase the variable by 1 on each iteration.
If you had a function named bacon() inside a module named spam, how would you call it after importing spam?
This function can be called with spam.bacon().
What are the two values of the Boolean data Type? How do you write them?
True or False, using capital T and F, with the rest of the word in lowercase.
What are three Boolean operators?
and, or, not
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.
for i in range(1, 11): print(i) i = 1 while i <= 10: print(i) i = i + 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.
if spam==1: Print('Hello') elif spam==2: print('Howdy') else: print('Greetings!')