Merge in "03-conditionals"
Merge branch "03-conditionals" into "develop" Summary of the merged in commits: *0b067ec
: Add review and exercises for notebook 03 *e184511
: Adjust content overview in README.md *4e2b329
: Add initial version of notebook 03 *514b2da
: Streamline previous content
This commit is contained in:
commit
dda246284b
7 changed files with 2236 additions and 28 deletions
|
@ -429,7 +429,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"Here is a brief history of and some background on Python:\n",
|
||||
"Here is a brief history of and some background on Python (cf., also this [TechRepublic article](https://www.techrepublic.com/article/python-is-eating-the-world-how-one-developers-side-project-became-the-hottest-programming-language-on-the-planet/) for a more elaborate story):\n",
|
||||
"\n",
|
||||
"- [Guido van Rossum](https://en.wikipedia.org/wiki/Guido_van_Rossum) (Python’s **[Benevolent Dictator for Life](https://en.wikipedia.org/wiki/Benevolent_dictator_for_life)**) was bored during a week around Christmas 1989 and started Python as a hobby project \"that would keep \\[him\\] occupied\" for some days\n",
|
||||
"- the idea was to create a **general-purpose scripting** language that would allow fast **prototyping** and would **run on every operating system**\n",
|
||||
|
@ -439,6 +439,17 @@
|
|||
"- the language is named after the sketch comedy group [Monty Python](https://en.wikipedia.org/wiki/Monty_Python)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "skip"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"#### Summary"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
|
@ -447,7 +458,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"Python is a **general-purpose** programming language that allows for **fast development**, is **easy to comprehend**, **open-source**, long established, unifies the knowledge of **hundreds of thousands of experts** around the world, runs on basically every machine, and can handle the complexities of applications involving **big data**."
|
||||
"Python is a **general-purpose** programming language that allows for **fast development**, is **easy to read**, **open-source**, long established, unifies the knowledge of **hundreds of thousands of experts** around the world, runs on basically every machine, and can handle the complexities of applications involving **big data**."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -770,7 +781,7 @@
|
|||
" 1. Elements of a Program\n",
|
||||
" 2. Functions & Modularization\n",
|
||||
"- What is the flow of execution? How can we form sentences from words?\n",
|
||||
" 3. Boolean Logic & Conditionals\n",
|
||||
" 3. Conditionals & Exceptions\n",
|
||||
" 4. Recursion & Looping"
|
||||
]
|
||||
},
|
||||
|
@ -813,7 +824,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"As with every good lecture, there has to be a [xkcd](https://xkcd.com/353/) comic somewhere."
|
||||
"As with every good book, there has to be a [xkcd](https://xkcd.com/353/) comic somewhere."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
|
@ -742,7 +742,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"The normal order of precedence from mathematics applies (i.e., \"PEMDAS\" rule) but parentheses help avoid confusion."
|
||||
"The normal [order of precedence](https://docs.python.org/3/reference/expressions.html#operator-precedence) from mathematics applies (i.e., \"PEMDAS\" rule) but parentheses help avoid confusion."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -949,7 +949,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"139867825605456"
|
||||
"139940106427216"
|
||||
]
|
||||
},
|
||||
"execution_count": 27,
|
||||
|
@ -973,7 +973,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"139867825776440"
|
||||
"139940106602344"
|
||||
]
|
||||
},
|
||||
"execution_count": 28,
|
||||
|
@ -997,7 +997,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"139867825460912"
|
||||
"139940105769456"
|
||||
]
|
||||
},
|
||||
"execution_count": 29,
|
||||
|
@ -1041,7 +1041,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"`a` and `d` indeed have the same value as can be checked with the **equality operator** `==`. The resulting `True` (and the `False` below) is yet another data type, a so-called **boolean**."
|
||||
"`a` and `d` indeed have the same value as can be checked with the **equality operator** `==`. The resulting `True` (and the `False` below) is yet another data type, a so-called **boolean**. We will look into that closely in Chapter 3."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -1780,7 +1780,7 @@
|
|||
" "
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.lib.display.YouTubeVideo at 0x7f358405d550>"
|
||||
"<IPython.lib.display.YouTubeVideo at 0x7f46584be518>"
|
||||
]
|
||||
},
|
||||
"execution_count": 50,
|
||||
|
@ -2087,7 +2087,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"A variable can be **re-assigned** as often as we wish. Thereby, we could also assign an object of a different type. Because this is allowed, Python is said to be a **dynamically typed** language. On the contrary, a **statically typed** language like C also allows re-assignment but only with objects of the same type. This subtle distinction is one reason why Python is slower at execution than C: As it runs a program, it needs to figure out an object's type each time it is referenced. But as mentioned before, this can be mitigated with third-party libraries."
|
||||
"A variable can be **re-assigned** as often as we wish. Thereby, we could also assign an object of a *different* type. Because this is allowed, Python is said to be a **dynamically typed** language. On the contrary, a **statically typed** language like C also allows re-assignment but only with objects of the *same* type. This subtle distinction is one reason why Python is slower at execution than C: As it runs a program, it needs to figure out an object's type each time it is referenced. But as mentioned before, this can be mitigated with third-party libraries."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -2209,7 +2209,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"Variables can be **[de-referenced](https://docs.python.org/3/reference/simple_stmts.html#the-del-statement)** (i.e., \"deleted\") with the `del` statement. This does *not* delete the object to which a variable points to. It merely removes the variable from the \"list of all variables\"."
|
||||
"Variables can be **[de-referenced](https://docs.python.org/3/reference/simple_stmts.html#the-del-statement)** (i.e., \"deleted\") with the `del` statement. This does *not* delete the object to which a variable points to. It merely removes the variable's name from the \"global list of all names\"."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -2257,7 +2257,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"If we refer to an unknown name, a runtime error occurs, namely a `NameError`."
|
||||
"If we refer to an unknown name, a *runtime* error occurs, namely a `NameError`. The `Name` in `NameError` gives a hint as to why we prefer the term *name* over *identifier*: Python just uses it more often in its error messages."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -2293,7 +2293,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"Some variables magically exist when we start Python. In this introductory book, we can safely ignore them."
|
||||
"Some variables magically exist when we start a Python process or are added by Jupyter. We can safely ignore the former until Chapter 10 and the latter for good."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -2740,7 +2740,7 @@
|
|||
"source": [
|
||||
"Let's change the first element of `x`.\n",
|
||||
"\n",
|
||||
"Chapter 7 discusses lists in more depth. For now, let's just view a `list` object as some sort of **container** that holds an arbitrary number of pointers to other objects and treat brackets `[]` attached to it as just another operator, called the **indexing operator**. `x[0]` instructs Python to first follow the pointer from the \"global\" directory of all names to the `x` object. Then, it follows the first pointer it finds there to the `1` object. The indexing operator must be an operator as we merely read the first element and do not change anything in memory.\n",
|
||||
"Chapter 7 discusses lists in more depth. For now, let's just view a `list` object as some sort of **container** that holds an arbitrary number of pointers to other objects and treat the brackets `[]` attached to it as just another operator, called the **indexing operator**. `x[0]` instructs Python to first follow the pointer from the global list of all names to the `x` object. Then, it follows the first pointer it finds there to the `1` object. The indexing operator must be an operator as we merely read the first element and do not change anything in memory.\n",
|
||||
"\n",
|
||||
"Note how Python **begins counting at 0**. This is not the case for many other languages, for example, [MATLAB](https://en.wikipedia.org/wiki/MATLAB), [R](https://en.wikipedia.org/wiki/R_%28programming_language%29), or [Stata](https://en.wikipedia.org/wiki/Stata). To understand why this makes sense, see this short [note](https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html) by one of the all-time greats in computer science, the late [Edsger Dijkstra](https://en.wikipedia.org/wiki/Edsger_W._Dijkstra)."
|
||||
]
|
||||
|
@ -2777,7 +2777,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"To change the first entry in the list, we use the assignment statement `=` again. Here, this does actually *not* create a *new* variable (or overwrite an existing one) but only changes the object to which the first pointer in the `x` list points to. As we only change parts of the `x` object, we say that we **mutate** (i.e., change) its **state**."
|
||||
"To change the first entry in the list, we use the assignment statement `=` again. Here, this does actually *not* create a *new* variable (or overwrite an existing one) but only changes the object to which the first pointer in the `x` list points to. As we only change parts of the `x` object, we say that we **mutate** (i.e., \"change\") its **state**. To use the bag analogy from above, we keep the same bag but \"flip\" some of the $0$s into $1$s and some of the $1$s into $0$s."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -2890,9 +2890,9 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"**[Phil Karlton](https://skeptics.stackexchange.com/questions/19836/has-phil-karlton-ever-said-there-are-only-two-hard-things-in-computer-science)** famously noted (during his time at [Netscape](https://en.wikipedia.org/wiki/Netscape)):\n",
|
||||
"[Phil Karlton](https://skeptics.stackexchange.com/questions/19836/has-phil-karlton-ever-said-there-are-only-two-hard-things-in-computer-science) famously noted during his time at [Netscape](https://en.wikipedia.org/wiki/Netscape):\n",
|
||||
"\n",
|
||||
"> \"There are two hard problems in computer science: naming things and cache invalidation ... and off-by-one errors.\""
|
||||
"> \"There are *two* hard problems in computer science: *naming things* and *cache invalidation* ... and *off-by-one* errors.\""
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -3077,7 +3077,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"Variables with leading and trailing double underscores, referred to as **dunder** in Python \"slang\", are used for important built-in variables. Do *not* use this style for custom variables!"
|
||||
"Variables with leading and trailing double underscores, referred to as **dunder** in Python jargon, are used for important built-in functionalities. Do *not* use this style for custom variables unless you know exactly what you are doing!"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -3123,7 +3123,7 @@
|
|||
}
|
||||
},
|
||||
"source": [
|
||||
"This PyCon talk by [Ned Batchelder](https://nedbatchelder.com/) (a software engineer at [edX](https://www.edx.org/) and the organizer of the [Python User Group](https://www.meetup.com/bostonpython/) in Boston) summarizes all situations where some sort of variable assignment is done in Python. The content is intermediate and therefore it is ok if you do not understand everything at this point. However, the contents should be known by everyone claiming to be a Pythonista."
|
||||
"This PyCon talk by [Ned Batchelder](https://nedbatchelder.com/), a well-known Pythonista and the organizer of the [Python User Group](https://www.meetup.com/bostonpython/) in Boston, summarizes all situations where some sort of variable assignment is done in Python. The content is intermediate and therefore it is ok if you do not understand everything at this point. However, the contents should be known by everyone claiming to be proficient in Python."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -3150,7 +3150,7 @@
|
|||
" "
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.lib.display.YouTubeVideo at 0x7f35840cfe48>"
|
||||
"<IPython.lib.display.YouTubeVideo at 0x7f465852de48>"
|
||||
]
|
||||
},
|
||||
"execution_count": 93,
|
|
@ -142,7 +142,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"140620654002656"
|
||||
"139655430681056"
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
|
@ -635,7 +635,7 @@
|
|||
"source": [
|
||||
"Code gets even more confusing when variables by the same name from different scopes collide. In particular, what should we expect to happen if a function changes a globally defined variable internally?\n",
|
||||
"\n",
|
||||
"`average_odds()` below works like `average_evens()` above except that it **[casts](https://en.wikipedia.org/wiki/Type_conversion)** (i.e., \"converts\") the elements of `numbers` as objects of type `int` with the [int()](https://docs.python.org/3/library/functions.html#int) built-in first before filtering and averaging them. In doing so, it introduces an *internal* variable `nums` whose name collides with the one in the global scope."
|
||||
"`average_odds()` below works like `average_evens()` above except that it **[casts](https://en.wikipedia.org/wiki/Type_conversion)** (i.e., \"converts\") the elements of `numbers` as objects of type `int` with the [int()](https://docs.python.org/3/library/functions.html#int) built-in first before filtering and averaging them. In doing so, it introduces an *internal* variable `nums` whose name collides with the one in the global scope. The **inequality operator** `!=` is just the **reversed** version of `==`."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -658,7 +658,7 @@
|
|||
" float: average\n",
|
||||
" \"\"\"\n",
|
||||
" nums = [int(n) for n in numbers] # cast all numbers as integers first\n",
|
||||
" odds = [n for n in nums if n % 2 != 0] # before filtering them\n",
|
||||
" odds = [n for n in nums if n % 2 != 0] # before filtering for odd numbers\n",
|
||||
" average = sum(odds) / len(odds)\n",
|
||||
" return average"
|
||||
]
|
||||
|
@ -2036,7 +2036,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"140620734513624"
|
||||
"139655519584648"
|
||||
]
|
||||
},
|
||||
"execution_count": 58,
|
||||
|
@ -2649,7 +2649,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"0.4028601552264015"
|
||||
"0.12717011866176486"
|
||||
]
|
||||
},
|
||||
"execution_count": 75,
|
||||
|
@ -2684,7 +2684,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"<bound method Random.choice of <random.Random object at 0x561d3c7fbba8>>"
|
||||
"<bound method Random.choice of <random.Random object at 0x562161984ba8>>"
|
||||
]
|
||||
},
|
||||
"execution_count": 76,
|
||||
|
|
1805
03_conditionals.ipynb
Normal file
1805
03_conditionals.ipynb
Normal file
File diff suppressed because it is too large
Load diff
389
03_conditionals_review_and_exercises.ipynb
Normal file
389
03_conditionals_review_and_exercises.ipynb
Normal file
|
@ -0,0 +1,389 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"\n",
|
||||
"# Chapter 3: Conditionals & Exceptions"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Content Review"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Read Chapter 3 of the book. Then work through the seven review questions."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Essay Questions "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Answer the following questions briefly with *at most* 300 characters per question!"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Q1**: What is the **singleton** design pattern? How many objects does the expression `[True, False, True, False]` generate in memory?"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
" "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Q2**: What do we mean when we talk about **truthy** and **falsy** expressions?"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
" "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Q3**: Explain how the conceptual difference between a **statement** and an **expression** relates to the difference between a **conditional statement** and a **conditional expression**."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
" "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Q4**: Why is the use of **temporary variables** encouraged in Python?"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
" "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Q5**: What does the `finally`-branch enforce in this code snippet? How can a `try` statement be useful *without* an `except`-branch?"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"```python\n",
|
||||
"try:\n",
|
||||
" print(\"Make a request to a service on the internet\")\n",
|
||||
"finally:\n",
|
||||
" print(\"This could be clean-up code\")\n",
|
||||
"```"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
" "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### True / False Questions"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Motivate your answer with *one short* sentence!"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Q6**: The objects `True`, `False`, and `None` represent the idea of \"yes\", \"no\", and \"maybe\" answers in a natural language.\n",
|
||||
"\n",
|
||||
"Hint: you also respond with a code cell."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
" "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Q7**: The `try` statement is useful for handling **syntax** errors."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
" "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Coding Exercises"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Discounting Customer Orders"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Q8.1**: Write a function `discounted_price()` that takes the positional arguments `unit_price` (of type `float`) and `quantity` (of type `int`) and implements a discount scheme for a line item in a customer order as follows:\n",
|
||||
"\n",
|
||||
"- if the unit price is over 100 dollars, grant 10% relative discount\n",
|
||||
"- if a customer orders more than 10 items, one in every five items is for free\n",
|
||||
"\n",
|
||||
"Only one of the two discounts is granted, whichever is better for the customer.\n",
|
||||
"\n",
|
||||
"The function should then return the overall price for the line item. Do not forget to round appropriately."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Q8.2**: Calculate the final price for the following line items of an order:\n",
|
||||
"- $7$ smartphones @ $99.00$ USD\n",
|
||||
"- $3$ workstations @ $999.00$ USD\n",
|
||||
"- $19$ GPUs @ $879.95$ USD\n",
|
||||
"- $14$ Raspberry Pis @ $35.00$ USD"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Q8.3**: Re-calculate the last two line items with order quantities of $20$ and $15$. What do you observe?"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
" (your observation)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Q8.4**: Looking at the `if`-`else`-logic in the function, why do you think the four example line items in **Q8.2** were chosen as they were?"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
" "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Fizz Buzz revisited"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"When you worked on the Fizz Buzz exercise in Chapter 1, you actually did not know about the `elif` and `else` keywords yet. Well, now you do."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"numbers = list(range(1, 101))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Q9**: Copy and paste your answer to **Q11.2** in Chapter 1 here and instead of three consecutive `if` statements re-write it with *one* compound `if` statement.\n",
|
||||
"\n",
|
||||
"This code will then be a lot more robust as the order of the three `if` statements cannot be screwed up."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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.3"
|
||||
},
|
||||
"toc": {
|
||||
"base_numbering": 1,
|
||||
"nav_menu": {},
|
||||
"number_sections": false,
|
||||
"sideBar": true,
|
||||
"skip_h1_title": true,
|
||||
"title_cell": "Table of Contents",
|
||||
"title_sidebar": "Contents",
|
||||
"toc_cell": false,
|
||||
"toc_position": {},
|
||||
"toc_section_display": false,
|
||||
"toc_window_display": false
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
|
@ -1,3 +1,5 @@
|
|||
**Important**: The notebooks are being added throughout the fall semester of 2019!
|
||||
|
||||
# An Introduction to Python and Programming
|
||||
|
||||
The purpose of this repository is to serve as an interactive "book" for a
|
||||
|
@ -13,8 +15,9 @@ science professionals and researchers.
|
|||
As such they can be viewed in a plain web browser:
|
||||
|
||||
- [00 - Start up](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/00_start_up.ipynb)
|
||||
- [01 - Elements of a Program](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/01_elements_of_a_program.ipynb)
|
||||
- [01 - Elements of a Program](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/01_elements.ipynb)
|
||||
- [02 - Functions & Modularization](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/02_functions.ipynb)
|
||||
- [03 - Conditionals & Exceptions](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/03_conditionals.ipynb)
|
||||
|
||||
However, it is recommended that students **install Python and Jupyter
|
||||
locally** and run the code in the notebooks on their own.
|
||||
|
|
Loading…
Reference in a new issue