Lecture 4 — Exercises¶
Solutions to the problems below must be sent to Submitty for grading. A separate file must submitted for each problem. These must be submitted by 4 pm on Tuesday, January 30.
Write a program that assigns a string to the variable called
phrase
and then transformsphrase
into a hashtag. In other words, all words inphrase
are capitalized, all spaces are removed, and a#
appears in front. Store the result in a variable calledhashtag
. Then print the value of bothphrase
andhashtag
. Your program should start withphrase = 'Things you wish you knew as a freshman'
and the output from the program (using
print()
function calls) should beThe phrase "Things you wish you knew as a freshman" becomes the hashtag "#ThingsYouWishYouKnewAsAFreshman"
Note that the output includes the quotation marks.
One of the challenges of programming is that there are often many ways to solve even the simplest problem. Consider computing the area of the circle with the standard formula
\[a(r) = \pi r^2\]Fortunately, we now have
pi
from the math module, but to compute the square of the radius we now can use**
orpow()
or we can just multiply the radius times itself. To print the area accurate to only a few decimal places we can now use the stringformat()
method or theround()
built-in function, which includes an optional second argument to specify the number of decimal places.Write a short Python program that computes and prints the areas of two circles, one with radius 5 and the other with radius 32. Your code must use
**
once andpow()
once and it must useformat()
once andround()
once. The output should be exactlyArea 1 = 78.54 Area 2 = 3216.99