15 lines
38 KiB
JSON
15 lines
38 KiB
JSON
{
|
|
"hash": "19c83181de61f7b89ae6811d8ff0da61",
|
|
"result": {
|
|
"markdown": "# Variables\n\n\n\n## Assignment\n\n\n\n\n\n\n\nThe Google calculator has a button `Ans` to refer to the answer to the previous evaluation. This is a form of memory. The last answer is stored in a specific place in memory for retrieval when `Ans` is used. In some calculators, more advanced memory features are possible. For some, it is possible to push values onto a stack of values for them to be referred to at a later time. This proves useful for complicated expressions, say, as the expression can be broken into smaller intermediate steps to be computed. These values can then be appropriately combined. This strategy is a good one, though the memory buttons can make its implementation a bit cumbersome.\n\n\nWith `Julia`, as with other programming languages, it is very easy to refer to past evaluations. This is done by *assignment* whereby a computed value stored in memory is associated with a name. The name can be used to look up the value later. Assignment does not change the value of the object being assigned, it only introduces a reference to it.\n\n\nAssignment in `Julia` is handled by the equals sign and takes the general form `variable_name = value`. For example, here we assign values to the variables `x` and `y`\n\n::: {.cell execution_count=4}\n``` {.julia .cell-code}\nx = sqrt(2)\ny = 42\n```\n\n::: {.cell-output .cell-output-display execution_count=5}\n```\n42\n```\n:::\n:::\n\n\nIn an assignment, the right hand side is always returned, so it appears nothing has happened. However, the values are there, as can be checked by typing their name\n\n::: {.cell execution_count=5}\n``` {.julia .cell-code}\nx\n```\n\n::: {.cell-output .cell-output-display execution_count=6}\n```\n1.4142135623730951\n```\n:::\n:::\n\n\nJust typing a variable name (without a trailing semicolon) causes the assigned value to be displayed.\n\n\nVariable names can be reused, as here, where we redefine `x`:\n\n::: {.cell hold='true' execution_count=6}\n``` {.julia .cell-code}\nx = 2\n```\n\n::: {.cell-output .cell-output-display execution_count=7}\n```\n2\n```\n:::\n:::\n\n\n:::{.callout-note}\n## Note\nThe `Pluto` interface for `Julia` is idiosyncratic, as variables are *reactive*. This interface allows changes to a variable `x` to propogate to all other cells referring to `x`. Consequently, the variable name can only be assigned *once* per notebook **unless** the name is in some other namespace, which can be arranged by including the assignment inside a function or a `let` block.\n\n:::\n\n`Julia` is referred to as a \"dynamic language\" which means (in most cases) that a variable can be reassigned with a value of a different type, as we did with `x` where first it was assigned to a floating point value then to an integer value. (Though we meet some cases - generic functions - where `Julia` balks at reassigning a variable if the type if different.)\n\n\nMore importantly than displaying a value, is the use of variables to build up more complicated expressions. For example, to compute\n\n\n\n$$\n\\frac{1 + 2 \\cdot 3^4}{5 - 6/7}\n$$\n\n\nwe might break it into the grouped pieces implied by the mathematical notation:\n\n::: {.cell execution_count=7}\n``` {.julia .cell-code}\ntop = 1 + 2*3^4\nbottom = 5 - 6/7\ntop/bottom\n```\n\n::: {.cell-output .cell-output-display execution_count=8}\n```\n39.34482758620689\n```\n:::\n:::\n\n\n### Examples\n\n\n##### Example\n\n\nImagine we have the following complicated expression related to the trajectory of a [projectile](http://www.researchgate.net/publication/230963032_On_the_trajectories_of_projectiles_depicted_in_early_ballistic_woodcuts) with wind resistance:\n\n\n\n$$\n\t\\left(\\frac{g}{k v_0\\cos(\\theta)} + \\tan(\\theta) \\right) t + \\frac{g}{k^2}\\ln\\left(1 - \\frac{k}{v_0\\cos(\\theta)} t \\right)\n$$\n\n\nHere $g$ is the gravitational constant $9.8$ and $v_0$, $\\theta$ and $k$ parameters, which we take to be $200$, $45$ degrees, and $1/2$ respectively. With these values, the above expression can be computed when $s=100$:\n\n::: {.cell execution_count=8}\n``` {.julia .cell-code}\ng = 9.8\nv0 = 200\ntheta = 45\nk = 1/2\nt = 100\na = v0 * cosd(theta)\n(g/(k*a) + tand(theta))* t + (g/k^2) * log(1 - (k/a)*t)\n```\n\n::: {.cell-output .cell-output-display execution_count=9}\n```\n96.75771791632161\n```\n:::\n:::\n\n\nBy defining a new variable `a` to represent a value that is repeated a few times in the expression, the last command is greatly simplified. Doing so makes it much easier to check for accuracy against the expression to compute.\n\n\n##### Example\n\n\nA common expression in mathematics is a polynomial expression, for example $-16s^2 + 32s - 12$. Translating this to `Julia` at $s =3$ we might have:\n\n::: {.cell execution_count=9}\n``` {.julia .cell-code}\ns = 3\n-16*s^2 + 32*s - 12\n```\n\n::: {.cell-output .cell-output-display execution_count=10}\n```\n-60\n```\n:::\n:::\n\n\nThis looks nearly identical to the mathematical expression, but we inserted `*` to indicate multiplication between the constant and the variable. In fact, this step is not needed as Julia allows numeric literals to have an implied multiplication:\n\n::: {.cell execution_count=10}\n``` {.julia .cell-code}\n-16s^2 + 32s - 12\n```\n\n::: {.cell-output .cell-output-display execution_count=11}\n```\n-60\n```\n:::\n:::\n\n\n## Where math and computer notations diverge\n\n\nIt is important to recognize that `=` to `Julia` is not in analogy to how $=$ is used in mathematical notation. The following `Julia` code is not an equation:\n\n::: {.cell hold='true' execution_count=11}\n``` {.julia .cell-code}\nx = 3\nx = x^2\n```\n\n::: {.cell-output .cell-output-display execution_count=12}\n```\n9\n```\n:::\n:::\n\n\nWhat happens instead? The right hand side is evaluated (`x` is squared), the result is stored and bound to the variable `x` (so that `x` will end up pointing to the new value, `9`, and not the original one, `3`); finally the value computed on the right-hand side is returned and in this case displayed, as there is no trailing semicolon to suppress the output.\n\n\nThis is completely unlike the mathematical equation $x = x^2$ which is typically solved for values of $x$ that satisfy the equation ($0$ and $1$).\n\n\n##### Example\n\n\nHaving `=` as assignment is usefully exploited when modeling sequences. For example, an application of Newton's method might end up with this expression:\n\n\n\n$$\nx_{i+1} = x_i - \\frac{x_i^2 - 2}{2x_i}\n$$\n\n\nAs a mathematical expression, for each $i$ this defines a new value for $x_{i+1}$ in terms of a known value $x_i$. This can be used to recursively generate a sequence, provided some starting point is known, such as $x_0 = 2$.\n\n\nThe above might be written instead with:\n\n::: {.cell hold='true' execution_count=12}\n``` {.julia .cell-code}\nx = 2\nx = x - (x^2 - 2) / (2x)\nx = x - (x^2 - 2) / (2x)\n```\n\n::: {.cell-output .cell-output-display execution_count=13}\n```\n1.4166666666666667\n```\n:::\n:::\n\n\nRepeating this last line will generate new values of `x` based on the previous one - no need for subscripts. This is exactly what the mathematical notation indicates is to be done.\n\n\n## Context\n\n\nThe binding of a value to a variable name happens within some context. For our simple illustrations, we are assigning values, as though they were typed at the command line. This stores the binding in the `Main` module. `Julia` looks for variables in this module when it encounters an expression and the value is substituted. Other uses, such as when variables are defined within a function, involve different contexts which may not be visible within the `Main` module.\n\n\n:::{.callout-note}\n## Note\nThe `varinfo` function will list the variables currently defined in the main workspace. There is no mechanism to delete a single variable.\n\n:::\n\n:::{.callout-warning}\n## Warning\n**Shooting oneselves in the foot.** `Julia` allows us to locally redefine variables that are built in, such as the value for `pi` or the function object assigned to `sin`. For example, this is a perfectly valid command `sin=3`. However, it will overwrite the typical value of `sin` so that `sin(3)` will be an error. At the terminal, the binding to `sin` occurs in the `Main` module. This shadows that value of `sin` bound in the `Base` module. Even if redefined in `Main`, the value in base can be used by fully qualifying the name, as in `Base.sin(pi)`. This uses the notation `module_name.variable_name` to look up a binding in a module.\n\n:::\n\n## Variable names\n\n\n`Julia` has a very wide set of possible [names](https://docs.julialang.org/en/stable/manual/variables/#Allowed-Variable-Names-1) for variables. Variables are case sensitive and their names can include many [Unicode](http://en.wikipedia.org/wiki/List_of_Unicode_characters) characters. Names must begin with a letter or an appropriate Unicode value (but not a number). There are some reserved words, such as `try` or `else` which can not be assigned to. However, many built-in names can be locally overwritten. Conventionally, variable names are lower case. For compound names, it is not unusual to see them squished together, joined with underscores, or written in camelCase.\n\n::: {.cell execution_count=13}\n``` {.julia .cell-code}\nvalue_1 = 1\na_long_winded_variable_name = 2\nsinOfX = sind(45)\n__private = 2 # a convention\n```\n\n::: {.cell-output .cell-output-display execution_count=14}\n```\n2\n```\n:::\n:::\n\n\n### Unicode names\n\n\nJulia allows variable names to use Unicode identifiers. Such names allow `julia` notation to mirror that of many mathematical texts. For example, in calculus the variable $\\epsilon$ is often used to represent some small number. We can assign to a symbol that looks like $\\epsilon$ using `Julia`'s LaTeX input mode. Typing `\\epsilon[tab]` will replace the text with the symbol within `IJulia` or the command line.\n\n::: {.cell execution_count=14}\n``` {.julia .cell-code}\nϵ = 1e-10\n```\n\n::: {.cell-output .cell-output-display execution_count=15}\n```\n1.0e-10\n```\n:::\n:::\n\n\nEntering Unicode names follows the pattern of \"backslash\" + LaTeX name + `[tab]` key. Some other ones that are useful are `\\delta[tab]`, `\\alpha[tab]`, and `\\beta[tab]`, though there are [hundreds](https://github.com/JuliaLang/julia/blob/master/stdlib/REPL/src/latex_symbols.jl) of other values defined.\n\n\nFor example, we could have defined `theta` (`\\theta[tab]`) and `v0` (`v\\_0[tab]`) using Unicode to make them match more closely the typeset math:\n\n::: {.cell execution_count=15}\n``` {.julia .cell-code}\nθ = 45; v₀ = 200\n```\n\n::: {.cell-output .cell-output-display execution_count=16}\n```\n200\n```\n:::\n:::\n\n\n:::{.callout-note}\n## Unicode\nThese notes can be presented as HTML files *or* as `Pluto` notebooks. They often use Unicode alternatives to avoid the `Pluto` requirement of a single use of assigning to a variable name in a notebook without placing the assignment in a `let` block or a function body.\n\n:::\n\n:::{.callout-note}\n## Emojis\nThere is even support for tab-completion of [emojis](https://github.com/JuliaLang/julia/blob/master/stdlib/REPL/src/emoji_symbols.jl) such as `\\\\:snowman:[tab]` or `\\\\:koala:[tab]`\n\n:::\n\n##### Example\n\n\nAs mentioned the value of $e$ is bound to the Unicode value `\\euler[tab]` and not the letter `e`, so Unicode entry is required to access this constant This isn't quite true. The `MathConstants` module defines `e`, as well as a few other values accessed via Unicode. When the `CalculusWithJulia` package is loaded, as will often be done in these notes, a value of `exp(1)` is assigned to `e`.\n\n\n## Tuple assignment\n\n\nIt is a common task to define more than one variable. Multiple definitions can be done in one line, using semicolons to break up the commands, as with:\n\n::: {.cell hold='true' execution_count=16}\n``` {.julia .cell-code}\na = 1; b = 2; c=3\n```\n\n::: {.cell-output .cell-output-display execution_count=17}\n```\n3\n```\n:::\n:::\n\n\nFor convenience, `Julia` allows an alternate means to define more than one variable at a time. The syntax is similar:\n\n::: {.cell hold='true' execution_count=17}\n``` {.julia .cell-code}\na, b, c = 1, 2, 3\nb\n```\n\n::: {.cell-output .cell-output-display execution_count=18}\n```\n2\n```\n:::\n:::\n\n\nThis sets `a=1`, `b=2`, and `c=3`, as suggested. This construct relies on *tuple destructuring*. The expression on the right hand side forms a tuple of values. A tuple is a container for different types of values, and in this case the tuple has 3 values. When the same number of variables match on the left-hand side as those in the container on the right, the names are assigned one by one.\n\n\nThe value on the right hand side is evaluated, then the assignment occurs. The following exploits this to swap the values assigned to `a` and `b`:\n\n::: {.cell hold='true' execution_count=18}\n``` {.julia .cell-code}\na, b = 1, 2\na, b = b, a\n```\n\n::: {.cell-output .cell-output-display execution_count=19}\n```\n(2, 1)\n```\n:::\n:::\n\n\n#### Example, finding the slope\n\n\nFind the slope of the line connecting the points $(1,2)$ and $(4,6)$. We begin by defining the values and then applying the slope formula:\n\n::: {.cell execution_count=19}\n``` {.julia .cell-code}\nx0, y0 = 1, 2\nx1, y1 = 4, 6\nm = (y1 - y0) / (x1 - x0)\n```\n\n::: {.cell-output .cell-output-display execution_count=20}\n```\n1.3333333333333333\n```\n:::\n:::\n\n\nOf course, this could be computed directly with `(6-2) / (4-1)`, but by using familiar names for the values we can be certain we apply the formula properly.\n\n\n## Questions\n\n\n###### Question\n\n\nLet $a=10$, $b=2.3$, and $c=8$. Find the value of $(a-b)/(a-c)$.\n\n::: {.cell hold='true' execution_count=20}\n\n::: {.cell-output .cell-output-display execution_count=21}\n```{=html}\n<form class=\"mx-2 my-3 mw-100\" name='WeaveQuestion' data-id='8775939163778976083' data-controltype=''>\n <div class='form-group '>\n <div class='controls'>\n <div class=\"form\" id=\"controls_8775939163778976083\">\n <div style=\"padding-top: 5px\">\n </br>\n<div class=\"input-group\">\n <input id=\"8775939163778976083\" type=\"number\" class=\"form-control\" placeholder=\"Numeric answer\">\n</div>\n\n \n </div>\n </div>\n <div id='8775939163778976083_message' style=\"padding-bottom: 15px\"></div>\n </div>\n </div>\n</form>\n\n<script text='text/javascript'>\ndocument.getElementById(\"8775939163778976083\").addEventListener(\"change\", function() {\n var correct = (Math.abs(this.value - 3.85) <= 0.001);\n var msgBox = document.getElementById('8775939163778976083_message');\n if(correct) {\n msgBox.innerHTML = \"<div class='pluto-output admonition note alert alert-success'><span> 👍 Correct </span></div>\";\n var explanation = document.getElementById(\"explanation_8775939163778976083\")\n if (explanation != null) {\n explanation.style.display = \"none\";\n }\n } else {\n msgBox.innerHTML = \"<div class='pluto-output admonition alert alert-danger'><span>👎 Incorrect </span></div>\";\n var explanation = document.getElementById(\"explanation_8775939163778976083\")\n if (explanation != null) {\n explanation.style.display = \"block\";\n }\n }\n\n});\n\n</script>\n```\n:::\n:::\n\n\n###### Question\n\n\nLet `x = 4`. Compute $y=100 - 2x - x^2$. What is the value:\n\n::: {.cell hold='true' execution_count=21}\n\n::: {.cell-output .cell-output-display execution_count=22}\n```{=html}\n<form class=\"mx-2 my-3 mw-100\" name='WeaveQuestion' data-id='16516910521083168700' data-controltype=''>\n <div class='form-group '>\n <div class='controls'>\n <div class=\"form\" id=\"controls_16516910521083168700\">\n <div style=\"padding-top: 5px\">\n </br>\n<div class=\"input-group\">\n <input id=\"16516910521083168700\" type=\"number\" class=\"form-control\" placeholder=\"Numeric answer\">\n</div>\n\n \n </div>\n </div>\n <div id='16516910521083168700_message' style=\"padding-bottom: 15px\"></div>\n </div>\n </div>\n</form>\n\n<script text='text/javascript'>\ndocument.getElementById(\"16516910521083168700\").addEventListener(\"change\", function() {\n var correct = (Math.abs(this.value - 76) <= 0.1);\n var msgBox = document.getElementById('16516910521083168700_message');\n if(correct) {\n msgBox.innerHTML = \"<div class='pluto-output admonition note alert alert-success'><span> 👍 Correct </span></div>\";\n var explanation = document.getElementById(\"explanation_16516910521083168700\")\n if (explanation != null) {\n explanation.style.display = \"none\";\n }\n } else {\n msgBox.innerHTML = \"<div class='pluto-output admonition alert alert-danger'><span>👎 Incorrect </span></div>\";\n var explanation = document.getElementById(\"explanation_16516910521083168700\")\n if (explanation != null) {\n explanation.style.display = \"block\";\n }\n }\n\n});\n\n</script>\n```\n:::\n:::\n\n\n###### Question\n\n\nWhat is the answer to this computation?\n\n``` {.julia .cell-code}\na = 3.2; b=2.3\na^b - b^a\n```\n\n\n::: {.cell hold='true' execution_count=23}\n\n::: {.cell-output .cell-output-display execution_count=23}\n```{=html}\n<form class=\"mx-2 my-3 mw-100\" name='WeaveQuestion' data-id='1110304090240401111' data-controltype=''>\n <div class='form-group '>\n <div class='controls'>\n <div class=\"form\" id=\"controls_1110304090240401111\">\n <div style=\"padding-top: 5px\">\n </br>\n<div class=\"input-group\">\n <input id=\"1110304090240401111\" type=\"number\" class=\"form-control\" placeholder=\"Numeric answer\">\n</div>\n\n \n </div>\n </div>\n <div id='1110304090240401111_message' style=\"padding-bottom: 15px\"></div>\n </div>\n </div>\n</form>\n\n<script text='text/javascript'>\ndocument.getElementById(\"1110304090240401111\").addEventListener(\"change\", function() {\n var correct = (Math.abs(this.value - 0.14354012963861962) <= 0.001);\n var msgBox = document.getElementById('1110304090240401111_message');\n if(correct) {\n msgBox.innerHTML = \"<div class='pluto-output admonition note alert alert-success'><span> 👍 Correct </span></div>\";\n var explanation = document.getElementById(\"explanation_1110304090240401111\")\n if (explanation != null) {\n explanation.style.display = \"none\";\n }\n } else {\n msgBox.innerHTML = \"<div class='pluto-output admonition alert alert-danger'><span>👎 Incorrect </span></div>\";\n var explanation = document.getElementById(\"explanation_1110304090240401111\")\n if (explanation != null) {\n explanation.style.display = \"block\";\n }\n }\n\n});\n\n</script>\n```\n:::\n:::\n\n\n###### Question\n\n\nFor longer computations, it can be convenient to do them in parts, as this makes it easier to check for mistakes.\n\n\nFor example, to compute\n\n\n\n$$\n\\frac{p - q}{\\sqrt{p(1-p)}}\n$$\n\n\nfor $p=0.25$ and $q=0.2$ we might do:\n\n``` {.julia .cell-code}\np, q = 0.25, 0.2\ntop = p - q\nbottom = sqrt(p*(1-p))\nans = top/bottom\n```\n\n\nWhat is the result of the above?\n\n::: {.cell hold='true' execution_count=25}\n\n::: {.cell-output .cell-output-display execution_count=24}\n```{=html}\n<form class=\"mx-2 my-3 mw-100\" name='WeaveQuestion' data-id='16633353597819953222' data-controltype=''>\n <div class='form-group '>\n <div class='controls'>\n <div class=\"form\" id=\"controls_16633353597819953222\">\n <div style=\"padding-top: 5px\">\n </br>\n<div class=\"input-group\">\n <input id=\"16633353597819953222\" type=\"number\" class=\"form-control\" placeholder=\"Numeric answer\">\n</div>\n\n \n </div>\n </div>\n <div id='16633353597819953222_message' style=\"padding-bottom: 15px\"></div>\n </div>\n </div>\n</form>\n\n<script text='text/javascript'>\ndocument.getElementById(\"16633353597819953222\").addEventListener(\"change\", function() {\n var correct = (Math.abs(this.value - 0.11547005383792514) <= 0.001);\n var msgBox = document.getElementById('16633353597819953222_message');\n if(correct) {\n msgBox.innerHTML = \"<div class='pluto-output admonition note alert alert-success'><span> 👍 Correct </span></div>\";\n var explanation = document.getElementById(\"explanation_16633353597819953222\")\n if (explanation != null) {\n explanation.style.display = \"none\";\n }\n } else {\n msgBox.innerHTML = \"<div class='pluto-output admonition alert alert-danger'><span>👎 Incorrect </span></div>\";\n var explanation = document.getElementById(\"explanation_16633353597819953222\")\n if (explanation != null) {\n explanation.style.display = \"block\";\n }\n }\n\n});\n\n</script>\n```\n:::\n:::\n\n\n###### Question\n\n\nUsing variables to record the top and the bottom of the expression, compute the following for $x=3$:\n\n\n\n$$\ny = \\frac{x^2 - 2x - 8}{x^2 - 9x - 20}.\n$$\n\n::: {.cell hold='true' execution_count=26}\n\n::: {.cell-output .cell-output-display execution_count=25}\n```{=html}\n<form class=\"mx-2 my-3 mw-100\" name='WeaveQuestion' data-id='3661688548716858201' data-controltype=''>\n <div class='form-group '>\n <div class='controls'>\n <div class=\"form\" id=\"controls_3661688548716858201\">\n <div style=\"padding-top: 5px\">\n </br>\n<div class=\"input-group\">\n <input id=\"3661688548716858201\" type=\"number\" class=\"form-control\" placeholder=\"Numeric answer\">\n</div>\n\n \n </div>\n </div>\n <div id='3661688548716858201_message' style=\"padding-bottom: 15px\"></div>\n </div>\n </div>\n</form>\n\n<script text='text/javascript'>\ndocument.getElementById(\"3661688548716858201\").addEventListener(\"change\", function() {\n var correct = (Math.abs(this.value - 0.13157894736842105) <= 0.001);\n var msgBox = document.getElementById('3661688548716858201_message');\n if(correct) {\n msgBox.innerHTML = \"<div class='pluto-output admonition note alert alert-success'><span> 👍 Correct </span></div>\";\n var explanation = document.getElementById(\"explanation_3661688548716858201\")\n if (explanation != null) {\n explanation.style.display = \"none\";\n }\n } else {\n msgBox.innerHTML = \"<div class='pluto-output admonition alert alert-danger'><span>👎 Incorrect </span></div>\";\n var explanation = document.getElementById(\"explanation_3661688548716858201\")\n if (explanation != null) {\n explanation.style.display = \"block\";\n }\n }\n\n});\n\n</script>\n```\n:::\n:::\n\n\n###### Question\n\n\nWhich if these is not a valid variable name (identifier) in `Julia`:\n\n::: {.cell hold='true' execution_count=27}\n\n::: {.cell-output .cell-output-display execution_count=26}\n```{=html}\n<form class=\"mx-2 my-3 mw-100\" name='WeaveQuestion' data-id='15003308656791275566' data-controltype=''>\n <div class='form-group '>\n <div class='controls'>\n <div class=\"form\" id=\"controls_15003308656791275566\">\n <div style=\"padding-top: 5px\">\n <div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_15003308656791275566_1\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_15003308656791275566\"\n id=\"radio_15003308656791275566_1\" value=\"1\">\n </input>\n <span class=\"label-body px-1\">\n <code>aMiXeDcAsEnAmE</code>\n </span>\n </label>\n</div>\n<div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_15003308656791275566_2\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_15003308656791275566\"\n id=\"radio_15003308656791275566_2\" value=\"2\">\n </input>\n <span class=\"label-body px-1\">\n <code>5degreesbelowzero</code>\n </span>\n </label>\n</div>\n<div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_15003308656791275566_3\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_15003308656791275566\"\n id=\"radio_15003308656791275566_3\" value=\"3\">\n </input>\n <span class=\"label-body px-1\">\n <code>some_really_long_name_that_is_no_fun_to_type</code>\n </span>\n </label>\n</div>\n<div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_15003308656791275566_4\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_15003308656791275566\"\n id=\"radio_15003308656791275566_4\" value=\"4\">\n </input>\n <span class=\"label-body px-1\">\n <code>fahrenheit451</code>\n </span>\n </label>\n</div>\n\n \n </div>\n </div>\n <div id='15003308656791275566_message' style=\"padding-bottom: 15px\"></div>\n </div>\n </div>\n</form>\n\n<script text='text/javascript'>\ndocument.querySelectorAll('input[name=\"radio_15003308656791275566\"]').forEach(function(rb) {\nrb.addEventListener(\"change\", function() {\n var correct = rb.value == 2;\n var msgBox = document.getElementById('15003308656791275566_message');\n if(correct) {\n msgBox.innerHTML = \"<div class='pluto-output admonition note alert alert-success'><span> 👍 Correct </span></div>\";\n var explanation = document.getElementById(\"explanation_15003308656791275566\")\n if (explanation != null) {\n explanation.style.display = \"none\";\n }\n } else {\n msgBox.innerHTML = \"<div class='pluto-output admonition alert alert-danger'><span>👎 Incorrect </span></div>\";\n var explanation = document.getElementById(\"explanation_15003308656791275566\")\n if (explanation != null) {\n explanation.style.display = \"block\";\n }\n }\n\n})});\n\n</script>\n```\n:::\n:::\n\n\n###### Question\n\n\nWhich of these symbols is one of `Julia`'s built-in math constants?\n\n::: {.cell hold='true' execution_count=28}\n\n::: {.cell-output .cell-output-display execution_count=27}\n```{=html}\n<form class=\"mx-2 my-3 mw-100\" name='WeaveQuestion' data-id='4972916273403194559' data-controltype=''>\n <div class='form-group '>\n <div class='controls'>\n <div class=\"form\" id=\"controls_4972916273403194559\">\n <div style=\"padding-top: 5px\">\n <div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_4972916273403194559_1\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_4972916273403194559\"\n id=\"radio_4972916273403194559_1\" value=\"1\">\n </input>\n <span class=\"label-body px-1\">\n <code>oo</code>\n </span>\n </label>\n</div>\n<div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_4972916273403194559_2\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_4972916273403194559\"\n id=\"radio_4972916273403194559_2\" value=\"2\">\n </input>\n <span class=\"label-body px-1\">\n <code>pi</code>\n </span>\n </label>\n</div>\n<div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_4972916273403194559_3\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_4972916273403194559\"\n id=\"radio_4972916273403194559_3\" value=\"3\">\n </input>\n <span class=\"label-body px-1\">\n <code>E</code>\n </span>\n </label>\n</div>\n<div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_4972916273403194559_4\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_4972916273403194559\"\n id=\"radio_4972916273403194559_4\" value=\"4\">\n </input>\n <span class=\"label-body px-1\">\n <code>I</code>\n </span>\n </label>\n</div>\n\n \n </div>\n </div>\n <div id='4972916273403194559_message' style=\"padding-bottom: 15px\"></div>\n </div>\n </div>\n</form>\n\n<script text='text/javascript'>\ndocument.querySelectorAll('input[name=\"radio_4972916273403194559\"]').forEach(function(rb) {\nrb.addEventListener(\"change\", function() {\n var correct = rb.value == 2;\n var msgBox = document.getElementById('4972916273403194559_message');\n if(correct) {\n msgBox.innerHTML = \"<div class='pluto-output admonition note alert alert-success'><span> 👍 Correct </span></div>\";\n var explanation = document.getElementById(\"explanation_4972916273403194559\")\n if (explanation != null) {\n explanation.style.display = \"none\";\n }\n } else {\n msgBox.innerHTML = \"<div class='pluto-output admonition alert alert-danger'><span>👎 Incorrect </span></div>\";\n var explanation = document.getElementById(\"explanation_4972916273403194559\")\n if (explanation != null) {\n explanation.style.display = \"block\";\n }\n }\n\n})});\n\n</script>\n```\n:::\n:::\n\n\n###### Question\n\n\nWhat key sequence will produce this assignment\n\n``` {.julia .cell-code}\nδ = 1/10\n```\n\n\n::: {.cell hold='true' execution_count=30}\n\n::: {.cell-output .cell-output-display execution_count=28}\n```{=html}\n<form class=\"mx-2 my-3 mw-100\" name='WeaveQuestion' data-id='17768021191373452920' data-controltype=''>\n <div class='form-group '>\n <div class='controls'>\n <div class=\"form\" id=\"controls_17768021191373452920\">\n <div style=\"padding-top: 5px\">\n <div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_17768021191373452920_1\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_17768021191373452920\"\n id=\"radio_17768021191373452920_1\" value=\"1\">\n </input>\n <span class=\"label-body px-1\">\n <code>delta[tab] = 1/10</code>\n </span>\n </label>\n</div>\n<div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_17768021191373452920_2\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_17768021191373452920\"\n id=\"radio_17768021191373452920_2\" value=\"2\">\n </input>\n <span class=\"label-body px-1\">\n <code>$\\\\delta$ = 1/10</code>\n </span>\n </label>\n</div>\n<div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_17768021191373452920_3\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_17768021191373452920\"\n id=\"radio_17768021191373452920_3\" value=\"3\">\n </input>\n <span class=\"label-body px-1\">\n <code>\\delta[tab] = 1/10</code>\n </span>\n </label>\n</div>\n\n \n </div>\n </div>\n <div id='17768021191373452920_message' style=\"padding-bottom: 15px\"></div>\n </div>\n </div>\n</form>\n\n<script text='text/javascript'>\ndocument.querySelectorAll('input[name=\"radio_17768021191373452920\"]').forEach(function(rb) {\nrb.addEventListener(\"change\", function() {\n var correct = rb.value == 3;\n var msgBox = document.getElementById('17768021191373452920_message');\n if(correct) {\n msgBox.innerHTML = \"<div class='pluto-output admonition note alert alert-success'><span> 👍 Correct </span></div>\";\n var explanation = document.getElementById(\"explanation_17768021191373452920\")\n if (explanation != null) {\n explanation.style.display = \"none\";\n }\n } else {\n msgBox.innerHTML = \"<div class='pluto-output admonition alert alert-danger'><span>👎 Incorrect </span></div>\";\n var explanation = document.getElementById(\"explanation_17768021191373452920\")\n if (explanation != null) {\n explanation.style.display = \"block\";\n }\n }\n\n})});\n\n</script>\n```\n:::\n:::\n\n\n###### Question\n\n\nWhich of these three statements will **not** be a valid way to assign three variables at once:\n\n::: {.cell hold='true' execution_count=31}\n\n::: {.cell-output .cell-output-display execution_count=29}\n```{=html}\n<form class=\"mx-2 my-3 mw-100\" name='WeaveQuestion' data-id='17376850340495187203' data-controltype=''>\n <div class='form-group '>\n <div class='controls'>\n <div class=\"form\" id=\"controls_17376850340495187203\">\n <div style=\"padding-top: 5px\">\n <div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_17376850340495187203_1\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_17376850340495187203\"\n id=\"radio_17376850340495187203_1\" value=\"1\">\n </input>\n <span class=\"label-body px-1\">\n <code>a=1; b=2; c=3</code>\n </span>\n </label>\n</div>\n<div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_17376850340495187203_2\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_17376850340495187203\"\n id=\"radio_17376850340495187203_2\" value=\"2\">\n </input>\n <span class=\"label-body px-1\">\n <code>a,b,c = 1,2,3</code>\n </span>\n </label>\n</div>\n<div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_17376850340495187203_3\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_17376850340495187203\"\n id=\"radio_17376850340495187203_3\" value=\"3\">\n </input>\n <span class=\"label-body px-1\">\n <code>a=1, b=2, c=3</code>\n </span>\n </label>\n</div>\n\n \n </div>\n </div>\n <div id='17376850340495187203_message' style=\"padding-bottom: 15px\"></div>\n </div>\n </div>\n</form>\n\n<script text='text/javascript'>\ndocument.querySelectorAll('input[name=\"radio_17376850340495187203\"]').forEach(function(rb) {\nrb.addEventListener(\"change\", function() {\n var correct = rb.value == 3;\n var msgBox = document.getElementById('17376850340495187203_message');\n if(correct) {\n msgBox.innerHTML = \"<div class='pluto-output admonition note alert alert-success'><span> 👍 Correct </span></div>\";\n var explanation = document.getElementById(\"explanation_17376850340495187203\")\n if (explanation != null) {\n explanation.style.display = \"none\";\n }\n } else {\n msgBox.innerHTML = \"<div class='pluto-output admonition alert alert-danger'><span>👎 Incorrect </span></div>\";\n var explanation = document.getElementById(\"explanation_17376850340495187203\")\n if (explanation != null) {\n explanation.style.display = \"block\";\n }\n }\n\n})});\n\n</script>\n```\n:::\n:::\n\n\n###### Question\n\n\nThe fact that assignment *always* returns the value of the right hand side *and* the fact that the `=` sign associates from right to left means that the following idiom:\n\n``` {.julia .cell-code}\nx = y = z = 3\n```\n\n\nWill always:\n\n::: {.cell hold='true' execution_count=33}\n\n::: {.cell-output .cell-output-display execution_count=30}\n```{=html}\n<form class=\"mx-2 my-3 mw-100\" name='WeaveQuestion' data-id='2530070119205053241' data-controltype=''>\n <div class='form-group '>\n <div class='controls'>\n <div class=\"form\" id=\"controls_2530070119205053241\">\n <div style=\"padding-top: 5px\">\n <div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_2530070119205053241_1\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_2530070119205053241\"\n id=\"radio_2530070119205053241_1\" value=\"1\">\n </input>\n <span class=\"label-body px-1\">\n Throw an error\n </span>\n </label>\n</div>\n<div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_2530070119205053241_2\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_2530070119205053241\"\n id=\"radio_2530070119205053241_2\" value=\"2\">\n </input>\n <span class=\"label-body px-1\">\n Assign all three variables at once to a value of <code>3</code>\n </span>\n </label>\n</div>\n<div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_2530070119205053241_3\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_2530070119205053241\"\n id=\"radio_2530070119205053241_3\" value=\"3\">\n </input>\n <span class=\"label-body px-1\">\n Create \\(3\\) linked values that will stay synced when any value changes\n </span>\n </label>\n</div>\n\n \n </div>\n </div>\n <div id='2530070119205053241_message' style=\"padding-bottom: 15px\"></div>\n </div>\n </div>\n</form>\n\n<script text='text/javascript'>\ndocument.querySelectorAll('input[name=\"radio_2530070119205053241\"]').forEach(function(rb) {\nrb.addEventListener(\"change\", function() {\n var correct = rb.value == 2;\n var msgBox = document.getElementById('2530070119205053241_message');\n if(correct) {\n msgBox.innerHTML = \"<div class='pluto-output admonition note alert alert-success'><span> 👍 Correct </span></div>\";\n var explanation = document.getElementById(\"explanation_2530070119205053241\")\n if (explanation != null) {\n explanation.style.display = \"none\";\n }\n } else {\n msgBox.innerHTML = \"<div class='pluto-output admonition alert alert-danger'><span>👎 Incorrect </span></div>\";\n var explanation = document.getElementById(\"explanation_2530070119205053241\")\n if (explanation != null) {\n explanation.style.display = \"block\";\n }\n }\n\n})});\n\n</script>\n```\n:::\n:::\n\n\n",
|
|
"supporting": [
|
|
"variables_files"
|
|
],
|
|
"filters": [],
|
|
"includes": {
|
|
"include-in-header": [
|
|
"<script src=\"https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js\" integrity=\"sha512-c3Nl8+7g4LMSTdrm621y7kf9v3SDPnhxLNhcjFJbKECVnmZHTdo+IRO05sNLTH/D3vA6u1X32ehoLC7WFVdheg==\" crossorigin=\"anonymous\"></script>\n<script src=\"https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js\" integrity=\"sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg==\" crossorigin=\"anonymous\"></script>\n<script type=\"application/javascript\">define('jquery', [],function() {return window.jQuery;})</script>\n"
|
|
]
|
|
}
|
|
}
|
|
} |