Python Q & A: Part 2

Posted on Aug. 19, 2020 by Ted Martin

1. We’ve seen that n = 42 is legal. What about 42 = n?

SyntaxError. Can't assign to a litteral. A variable cannot begin with a number and "n" is not defined.

2. How about x = y = 1?

This works because both x and y are given the value "1" in this case.

3. In some languages every statement ends with a semi-colon. What happens if you put a

semi-colon at the end of a Python statement?

Nothing happens. It is unnecessary but it does not break the output.

4. What if you put a period at the end of a statement?

A period at the end of a statement yields a SyntaxError

5. In math notation you can multiply x and y like this: xy. What happens if you try that in Python?

This will yield a NameError as "xy" is not defined.


6. The volume ofa sphere with radius r is 4/3πr**3. What is the volume of a sphere with radius 5?

I use parentheses to make the order of precedence easier to read. Also, make sure you import the math module to use 'pi'. (4/3) * (math.pi * 125) yields an anser of 523.5987755982987

7. Suppose the cover price of a book is $24.95, but bookstores get a 40% discount. Shipping costs $3 for the first copy and 75 cents for each additional copy. What is the total wholesale cost for 60 copies?

This problem could be solved by finding out the discounted price for bookstores: 24.95 * 0.40

Each book is $14.97. The first shipped book is $14.97 + $3.00 which is $17.97. The remaining copies are $14.97 + 0.75 which is $15.72. This number times 59 equals $927.48. $927.48 plus the cost of the first copy is $945.45.

8. If I leave my house at 6:52am and run 1 mile at an easy pace (8:15 per mile), then 3 miles at tempo (7:12 per mile) and 1 mile at an easy pace again, what time do I get home for breakfast?

8.15 * 2 = 16:30

7.12 * 3 = 21:36

16:30 + 21:36 = 38:06

06:52 + 38:06 = 07:30:06

You will be eating breakfast at 07:30:06

Share post


There are no comments yet.

Add a new comment