How to Think Like a Computer Scientist : Learning with Python Exercises – Flashcards

Unlock all answers in this set

Unlock answers
question
C2 E1. Take the sentence : All work and no play makes Jack a dull boy. Store each word in a separate variable, then print out the sentence on one line using print.
answer
word1 = "All" word2 = "work" word3 = "and" word4 = "no" word5 = "play" word6 = "makes" word7 = "Jack" word8 = "a" word9 = "boy" print (word1,word2,word3,word4,word5,word6,word7,word8,word9)
question
C2 E2. Add parenthesis to the expression 6 * 1 - 2 to change its value from 4 to -6.
answer
print ( 6 * 1 - 2 ) to print (6 * ( 1 - 2 ) )
question
C2 E3. Place a comment before a line of code that previously worked, and record what happens when you rerun the program.
answer
A comment # only tells the person reading it more information. It does not affect the program execution at all.
question
C2 E4. Start the Python interpreter and enter bruce + 4 at the prompt. This will give you an error. Assign a value to bruce so that bruce + 4 evaluates to 10.
answer
bruce = 6 print ( bruce + 4 )
question
C2 E6. Evaluate the following numerical expressions in your head, then use the Python interpreter to check your results : (a) >>> 5 % 2 (b) >>> 9 % 5 (c) >>> 15 % 12 (d) >>> 12 % 15 (e) >>> 6 % 6 (f) >>> 0 % 7 (g) >>> 7 % 0 What happened with the last example? Why?
answer
(a) >>> 5 % 2 --- 1 (b) >>> 9 % 5 --- 4 (c) >>> 15 % 12 --- 3 (d) >>> 12 % 15 --- 12 (e) >>> 6 % 6 --- 0 (f) >>> 0 % 7 --- 0 (g) >>> 7 % 0 --- Error --- runtime error
Get an explanation on any task
Get unstuck with the help of our AI assistant in seconds
New