{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Climb the Ladder!\n", "Our class moves quickly! Here you'll learn Python _slowly_, by doing many, many examples. Problems in this...

1 answer below ยป
Basic Python


{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Climb the Ladder!\n", "Our class moves quickly! Here you'll 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." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Section I: Numbers, variables, and math." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1) What is 123456789 times 987654321?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2) Create a variable called `mass` that is equal to 100." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3) Create a variable called `velocity` that is equal to 5." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "4) 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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "5) 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. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### How much icing do you need?\n", "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.\n", "\n", "![](imgs/donut.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "6) Create a variable called `pi` that is equal to 3.141" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "7) 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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "8) Find the area of the larger circle (the whole doughnut, including the hole). Call this variable `area_big_circle`.\n", "\n", "* _Hint:_ The area of a circle with radius $r$ is $A = \\pi r^2$." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "9) Find the area of the smaller circle (the hole). Call this variable `area_small_circle`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "10) Find the area of one triangle (a sprinkle). Call this variable `area_sprinkle`.\n", "\n", "* _Hint:_ The area of a triangle with base $b$ and height $h$ is $A = \\frac{1}{2}bh$." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "11) 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`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Part II: Strings" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "12) Create a variable called `name` that is equal to your name." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "13) 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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "14) Define the string `fact` to be `\"Python programming is fun.\"`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "15) Index `fact` in order to get the \"o\" in \"Python\"." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "16) Index `fact` in order to get the \"u\" in \"fun\" using a negative index." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "17) Slice `fact` in order to get the word \"programming\"." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "18) Uppercase `fact`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown",
Answered Same DayOct 17, 2021

Answer To: { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Climb the Ladder!\n", "Our...

Vibhav answered on Oct 17 2021
122 Votes
assign/greynodes0117.ipynb
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Climb the Ladder!\n",
"Our class moves quickly! Here you'll 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."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Section I: Numbers, variables, and math."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1) What is 123456789 times 987654321?"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"121932631112635269\n"
]
}
],
"source": [
"print(123456789*987654321)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2) Create a variable called `mass` that is equal to 100."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"mass=100"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3) Create a variable called `velocity` that is equal to 5."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"velocity=5"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"4) 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."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Momentum: 500\n"
]
}
],
"source": [
"p = mass*velocity\n",
"print('Momentum:', p)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"5) 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. "
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1250.0\n"
]
}
],
"source": [
"K = 0.5*mass*velocity**2\n",
"print(K)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### How much icing do you need?\n",
"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.\n",
"\n",
"![](imgs/donut.png)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"6) Create a variable called `pi` that is equal to 3.141"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"pi = 3.141"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"7) 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."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"R = 13.425\n",
"r = 4.792\n",
"s = 3.28"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"8) Find the area of the larger circle (the whole doughnut, including the hole). Call this variable `area_big_circle`.\n",
"\n",
"* _Hint:_ The area of a circle with radius $r$ is $A = \\pi r^2$."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"566.1043931250001\n"
]
}
],
"source": [
"area_big_circle = pi*R*R\n",
"print(area_big_circle)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"9) Find the area of the smaller circle (the hole). Call this variable `area_small_circle`."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"72.127612224\n"
]
}
],
"source"
: [
"area_small_circle = pi*r*r\n",
"print(area_small_circle)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"10) Find the area of one triangle (a sprinkle). Call this variable `area_sprinkle`.\n",
"\n",
"* _Hint:_ The area of a triangle with base $b$ and height $h$ is $A = \\frac{1}{2}bh$."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5.379199999999999\n"
]
}
],
"source": [
"#Since image is not available and it's unclear where the triangle is located, I assume b and h are same for triangle\n",
"area_sprinkle = 0.5*s*s\n",
"print(area_sprinkle)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"11) 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`."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"493.9767809010001\n"
]
}
],
"source": [
"#It's unclear which shaded region is the area of concern, since there is no image\n",
"#I assume it's the area of bigger circle, which is not part of smaller circle\n",
"area_donut = area_big_circle - area_small_circle\n",
"print(area_donut)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Part II: Strings"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"12) Create a variable called `name` that is equal to your name."
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"name = \"Abcdxyz\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"13) 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."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello, my name is Abcdxyz!\n"
]
}
],
"source": [
"print('Hello, my name is ', name, '!', sep='')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"14) Define the string `fact` to be `\"Python programming is fun.\"`"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"fact = 'Python programming is fun.'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"15) Index `fact` in order to get the \"o\" in \"Python\"."
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"o\n"
]
}
],
"source": [
"print(fact[4])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"16) Index `fact` in order to get the \"u\" in \"fun\" using a negative index."
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'u'"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fact[-3]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"17) Slice `fact` in order to get the word \"programming\"."
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'programming'"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fact[7:18]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"18) Uppercase `fact`."
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"PYTHON PROGRAMMING IS FUN.\n"
]
}
],
"source": [
"fact = str.upper(fact)\n",
"print(fact)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"19) Replace the period at the end of `fact` with an exclamation point."
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'PYTHON PROGRAMMING IS FUN!'"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#It will replace all periods in the string, if we want to replace a certain period then we'll have to use lists\n",
"fact.replace('.', '!')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"20) Use `.split()` to get the second word of this string. Was this easier than one of the problems above?"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'PROGRAMMING'"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fact.split(' ')[1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"21) Reverse `fact`."
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
".NUF SI GNIMMARGORP NOHTYP\n"
]
}
],
"source": [
"fact = fact[-1::-1]\n",
"print(fact)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"22) Does `fact` contain the letter `y`? To find this answer, use the `in` keyword."
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Since it has been capitalized, no y is there, but Y is present\n",
"'y' in fact"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"23) How many `o`s does `fact` have?"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Fact is capitalized, so it does not contain o but contains 2 O's\n",
"fact.count('O')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"24) Replace all the `o`s in `fact` with an underscore (do not redefine `fact`)."
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'.NUF SI GNIMMARG_RP N_HTYP'"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fact.replace('O', '_')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Part III: Lists"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"25) Create a list `friends` of your three best friends, `\"Alice\"`, `\"Bob\"`, and `\"Charlie\"`."
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [],
"source": [
"friends = ['Alice', 'Bob', 'Charlie']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"26) Find the length of `friends`."
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(friends)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"27) Add your new friend, `\"Debbie\"` to your list of `friends`."
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [],
"source": [
"friends.append('Debbie')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"28) Charlie has no-showed to your piano recital. Remove him from your `friends`."
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
"friends.remove('Charlie')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"29) Using the `.join()` method, join `friends` to look like this _exactly_: `\"Alice & Bob & Debbie\"`"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Alice & Bob & Debbie'"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"' & '.join(friends)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"30) What is the length of `[]`? What about `[[]]`? Why?"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len([])"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len([[]])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Length in first case is 0 and in second is 1, since first list doesn't contain anything hence lenght is 0. But, in second there is a list inside another list. So, the outer list contains one list, it doesn't matter whether it is empty, but it is a list. So, outer list contains one list, and len() returns just that."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For the next few problems, we'll use this list:"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [],
"source": [
"nested_deep = [1, [2, 3], 4, [5, [6, 7, [8]]]]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"31) What is the length of `nested_deep`?"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(nested_deep)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"32) Index `nested_deep` to get the 5."
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nested_deep[3][0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"33) Index `nested_deep` to get the 8.\n",
"\n",
"* _Hint:_ If your answer looks like `[8]`, that's not the correct answer!"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"8"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nested_deep[3][1][2][0]"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"state": {},
"version_major": 2,
"version_minor": 0
}
}
},
"nbformat": 4,
"nbformat_minor": 4
}
assign/Greynodes1710.ipynb
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Climb the Ladder!\n",
"Our class moves quickly! Here you'll 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."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Section IV: Conditionals and Loops"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"34) Print out the numbers 1 through 10."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"6\n",
"7\n",
"8\n",
"9\n",
"10\n"
]
}
],
"source": [
"for i in range(1, 11):\n",
" print(i)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"35) Print out the odd numbers between 1 and 20 by using the third paramter to the `range()` function."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"3\n",
"5\n",
"7\n",
"9\n",
"11\n",
"13\n",
"15\n",
"17\n",
"19\n"
]
}
],
"source": [
"for i in range(1, 21, 2):\n",
" print(i)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"36) Print out the odd numbers between 1 and 20 by checking if the number is even before printing."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"3\n",
"5\n",
"7\n",
"9\n",
"11\n",
"13\n",
"15\n",
"17\n",
"19\n"
]
}
],
"source": [
"for i in range(1, 21):\n",
" if i %2 ==0:\n",
" continue\n",
" else:\n",
" print(i)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"37) What is the sum of the first 100 integers? Hint: Create a variable `total` equal to zero, and increment `total` inside a loop."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5050\n"
]
}
],
"source": [
"total = 0\n",
"for i in range(1, 101):\n",
" total += i\n",
"print(total)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"38) For each friend in your `friends` list, print `\"PERSON IS AWESOME!\"` in all caps, where `PERSON` is of course replaced with your friend's name."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"friends = [\"Alice\", \"Bob\", \"Charlie\", \"Debbie\"]"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ALICE IS AWESOME!\n",
"BOB IS AWESOME!\n",
"CHARLIE IS AWESOME!\n",
"DEBBIE IS AWESOME!\n"
]
}
],
"source": [
"for frnd in friends:\n",
" print(str.upper(frnd), 'IS AWESOME!')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"39) Similar to the last problem, print `\"PERSON IS AWESOME!\"` for each friend in your friends list _only if their name ends in a vowel_. Otherwise, print `\"person is ok...\"` (all lowercase). "
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"PERSON IS AWESOME!\n",
"person is ok...\n",
"PERSON IS AWESOME!\n",
"PERSON IS AWESOME!\n"
]
}
],
"source": [
"vowels = ['a', 'e' , 'i', 'o', 'u']\n",
"for frnd in friends:\n",
" if frnd[-1] in vowels:\n",
" print('PERSON IS AWESOME!')\n",
" else:\n",
" print('person is ok...')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"40) For each number between 1 and 30, if a number is divisible by 3, print `\"Fizz\"`. Otherwise, just print the number."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"Fizz\n",
"4\n",
"5\n",
"Fizz\n",
"7\n",
"8\n",
"Fizz\n",
"10\n",
"11\n",
"Fizz\n",
"13\n",
"14\n",
"Fizz\n",
"16\n",
"17\n",
"Fizz\n",
"19\n",
"20\n",
"Fizz\n",
"22\n",
"23\n",
"Fizz\n",
"25\n",
"26\n",
"Fizz\n",
"28\n",
"29\n",
"Fizz\n"
]
}
],
"source": [
"for i in range(1, 31):\n",
" if i%3 == 0:\n",
" print('Fizz')\n",
" else:\n",
" print(i)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"41) For each number between 1 and 30, if a number is divisible by 5, print `\"Buzz\"`. Otherwise, just print the number."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"3\n",
"4\n",
"Buzz\n",
"6\n",
"7\n",
"8\n",
"9\n",
"Buzz\n",
"11\n",
"12\n",
"13\n",
"14\n",
"Buzz\n",
"16\n",
"17\n",
"18\n",
"19\n",
"Buzz\n",
"21\n",
"22\n",
"23\n",
"24\n",
"Buzz\n",
"26\n",
"27\n",
"28\n",
"29\n",
"Buzz\n"
]
}
],
"source": [
"for i in range(1, 31):\n",
" if i%5 == 0:\n",
" print('Buzz')\n",
" else:\n",
" print(i)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"42) **FIZZBUZZ!** For each number between 1 and 30,\n",
"* if a number is divisible by 3, print `\"Fizz\"`\n",
"* if a number is divisible by 5, print `\"Buzz\"`\n",
"* if a number is divisible by both 3 and 5, instead print `\"Fizzbuzz\"`,\n",
"* otherwise, just print the number\n",
"\n",
"This problem is the famous \"Fizzbuzz\" problem - and is a very common interview coding challenge! If you can do this without help, you're making awesome progress! Well done!"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"Fizz\n",
"4\n",
"Buzz\n",
"Fizz\n",
"7\n",
"8\n",
"Fizz\n",
"Buzz\n",
"11\n",
"Fizz\n",
"13\n",
"14\n",
"Fizzbuzz\n",
"16\n",
"17\n",
"Fizz\n",
"19\n",
"Buzz\n",
"Fizz\n",
"22\n",
"23\n",
"Fizz\n",
"Buzz\n",
"26\n",
"Fizz\n",
"28\n",
"29\n",
"Fizzbuzz\n"
]
}
],
"source": [
"for i in range(1, 31):\n",
" if i%3 == 0 and i%5 == 0:\n",
" print('Fizzbuzz')\n",
" elif i%3 == 0:\n",
" print('Fizz')\n",
" elif i%5 == 0:\n",
" print('Buzz')\n",
" else:\n",
" print(i)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"43) Define some string `sentence`. If you can't think of a good one, feel free to use `fact` from the previous notebook."
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"sentence = 'Python programming is fun'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"44) Count the number of vowels in `sentence` (no, y is not a vowel).\n",
"* _Hint:_ What happens if you loop through a string?"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Vowels: 6\n"
]
}
],
"source": [
"count = 0\n",
"vowels = ['a','e','i','o','u']\n",
"for i in range(len(sentence)):\n",
" if sentence[i] in vowels:\n",
" count += 1\n",
" \n",
"print('Vowels:', count)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"45) Define a variable `sentence_rm` that is equal to `sentence` with all vowels removed. \n",
"* _Hint:_ This is a tough one. Start out with an empty string and concatenate all of the consonants to it inside a loop."
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Pythn prgrmmng s fn\n"
]
}
],
"source": [
"sentence_rm = ''\n",
"vowels = ['a','e','i','o','u']\n",
"for i in range(len(sentence)):\n",
" if sentence[i] not in vowels:\n",
" sentence_rm += sentence[i]\n",
"print(sentence_rm)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"46) Create a variable `fav_number` equal to whatever is your favorite integer. Write a loop to determine if `fav_number` is prime. Recall that a prime number is any number whose only divisors are 1 and itself. For example, 6 is not prime since it's divisible by 1, 2, 3, and 6. But 7 _is_ prime since it's only divisible by 1 and 7."
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Prime\n"
]
}
],
"source": [
"fav_number = 11\n",
"prime = True\n",
"for i in range(2, fav_number):\n",
" if fav_number % i == 0:\n",
" prime = False\n",
" break\n",
" \n",
"if not prime:\n",
" print('Not a prime')\n",
"else:\n",
" print('Prime')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"47) A palindromic number reads the same both ways. For example, 1234321 is a palindrome. Write an `if` statement to test whether or not a number is palindromic."
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Not palindrome\n"
]
}
],
"source": [
"num = 12345321\n",
"if str(num) == str(num)[-1::-1]:\n",
" print('Palindrome')\n",
"else:\n",
" print('Not palindrome')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"48) Find the largest palindrome made from the product of two two-digit numbers.\n",
"* _Tip:_ Do not worry about the \"efficiency\" of your answer! The easiest answer is very inefficient."
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"scrolled": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Greatest palindrome: 9009\n"
]
}
],
"source": [
"greatest = 0\n",
"for i in range(10,100):\n",
" for j in range(10,100):\n",
" product = i*j\n",
" if str(product) == str(product)[-1::-1] and product > greatest:\n",
" greatest = product\n",
" \n",
"print('Greatest palindrome:', greatest)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# BONUS: List Comprehensions\n",
"\n",
"**Disclaimer:** Many (maybe all) of these exercises can be done without a list comprehension. However, listcomps are often the easiest way to solve a problem like this. Listcomps are also optimized in Python, meaning they are also often the _fastest_ way to solve a problem. In fact, **every problem in this section can be solved in one line of code!**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"49) Create a list of the numbers 1 through 10."
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n"
]
}
],
"source": [
"l = [i for i in range(1,11)]\n",
"print(l)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"50) Create a list of the squares of the numbers between 1 and 10."
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n"
]
}
],
"source": [
"l = [i*i for i in range(1,11)]\n",
"print(l)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"51) Create a list of all the even numbers between 18 and 47."
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46]\n"
]
}
],
"source": [
"l = [i for i in range(18,47) if i%2==0]\n",
"print(l)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"52) Create a list of the squares of the even numbers between 1 and 20."
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[324, 400, 484, 576, 676, 784, 900, 1024, 1156, 1296, 1444, 1600, 1764, 1936, 2116]\n"
]
}
],
"source": [
"l = [i*i for i in range(18,47) if i%2==0]\n",
"print(l)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"53) What is the sum of the cubes of the numbers between 7 and 37?"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"443115"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum([i**3 for i in range(7,37)])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"54) Create a list of all of the numbers divisible by either 3 or 5 (or both) between 1 and 30."
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[3, 5, 6, 9, 10, 12, 15, 18, 20, 21, 24, 25, 27]\n"
]
}
],
"source": [
"l = [i for i in range(1,30) if i%3==0 or i%5==0]\n",
"print(l)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"55) Redefine this list to be all uppercase."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"friends = [\"Alice\", \"Bob\", \"Charlie\", \"Derek\"]"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['ALICE', 'BOB', 'CHARLIE', 'DEBBIE']\n"
]
}
],
"source": [
"frnds = [str.upper(fr) for fr in friends]\n",
"print(frnds)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"56) Remove all elements from `friends` whose names do not end in a vowel."
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Alice', 'Charlie', 'Debbie']\n"
]
}
],
"source": [
"frnds = [fr for fr in friends if fr[-1] in ['a','e','i','o','u']]\n",
"print(frnds)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"57) Take the `sentence` you used earlier and remove the vowels again. This time, using a one-line list comprehension.\n",
"\n",
"* _Hint:_ You'll find the `.join()` method to be useful here."
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Pythn prgrmmng s fn'"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"''.join([ch for ch in sentence if ch not in ['a','e','i','o','u']])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"58) You need to access Excel files that have names like `Jan 5.xlsx` or `Mar 15.xlsx`. Create a list of January 1 through January 31 like this. That is, create the list of: `[\"Jan 1.xlsx\", \"Jan 2.xlsx\", ... , \"Jan 31.xlsx\"]`."
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"['Jan1.xlsx',\n",
" 'Jan2.xlsx',\n",
" 'Jan3.xlsx',\n",
" 'Jan4.xlsx',\n",
" 'Jan5.xlsx',\n",
" 'Jan6.xlsx',\n",
" 'Jan7.xlsx',\n",
" 'Jan8.xlsx',\n",
" 'Jan9.xlsx',\n",
" 'Jan10.xlsx',\n",
" 'Jan11.xlsx',\n",
" 'Jan12.xlsx',\n",
" 'Jan13.xlsx',\n",
" 'Jan14.xlsx',\n",
" 'Jan15.xlsx',\n",
" 'Jan16.xlsx',\n",
" 'Jan17.xlsx',\n",
" 'Jan18.xlsx',\n",
" 'Jan19.xlsx',\n",
" 'Jan20.xlsx',\n",
" 'Jan21.xlsx',\n",
" 'Jan22.xlsx',\n",
" 'Jan23.xlsx',\n",
" 'Jan24.xlsx',\n",
" 'Jan25.xlsx',\n",
" 'Jan26.xlsx',\n",
" 'Jan27.xlsx',\n",
" 'Jan28.xlsx',\n",
" 'Jan29.xlsx',\n",
" 'Jan30.xlsx',\n",
" 'Jan31.xlsx']"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"['Jan'+str(i)+'.xlsx' for i in range(1, 32)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"59) Below is a list of amounts in euros. Create a new list, `dollars`, which is these amounts converted to U.S. dollars. There are 1.1 dollars per euro."
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
"euros = [4.50, 6.70, 3.25, 9.99, 12.75, 0.35]"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[4.95, 7.370000000000001, 3.575, 10.989, 14.025, 0.385]\n"
]
}
],
"source": [
"dollars = [1.1*e for e in euros]\n",
"print(dollars)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"60) Below is a list of heights in inches. In order to ride a roller coaster, you must be at least 5 feet tall (60 inches). Filter this list to be _only_ those heights tall enough to ride."
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [],
"source": [
"heights = [71, 48, 55, 65, 68, 60, 58, 53]"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[71, 65, 68, 60]"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[h for h in heights if h>=60]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"61) Repeat the above exercise, except replace the lower heights with a `None` instead of dropping them.\n",
"* _Hint:_ Python has an in-line `if` statement, sometimes called a _ternary operator_ that you might find useful here. For example:\n",
"\n",
"```\n",
"print(\"Tall enough\" if 72 > 60 else \"Too short\")\n",
"print(\"Tall enough\" if 52 > 60 else \"Too short\")\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[71, None, None, 65, 68, 60, None, None]"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[h if h>=60 else None for h in heights]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"62) Here are more heights below. However, this time, the information is contained in a dictionary. Create a list of the names _and ONLY the names_ of the people who are tall enough to ride the roller coaster.\n",
"\n",
"* _Hint:_ How can you loop through a dictionary?"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [],
"source": [
"people = {\n",
" \"Aaron\": 58,\n",
" \"Barbara\": 66,\n",
" \"Clarence\": 62,\n",
" \"Donovan\": 55,\n",
" \"Erika\": 70,\n",
" \"Fernando\": 72\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Barbara', 'Clarence', 'Erika', 'Fernando']"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[k for k in people.keys() if people[k]>=60]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"63) Below we have some more data on our classmates. This time, the dictionary values are test scores. A student's final grade is their _maximum_ score on these three tests. Create a list of the students' final grades."
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [],
"source": [
"people = {\n",
" \"Aaron\": [87, 52, 78],\n",
" \"Barbara\": [92, 79, 85],\n",
" \"Clarence\": [42, 68, 55],\n",
" \"Donovan\": [95, 100, 87],\n",
" \"Erika\": [62, 88, 47],\n",
" \"Fernando\": [84, 99, 0]\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[87, 92, 68, 100, 88, 99]"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[max(people[k]) for k in people.keys()]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"64) Repeat the above problem, except create a _dictionary_ of the final scores, where the dictionary keys are the names, and the dictionary values are the final grade.\n",
"\n",
"* _Hint:_ You can do **dictionary comprehension** to solve this problem! Yes, it exists!"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Aaron': 87,\n",
" 'Barbara': 92,\n",
" 'Clarence': 68,\n",
" 'Donovan': 100,\n",
" 'Erika': 88,\n",
" 'Fernando': 99}"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"{k: max(people[k]) for k in people.keys()}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"65) Using the two lists defined below, create the following resulting list:\n",
"\n",
"`[\"AZ\", \"BY\", \"CX\", \"DW\", \"EV\"]`\n",
"\n",
"* _Hint:_ Check out the `zip()` function."
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"outputs": [],
"source": [
"letters_a = [\"A\", \"B\", \"C\", \"D\", \"E\"]\n",
"letters_z = [\"Z\", \"Y\", \"X\", \"W\", \"V\"]"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['AZ', 'BY', 'CX', 'DW', 'EV']"
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[''.join(x) for x in zip(letters_a, letters_z)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"66) Using `letters_a` defined in the previous problem, create the following list:\n",
"\n",
"```\n",
"[\n",
" \"A is letter 1 of the alphabet\",\n",
" \"B is letter 2 of the alphabet\",\n",
" ...\n",
"]\n",
"```\n",
"\n",
"* _Hint:_ Checkout the `enumerate()` function."
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['A is letter 1 of the alphabet',\n",
" 'B is letter 2 of the alphabet',\n",
" 'C is letter 3 of the alphabet',\n",
" 'D is letter 4 of the alphabet',\n",
" 'E is letter 5 of the alphabet']"
]
},
"execution_count": 73,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[letter + ' is letter ' + str(i+1) + ' of the alphabet' for i, letter in enumerate(letters_a)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Section VI: Libraries, imports and some more math!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"67) Import the `math` library"
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {},
"outputs": [],
"source": [
"import math"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"68) The `math` library has a lot of cool stuff in it. For this problem, evaluate the following math expressions:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"a) $\\sin{\\frac{\\pi}{2}}$"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.0"
]
},
"execution_count": 75,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"math.sin(math.pi/2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"b) $\\ln{\\sqrt{e}}$\n",
"* _Hint:_ A logarithm with base $e$ is represented by just `log` in `math`. In fact, throughout the course, $\\log$ with no subscript will always denote the natural log (ie, log base $e$)."
]
},
{
"cell_type": "code",
"execution_count": 78,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.6487212707001282"
]
},
"execution_count": 78,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"math.sqrt(math.e)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"c) $e^{3!}$"
]
},
{
"cell_type": "code",
"execution_count": 79,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"403.428793492735"
]
},
"execution_count": 79,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"math.pow(math.e, math.factorial(3))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"69) Two popular math functions are the \"round up\" and \"round down\" functions. In computer science, we call them the \"ceiling\" and \"floor\" functions. They're used and denoted as follows:\n",
"\n",
"* `ceil(3.8)` $= \\lceil 3.8 \\rceil = 4$\n",
"* `ceil(3.2)` $= \\lceil 3.2 \\rceil = 4$\n",
"* `floor(3.8)` $= \\lfloor 3.8 \\rfloor = 3$\n",
"* `floor(3.2)` $= \\lfloor 3.2 \\rfloor = 3$\n",
"\n",
"We _only_ need these two functions out of the `math` library. Use an `import` statement to import _only_ thees two functions and verify the above examples are true."
]
},
{
"cell_type": "code",
"execution_count": 80,
"metadata": {},
"outputs": [],
"source": [
"from math import ceil, floor"
]
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n",
"3\n"
]
}
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions ยป

Submit New Assignment

Copy and Paste Your Assignment Here