Climb the Ladder!#

Our class moves quickly! Sometimes, it feels like we make leaps in logic that are a bit too big. Learn Python slowly, by doing many, many examples. Problems in this notebook start out easy and progressively get harder, so that the next rung of the Python ladder is always within reach.

Section I: Numbers, variables, and math.#

  1. What is 123456789 times 987654321?

  1. Create a variable called mass that is equal to 100.

  1. Create a variable called velocity that is equal to 5.

  1. In physics, the momentum an object is exerting is equal to its mass times its velocity. That is, \(p = mv\). Use the variables you defined above to compute this object’s momentum.

  1. The kinetic energy of an object is half its mass times the square of its velocity. That is, \(K = \frac{1}{2}mv^2\). Compute the kinetic energy for this object using the variables you’ve already created.

How much icing do you need?#

In the next few exercises, our goal will be to figure out how much icing we will need to perfectly cover a giant doughnut. The doughnut features a hole (unfortunately no jelly filling), and a few large sprinkles. We only need to find the area of the red region in this shape.

  1. Create a variable called pi that is equal to 3.141

  1. The radius of the largest circle is 13.425. The radius of the smaller circle is 4.792. The sprinkles are right triangles with side length 3.28. Create the variables R, r, and s that represent these values.

  1. Find the area of the larger circle (the whole doughnut, including the hole). Call this variable area_big_circle.

  • Hint: The area of a circle with radius \(r\) is \(A = \pi r^2\).

  1. Find the area of the smaller circle (the hole). Call this variable area_small_circle.

  1. Find the area of one triangle (a sprinkle), where each sprinkle has a base and height of length s. Call this variable area_sprinkle.

  • Hint: The area of a triangle with base \(b\) and height \(h\) is \(A = \frac{1}{2}bh\).

  1. Using the three values you calculated above, find the area of the shaded region (i.e., how much icing will you need?). Save the result in a variable called area_donut.

Part II: Strings#

  1. Create a variable called name that is equal to your name.

  1. By adding strings together, introduce yourself in a string. For example "Hello, my name is Tim!". You may use f-strings if you know what they are and prefer to use them.

  1. Define the string fact to be "Python programming is fun."

  1. Index fact in order to get the “o” in “Python”.

  1. Index fact in order to get the “u” in “fun” using a negative index.

  1. Slice fact in order to get the word “programming”.

  1. Uppercase fact.

  1. Replace the period at the end of fact with an exclamation point.

  1. Use .split() to get the second word of this string. Was this easier than one of the problems above?

  1. Reverse fact.

  1. Does fact contain the letter y? To find this answer, use the in keyword.

  1. How many os does fact have?

  1. Replace all the os in fact with an underscore (do not redefine fact).

Part III: Lists#

  1. Create a list friends of your three best friends, "Alice", "Bob", and "Charlie".

  1. Find the length of friends.

  1. Add your new friend "Debbie" to your list of friends.

  1. Charlie has no-showed to your piano recital. Remove him from your friends.

  1. Using the .join() method, join friends to look like this exactly: "Alice & Bob & Debbie"

  1. What is the length of []? What about [[]]? Why?

For the next few problems, we’ll use this list:

nested_deep = [1, [2, 3], 4, [5, [6, 7, [8]]]]
  1. What is the length of nested_deep?

  1. Index nested_deep to get the 5.

  1. Index nested_deep to get the 8.

  • Hint: If your answer looks like [8], that’s not the correct answer!