Add video and streamline content

This commit is contained in:
Alexander Hess 2020-03-30 14:14:52 +02:00
parent 6e68175252
commit 6d2c7aeaa1
3 changed files with 419 additions and 687 deletions

File diff suppressed because one or more lines are too long

View file

@ -5,7 +5,7 @@
"metadata": {},
"source": [
"\n",
"# Chapter 5: Bits & Numbers"
"# Chapter 5: Numbers & Bits"
]
},
{
@ -19,7 +19,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Read [Chapter 5](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/05_numbers_00_lecture.ipynb) of the book. Then, work through the questions below."
"The questions below assume that you have read [Chapter 5](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/05_numbers_00_lecture.ipynb) in the book.\n",
"\n",
"Be concise in your answers! Most questions can be answered in *one* sentence."
]
},
{
@ -47,7 +49,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -61,28 +63,14 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q3**: Why is it *inefficient* to store `bool` objects in bits resembling a **hexadecimal representation**?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q4**: Colors are commonly expressed in the **hexadecimal system** in websites (cf., the [HTML](https://en.wikipedia.org/wiki/HTML) and [CSS](https://en.wikipedia.org/wiki/Cascading_Style_Sheets) formats).\n",
"**Q3**: Colors are commonly expressed in the **hexadecimal system** in websites (cf., the [HTML](https://en.wikipedia.org/wiki/HTML) and [CSS](https://en.wikipedia.org/wiki/Cascading_Style_Sheets) formats).\n",
"\n",
"For example, $#000000$, $#ff9900$, and $#ffffff$ turn out to be black, orange, and white. The six digits are read in *pairs of two* from left to right, and the *three pairs* correspond to the proportions of red, green, and blue mixed together. They reach from $0_{16} = 0_{10}$ for $0$% to $\\text{ff}_{16} = 255_{10}$ for $100$% (cf., this [article](https://en.wikipedia.org/wiki/RGB_color_model) for an in-depth discussion).\n",
"\n",
@ -93,77 +81,77 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q5**: What does it mean for a code fragment to **fail silently**?"
"**Q4**: What does it mean for a code fragment to **fail silently**?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q6**: Explain why the mathematical set of all real numbers $\\mathbb{R}$ can only be **approximated** by floating-point numbers on a computer!"
"**Q5**: Explain why the mathematical set of all real numbers $\\mathbb{R}$ can only be **approximated** by floating-point numbers on a computer!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q7**: How do we deal with a `float` object's imprecision if we need to **check for equality**?"
"**Q6**: How do we deal with a `float` object's imprecision if we need to **check for equality**?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q8**: What data type, built-in or from the [standard library](https://docs.python.org/3/library/index.html), is best suited to represent the [transcendental numbers](https://en.wikipedia.org/wiki/Transcendental_number) $\\pi$ and $\\text{e}$?"
"**Q7**: What data type, built-in or from the [standard library](https://docs.python.org/3/library/index.html), is best suited to represent the [transcendental numbers](https://en.wikipedia.org/wiki/Transcendental_number) $\\pi$ and $\\text{e}$?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q9**: How can **abstract base classes**, for example, as defined in the **numerical tower**, be helpful in enabling **duck typing**?"
"**Q8**: How can **abstract base classes**, for example, as defined in the **numerical tower**, be helpful in enabling **duck typing**?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -184,63 +172,63 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q10**: The precision of `int` objects depends on how we choose to represent them in memory. For example, using a **hexadecimal representation** gives us $16^8$ digits whereas with a **binary representation** an `int` object can have *at most* $2^8$ digits."
"**Q9**: The precision of `int` objects depends on how we choose to represent them in memory. For example, using a **hexadecimal representation** gives us $16^8$ digits whereas with a **binary representation** an `int` object can have *at most* $2^8$ digits."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q11**: With the built-in [round()](https://docs.python.org/3/library/functions.html#round) function, we obtain a *precise* representation for any `float` object if we can live with *less than* $15$ digits of precision."
"**Q10**: With the built-in [round()](https://docs.python.org/3/library/functions.html#round) function, we obtain a *precise* representation for any `float` object if we can live with *less than* $15$ digits of precision."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q12**: As most currencies operate with $2$ or $3$ decimals (e.g., EUR $9.99$), the `float` type's limitation of *at most* $15$ digits is *not* a problem in practice."
"**Q11**: As most currencies operate with $2$ or $3$ decimals (e.g., EUR $9.99$), the `float` type's limitation of *at most* $15$ digits is *not* a problem in practice."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q13**: The [IEEE 754](https://en.wikipedia.org/wiki/IEEE_754) standard's **special values** provide no benefit in practice as we could always use a **[sentinel](https://en.wikipedia.org/wiki/Sentinel_value)** value (i.e., a \"dummy\"). For example, instead of `nan`, we can always use `0` to indicate a *missing* value."
"**Q12**: The [IEEE 754](https://en.wikipedia.org/wiki/IEEE_754) standard's **special values** provide no benefit in practice as we could always use a **[sentinel](https://en.wikipedia.org/wiki/Sentinel_value)** value (i.e., a \"dummy\"). For example, instead of `nan`, we can always use `0` to indicate a *missing* value."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q14**: The following code fragment raises an `InvalidOperation` exception. That is an example of code **failing loudly**.\n",
"**Q13**: The following code fragment raises an `InvalidOperation` exception. That is an example of code **failing loudly**.\n",
"```python\n",
"float(\"inf\") + float(\"-inf\")\n",
"```"
@ -250,63 +238,63 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q15**: Python provides a `scientific` type (e.g., `1.23e4`) that is useful mainly to model problems in the domains of physics or astrophysics."
"**Q14**: Python provides a `scientific` type (e.g., `1.23e4`) that is useful mainly to model problems in the domains of physics or astrophysics."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q16**: From a practitioner's point of view, the built-in [format()](https://docs.python.org/3/library/functions.html#format) function does the *same* as the built-in [round()](https://docs.python.org/3/library/functions.html#round) function."
"**Q15**: From a practitioner's point of view, the built-in [format()](https://docs.python.org/3/library/functions.html#format) function does the *same* as the built-in [round()](https://docs.python.org/3/library/functions.html#round) function."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q17**: The `Decimal` type from the [decimal](https://docs.python.org/3/library/decimal.html) module in the [standard library](https://docs.python.org/3/library/index.html) allows us to model the set of the real numbers $\\mathbb{R}$ *precisely*."
"**Q16**: The `Decimal` type from the [decimal](https://docs.python.org/3/library/decimal.html) module in the [standard library](https://docs.python.org/3/library/index.html) allows us to model the set of the real numbers $\\mathbb{R}$ *precisely*."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q18**: The `Fraction` type from the [fractions](https://docs.python.org/3/library/fractions.html) module in the [standard library](https://docs.python.org/3/library/index.html) allows us to model the set of the rational numbers $\\mathbb{Q}$ *precisely*."
"**Q17**: The `Fraction` type from the [fractions](https://docs.python.org/3/library/fractions.html) module in the [standard library](https://docs.python.org/3/library/index.html) allows us to model the set of the rational numbers $\\mathbb{Q}$ *precisely*."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
}
],

View file

@ -4,8 +4,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"# Chapter 5: Bits & Numbers"
"# Chapter 5: Numbers & Bits"
]
},
{
@ -19,7 +18,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Read [Chapter 5](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/05_numbers_00_lecture.ipynb) of the book. Then, work through the exercises below. The `...` indicate where you need to fill in your answers. You should not need to create any additional code cells."
"The exercises below assume that you have read [Chapter 5](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/05_numbers_00_lecture.ipynb) in the book.\n",
"\n",
"The `...`'s in the code cells indicate where you need to fill in code snippets. The number of `...`'s within a code cell give you a rough idea of how many lines of code are needed to solve the task. You should not need to create any additional code cells for your final solution. However, you may want to use temporary code cells to try out some ideas."
]
},
{
@ -80,7 +81,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -94,7 +95,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -110,7 +111,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
" "
" < your answer >"
]
},
{
@ -238,7 +239,7 @@
"\n",
"Enable **duck typing** by allowing the function to be called with various numeric types as the arguments, in particular, `quantity` may be a non-integer as well: Use an appropriate **abstract base class** from the [numbers](https://docs.python.org/3/library/numbers.html) module in the [standard library](https://docs.python.org/3/library/index.html) to verify the arguments' types and also that they are both positive!\n",
"\n",
"It is considered a *best practice* to only round towards the *end* of the calculations."
"Hint: It is considered a *best practice* to only round towards the *end* of the calculations."
]
},
{
@ -256,8 +257,43 @@
"metadata": {},
"outputs": [],
"source": [
"def discounted_price(...):\n",
" ..."
"def discounted_price(unit_price, quantity):\n",
" \"\"\"Calculate the price of a line item in an order.\n",
"\n",
" Args:\n",
" unit_price (numbers.Real): price of an ordered item; must be positive\n",
" quantity (numbers.Real): number of items ordered; must be positive\n",
"\n",
" Returns:\n",
" line_item_price (decimal.Decimal): precision of 2 decimals\n",
" \"\"\"\n",
" # Basic input validation.\n",
" ...\n",
" ...\n",
" ...\n",
" ...\n",
"\n",
" # Calculate the final price if only\n",
" # the first discount scheme is applied.\n",
" ...\n",
" ...\n",
" ...\n",
" ...\n",
"\n",
" # Calculate the final price if only\n",
" # the second discount scheme is applied.\n",
" # \"One in every five\" means we need to figure out\n",
" # how many full groups of five are contained.\n",
" ...\n",
" ...\n",
" ...\n",
" ...\n",
" ...\n",
"\n",
" # Choose the better option for the customer.\n",
" ...\n",
" # Round correctly for pricing purposes.\n",
" return ..."
]
},
{