Skip to content

Instantly share code, notes, and snippets.

@mxeliezer
Last active December 29, 2020 14:45
Show Gist options
  • Save mxeliezer/c97f5f1a0d95bc2ef1d4808488577ec9 to your computer and use it in GitHub Desktop.
Save mxeliezer/c97f5f1a0d95bc2ef1d4808488577ec9 to your computer and use it in GitHub Desktop.
01. Getting started_Julia_lang.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"source": [
"# Getting started with Julia\n",
"\n",
"Topics:\n",
"1. How to print\n",
"2. How to assign variables\n",
"3. How to comment\n",
"4. Syntax for basic math"
],
"metadata": {}
},
{
"cell_type": "markdown",
"source": [
"## How to print\n",
"\n",
"In Julia we usually use `println()` to print"
],
"metadata": {}
},
{
"cell_type": "code",
"source": [
"println(\"I'm excited to learn Julia!\")"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"I'm excited to learn Julia!\n"
]
}
],
"execution_count": 2,
"metadata": {
"execution": {
"iopub.status.busy": "2020-12-27T20:50:06.061Z",
"iopub.execute_input": "2020-12-27T20:50:06.337Z",
"iopub.status.idle": "2020-12-27T20:50:06.619Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"## How to assign variables\n",
"\n",
"All we need is a variable name, value, and an equal's sign!<br>\n",
"Julia will figure out types for us."
],
"metadata": {}
},
{
"cell_type": "code",
"source": [
"my_answer = 42\n",
"typeof(my_answer)"
],
"outputs": [
{
"output_type": "execute_result",
"execution_count": 3,
"data": {
"text/plain": "Int64"
},
"metadata": {}
}
],
"execution_count": 3,
"metadata": {
"execution": {
"iopub.status.busy": "2020-12-27T20:50:39.465Z",
"iopub.execute_input": "2020-12-27T20:50:39.735Z",
"iopub.status.idle": "2020-12-27T20:50:45.242Z"
}
}
},
{
"cell_type": "code",
"source": [
"my_pi = 3.14159\n",
"typeof(my_pi)"
],
"outputs": [
{
"output_type": "execute_result",
"execution_count": 4,
"data": {
"text/plain": "Float64"
},
"metadata": {}
}
],
"execution_count": 4,
"metadata": {
"execution": {
"iopub.status.busy": "2020-12-27T20:50:57.666Z",
"iopub.execute_input": "2020-12-27T20:50:57.749Z",
"iopub.status.idle": "2020-12-27T20:50:58.122Z"
}
}
},
{
"cell_type": "code",
"source": [
"😺 = \"smiley cat!\"\n",
"typeof(😺)"
],
"outputs": [
{
"output_type": "execute_result",
"execution_count": 5,
"data": {
"text/plain": "String"
},
"metadata": {}
}
],
"execution_count": 5,
"metadata": {
"execution": {
"iopub.status.busy": "2020-12-27T20:51:02.469Z",
"iopub.execute_input": "2020-12-27T20:51:02.560Z",
"iopub.status.idle": "2020-12-27T20:51:02.699Z"
}
}
},
{
"cell_type": "code",
"source": [
"my_name=\"Magno\"\n",
"typeof(my_name)"
],
"outputs": [
{
"output_type": "execute_result",
"execution_count": 7,
"data": {
"text/plain": "String"
},
"metadata": {}
}
],
"execution_count": 7,
"metadata": {
"collapsed": true,
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"execution": {
"iopub.status.busy": "2020-12-27T20:58:53.308Z",
"iopub.execute_input": "2020-12-27T20:58:53.374Z",
"iopub.status.idle": "2020-12-27T20:58:53.737Z"
}
}
},
{
"cell_type": "code",
"source": [
"my_answer=my_name\n",
"typeof(my_answer)"
],
"outputs": [
{
"output_type": "execute_result",
"execution_count": 12,
"data": {
"text/plain": "String"
},
"metadata": {}
}
],
"execution_count": 12,
"metadata": {
"collapsed": true,
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"execution": {
"iopub.status.busy": "2020-12-27T21:02:33.485Z",
"iopub.execute_input": "2020-12-27T21:02:33.544Z",
"iopub.status.idle": "2020-12-27T21:02:33.671Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"To type a smiley cat, use tab completion to select the emoji name and then tab again"
],
"metadata": {}
},
{
"cell_type": "code",
"source": [
"# \\:smi + <tab> --> select with down arrow + <enter> ---> <tab> + <enter> to complete"
],
"outputs": [],
"execution_count": 6,
"metadata": {
"execution": {
"iopub.status.busy": "2020-12-27T20:51:27.934Z",
"iopub.execute_input": "2020-12-27T20:51:28.012Z",
"iopub.status.idle": "2020-12-27T20:51:28.139Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"After assigning a value to a variable, we can reassign a value of a different type to that variable without any issue."
],
"metadata": {}
},
{
"cell_type": "code",
"source": [
"😺 = 1"
],
"outputs": [
{
"output_type": "execute_result",
"execution_count": 8,
"data": {
"text/plain": "1"
},
"metadata": {}
}
],
"execution_count": 8,
"metadata": {
"execution": {
"iopub.status.busy": "2020-12-27T20:59:43.141Z",
"iopub.execute_input": "2020-12-27T20:59:43.216Z",
"iopub.status.idle": "2020-12-27T20:59:46.506Z"
}
}
},
{
"cell_type": "code",
"source": [
"typeof(😺)"
],
"outputs": [
{
"output_type": "execute_result",
"execution_count": 9,
"data": {
"text/plain": "Int64"
},
"metadata": {}
}
],
"execution_count": 9,
"metadata": {
"execution": {
"iopub.status.busy": "2020-12-27T20:59:48.572Z",
"iopub.execute_input": "2020-12-27T20:59:48.662Z",
"iopub.status.idle": "2020-12-27T20:59:48.810Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"Note: Julia allows us to write super generic code, and 😺 is an example of this. \n",
"\n",
"This allows us to write code like"
],
"metadata": {}
},
{
"cell_type": "code",
"source": [
"😀 = 0\n",
"😞 = -1"
],
"outputs": [
{
"output_type": "execute_result",
"execution_count": 10,
"data": {
"text/plain": "-1"
},
"metadata": {}
}
],
"execution_count": 10,
"metadata": {
"execution": {
"iopub.status.busy": "2020-12-27T21:00:20.885Z",
"iopub.execute_input": "2020-12-27T21:00:20.959Z",
"iopub.status.idle": "2020-12-27T21:00:21.104Z"
}
}
},
{
"cell_type": "code",
"source": [
"😺 + 😞 == 😀"
],
"outputs": [
{
"output_type": "execute_result",
"execution_count": 11,
"data": {
"text/plain": "true"
},
"metadata": {}
}
],
"execution_count": 11,
"metadata": {
"execution": {
"iopub.status.busy": "2020-12-27T21:00:29.648Z",
"iopub.execute_input": "2020-12-27T21:00:29.710Z",
"iopub.status.idle": "2020-12-27T21:00:31.045Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"## How to comment"
],
"metadata": {}
},
{
"cell_type": "code",
"source": [
"# You can leave comments on a single line using the pound/hash key"
],
"outputs": [],
"execution_count": 13,
"metadata": {
"execution": {
"iopub.status.busy": "2020-12-27T21:03:01.253Z",
"iopub.execute_input": "2020-12-27T21:03:01.341Z",
"iopub.status.idle": "2020-12-27T21:03:01.460Z"
}
}
},
{
"cell_type": "code",
"source": [
"#=\n",
"\n",
"For multi-line comments, \n",
"use the '#= =#' sequence.\n",
"\n",
"=#"
],
"outputs": [],
"execution_count": 14,
"metadata": {
"execution": {
"iopub.status.busy": "2020-12-27T21:03:04.538Z",
"iopub.execute_input": "2020-12-27T21:03:04.605Z",
"iopub.status.idle": "2020-12-27T21:03:04.703Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"## Syntax for basic math"
],
"metadata": {}
},
{
"cell_type": "code",
"source": [
"sum = 3 + 7"
],
"outputs": [
{
"output_type": "execute_result",
"execution_count": 16,
"data": {
"text/plain": "10"
},
"metadata": {}
}
],
"execution_count": 16,
"metadata": {
"execution": {
"iopub.status.busy": "2020-12-27T21:06:34.993Z",
"iopub.execute_input": "2020-12-27T21:06:35.082Z",
"iopub.status.idle": "2020-12-27T21:06:35.217Z"
}
}
},
{
"cell_type": "code",
"source": [
"difference = 10 - 3"
],
"outputs": [
{
"output_type": "execute_result",
"execution_count": 17,
"data": {
"text/plain": "7"
},
"metadata": {}
}
],
"execution_count": 17,
"metadata": {
"execution": {
"iopub.status.busy": "2020-12-27T21:06:36.926Z",
"iopub.execute_input": "2020-12-27T21:06:37.026Z",
"iopub.status.idle": "2020-12-27T21:06:37.177Z"
}
}
},
{
"cell_type": "code",
"source": [
"product = 20 * 5"
],
"outputs": [
{
"output_type": "execute_result",
"execution_count": 18,
"data": {
"text/plain": "100"
},
"metadata": {}
}
],
"execution_count": 18,
"metadata": {
"execution": {
"iopub.status.busy": "2020-12-27T21:06:39.367Z",
"iopub.execute_input": "2020-12-27T21:06:39.445Z",
"iopub.status.idle": "2020-12-27T21:06:39.634Z"
}
}
},
{
"cell_type": "code",
"source": [
"quotient = 100 / 10"
],
"outputs": [
{
"output_type": "execute_result",
"execution_count": 19,
"data": {
"text/plain": "10.0"
},
"metadata": {}
}
],
"execution_count": 19,
"metadata": {
"execution": {
"iopub.status.busy": "2020-12-27T21:06:41.635Z",
"iopub.execute_input": "2020-12-27T21:06:41.777Z",
"iopub.status.idle": "2020-12-27T21:06:43.626Z"
}
}
},
{
"cell_type": "code",
"source": [
"power = 10 ^ 2"
],
"outputs": [
{
"output_type": "execute_result",
"execution_count": 20,
"data": {
"text/plain": "100"
},
"metadata": {}
}
],
"execution_count": 20,
"metadata": {
"execution": {
"iopub.status.busy": "2020-12-27T21:06:43.728Z",
"iopub.execute_input": "2020-12-27T21:06:43.789Z",
"iopub.status.idle": "2020-12-27T21:06:43.972Z"
}
}
},
{
"cell_type": "code",
"source": [
"modulus = 101 % 2"
],
"outputs": [
{
"output_type": "execute_result",
"execution_count": 21,
"data": {
"text/plain": "1"
},
"metadata": {}
}
],
"execution_count": 21,
"metadata": {
"execution": {
"iopub.status.busy": "2020-12-27T21:07:32.156Z",
"iopub.execute_input": "2020-12-27T21:07:32.220Z",
"iopub.status.idle": "2020-12-27T21:07:34.681Z"
}
}
},
{
"cell_type": "code",
"source": [
"?modulus\n"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"search: \u001b[0m\u001b[1mm\u001b[22m\u001b[0m\u001b[1mo\u001b[22m\u001b[0m\u001b[1md\u001b[22m\u001b[0m\u001b[1mu\u001b[22m\u001b[0m\u001b[1ml\u001b[22m\u001b[0m\u001b[1mu\u001b[22m\u001b[0m\u001b[1ms\u001b[22m \u001b[0m\u001b[1mm\u001b[22m\u001b[0m\u001b[1mo\u001b[22m\u001b[0m\u001b[1md\u001b[22m\u001b[0m\u001b[1mu\u001b[22m\u001b[0m\u001b[1ml\u001b[22me \u001b[0m\u001b[1mM\u001b[22m\u001b[0m\u001b[1mo\u001b[22m\u001b[0m\u001b[1md\u001b[22m\u001b[0m\u001b[1mu\u001b[22m\u001b[0m\u001b[1ml\u001b[22me @__\u001b[0m\u001b[1mM\u001b[22m\u001b[0m\u001b[1mO\u001b[22m\u001b[0m\u001b[1mD\u001b[22m\u001b[0m\u001b[1mU\u001b[22m\u001b[0m\u001b[1mL\u001b[22mE__ bare\u001b[0m\u001b[1mm\u001b[22m\u001b[0m\u001b[1mo\u001b[22m\u001b[0m\u001b[1md\u001b[22m\u001b[0m\u001b[1mu\u001b[22m\u001b[0m\u001b[1ml\u001b[22me parent\u001b[0m\u001b[1mm\u001b[22m\u001b[0m\u001b[1mo\u001b[22m\u001b[0m\u001b[1md\u001b[22m\u001b[0m\u001b[1mu\u001b[22m\u001b[0m\u001b[1ml\u001b[22me\n",
"\n"
]
},
{
"output_type": "execute_result",
"execution_count": 22,
"data": {
"text/plain": " No documentation found.\n\n \u001b[36mmodulus\u001b[39m is of type \u001b[36mInt64\u001b[39m.\n\n\u001b[1m Summary\u001b[22m\n\u001b[1m ≡≡≡≡≡≡≡≡≡\u001b[22m\n\n\u001b[36m primitive type Int64 <: Signed\u001b[39m\n\n\u001b[1m Supertype Hierarchy\u001b[22m\n\u001b[1m ≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡\u001b[22m\n\n\u001b[36m Int64 <: Signed <: Integer <: Real <: Number <: Any\u001b[39m",
"text/markdown": "No documentation found.\n\n`modulus` is of type `Int64`.\n\n# Summary\n\n```\nprimitive type Int64 <: Signed\n```\n\n# Supertype Hierarchy\n\n```\nInt64 <: Signed <: Integer <: Real <: Number <: Any\n```\n",
"text/latex": "No documentation found.\n\n\\texttt{modulus} is of type \\texttt{Int64}.\n\n\\section{Summary}\n\\begin{verbatim}\nprimitive type Int64 <: Signed\n\\end{verbatim}\n\\section{Supertype Hierarchy}\n\\begin{verbatim}\nInt64 <: Signed <: Integer <: Real <: Number <: Any\n\\end{verbatim}\n"
},
"metadata": {}
}
],
"execution_count": 22,
"metadata": {
"collapsed": true,
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"execution": {
"iopub.status.busy": "2020-12-27T21:08:00.927Z",
"iopub.execute_input": "2020-12-27T21:08:01.006Z",
"iopub.status.idle": "2020-12-27T21:08:07.782Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"### Exercises\n",
"\n",
"#### 1.1 \n",
"Look up docs for the `convert` function."
],
"metadata": {}
},
{
"cell_type": "code",
"source": [
"?convert()\n"
],
"outputs": [
{
"output_type": "execute_result",
"execution_count": 23,
"data": {
"text/plain": "\u001b[36m convert(T, x)\u001b[39m\n\n Convert \u001b[36mx\u001b[39m to a value of type \u001b[36mT\u001b[39m.\n\n If \u001b[36mT\u001b[39m is an \u001b[36mInteger\u001b[39m type, an \u001b[36mInexactError\u001b[39m will be raised if \u001b[36mx\u001b[39m is not\n representable by \u001b[36mT\u001b[39m, for example if \u001b[36mx\u001b[39m is not integer-valued, or is outside\n the range supported by \u001b[36mT\u001b[39m.\n\n\u001b[1m Examples\u001b[22m\n\u001b[1m ≡≡≡≡≡≡≡≡≡≡\u001b[22m\n\n\u001b[36m julia> convert(Int, 3.0)\u001b[39m\n\u001b[36m 3\u001b[39m\n\u001b[36m \u001b[39m\n\u001b[36m julia> convert(Int, 3.5)\u001b[39m\n\u001b[36m ERROR: InexactError: Int64(3.5)\u001b[39m\n\u001b[36m Stacktrace:\u001b[39m\n\u001b[36m [...]\u001b[39m\n\n If \u001b[36mT\u001b[39m is a \u001b[36mAbstractFloat\u001b[39m or \u001b[36mRational\u001b[39m type, then it will return the closest\n value to \u001b[36mx\u001b[39m representable by \u001b[36mT\u001b[39m.\n\n\u001b[36m julia> x = 1/3\u001b[39m\n\u001b[36m 0.3333333333333333\u001b[39m\n\u001b[36m \u001b[39m\n\u001b[36m julia> convert(Float32, x)\u001b[39m\n\u001b[36m 0.33333334f0\u001b[39m\n\u001b[36m \u001b[39m\n\u001b[36m julia> convert(Rational{Int32}, x)\u001b[39m\n\u001b[36m 1//3\u001b[39m\n\u001b[36m \u001b[39m\n\u001b[36m julia> convert(Rational{Int64}, x)\u001b[39m\n\u001b[36m 6004799503160661//18014398509481984\u001b[39m\n\n If \u001b[36mT\u001b[39m is a collection type and \u001b[36mx\u001b[39m a collection, the result of \u001b[36mconvert(T, x)\u001b[39m\n may alias all or part of \u001b[36mx\u001b[39m.\n\n\u001b[36m julia> x = Int[1, 2, 3];\u001b[39m\n\u001b[36m \u001b[39m\n\u001b[36m julia> y = convert(Vector{Int}, x);\u001b[39m\n\u001b[36m \u001b[39m\n\u001b[36m julia> y === x\u001b[39m\n\u001b[36m true\u001b[39m",
"text/markdown": "```\nconvert(T, x)\n```\n\nConvert `x` to a value of type `T`.\n\nIf `T` is an [`Integer`](@ref) type, an [`InexactError`](@ref) will be raised if `x` is not representable by `T`, for example if `x` is not integer-valued, or is outside the range supported by `T`.\n\n# Examples\n\n```jldoctest\njulia> convert(Int, 3.0)\n3\n\njulia> convert(Int, 3.5)\nERROR: InexactError: Int64(3.5)\nStacktrace:\n[...]\n```\n\nIf `T` is a [`AbstractFloat`](@ref) or [`Rational`](@ref) type, then it will return the closest value to `x` representable by `T`.\n\n```jldoctest\njulia> x = 1/3\n0.3333333333333333\n\njulia> convert(Float32, x)\n0.33333334f0\n\njulia> convert(Rational{Int32}, x)\n1//3\n\njulia> convert(Rational{Int64}, x)\n6004799503160661//18014398509481984\n```\n\nIf `T` is a collection type and `x` a collection, the result of `convert(T, x)` may alias all or part of `x`.\n\n```jldoctest\njulia> x = Int[1, 2, 3];\n\njulia> y = convert(Vector{Int}, x);\n\njulia> y === x\ntrue\n```\n",
"text/latex": "\\begin{verbatim}\nconvert(T, x)\n\\end{verbatim}\nConvert \\texttt{x} to a value of type \\texttt{T}.\n\nIf \\texttt{T} is an \\href{@ref}{\\texttt{Integer}} type, an \\href{@ref}{\\texttt{InexactError}} will be raised if \\texttt{x} is not representable by \\texttt{T}, for example if \\texttt{x} is not integer-valued, or is outside the range supported by \\texttt{T}.\n\n\\section{Examples}\n\\begin{verbatim}\njulia> convert(Int, 3.0)\n3\n\njulia> convert(Int, 3.5)\nERROR: InexactError: Int64(3.5)\nStacktrace:\n[...]\n\\end{verbatim}\nIf \\texttt{T} is a \\href{@ref}{\\texttt{AbstractFloat}} or \\href{@ref}{\\texttt{Rational}} type, then it will return the closest value to \\texttt{x} representable by \\texttt{T}.\n\n\\begin{verbatim}\njulia> x = 1/3\n0.3333333333333333\n\njulia> convert(Float32, x)\n0.33333334f0\n\njulia> convert(Rational{Int32}, x)\n1//3\n\njulia> convert(Rational{Int64}, x)\n6004799503160661//18014398509481984\n\\end{verbatim}\nIf \\texttt{T} is a collection type and \\texttt{x} a collection, the result of \\texttt{convert(T, x)} may alias all or part of \\texttt{x}.\n\n\\begin{verbatim}\njulia> x = Int[1, 2, 3];\n\njulia> y = convert(Vector{Int}, x);\n\njulia> y === x\ntrue\n\\end{verbatim}\n"
},
"metadata": {}
}
],
"execution_count": 23,
"metadata": {
"execution": {
"iopub.status.busy": "2020-12-27T21:09:03.316Z",
"iopub.execute_input": "2020-12-27T21:09:03.382Z",
"iopub.status.idle": "2020-12-27T21:09:03.746Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"#### 1.2 \n",
"Assign `365` to a variable named `days`. Convert `days` to a float and assign it to variable `days_float`"
],
"metadata": {}
},
{
"cell_type": "code",
"source": [
"days=365\n",
"days_float=convert(Float64, days)\n",
"typeof(days_float)"
],
"outputs": [
{
"output_type": "execute_result",
"execution_count": 30,
"data": {
"text/plain": "Float64"
},
"metadata": {}
}
],
"execution_count": 30,
"metadata": {
"execution": {
"iopub.status.busy": "2020-12-27T21:15:13.850Z",
"iopub.execute_input": "2020-12-27T21:15:13.907Z",
"iopub.status.idle": "2020-12-27T21:15:14.046Z"
}
}
},
{
"cell_type": "code",
"source": [
"days_float"
],
"outputs": [
{
"output_type": "execute_result",
"execution_count": 34,
"data": {
"text/plain": "365.0"
},
"metadata": {}
}
],
"execution_count": 34,
"metadata": {
"collapsed": true,
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"execution": {
"iopub.status.busy": "2020-12-27T21:17:10.231Z",
"iopub.execute_input": "2020-12-27T21:17:10.290Z",
"iopub.status.idle": "2020-12-27T21:17:10.436Z"
}
}
},
{
"cell_type": "code",
"source": [
"@assert days == 365\n",
"@assert days_float == 365.0\n"
],
"outputs": [],
"execution_count": 32,
"metadata": {
"deletable": false,
"editable": false,
"hide_input": true,
"nbgrader": {
"checksum": "a2dc243275e0310c3b29a745b952f321",
"grade": true,
"grade_id": "cell-715f78016beb0489",
"locked": true,
"points": 1,
"schema_version": 1,
"solution": false
},
"execution": {
"iopub.status.busy": "2020-12-27T21:15:49.027Z",
"iopub.execute_input": "2020-12-27T21:15:49.103Z",
"iopub.status.idle": "2020-12-27T21:15:49.213Z"
}
}
},
{
"cell_type": "code",
"source": [
"?@assert\n"
],
"outputs": [
{
"output_type": "execute_result",
"execution_count": 33,
"data": {
"text/plain": "\u001b[36m @assert cond [text]\u001b[39m\n\n Throw an \u001b[36mAssertionError\u001b[39m if \u001b[36mcond\u001b[39m is \u001b[36mfalse\u001b[39m. Preferred syntax for writing\n assertions. Message \u001b[36mtext\u001b[39m is optionally displayed upon assertion failure.\n\n\u001b[33m\u001b[1m │ \u001b[22m\u001b[39m\u001b[33m\u001b[1mWarning\u001b[22m\u001b[39m\n\u001b[33m\u001b[1m │\u001b[22m\u001b[39m\n\u001b[33m\u001b[1m │\u001b[22m\u001b[39m An assert might be disabled at various optimization levels. Assert\n\u001b[33m\u001b[1m │\u001b[22m\u001b[39m should therefore only be used as a debugging tool and not used for\n\u001b[33m\u001b[1m │\u001b[22m\u001b[39m authentication verification (e.g., verifying passwords), nor\n\u001b[33m\u001b[1m │\u001b[22m\u001b[39m should side effects needed for the function to work correctly be\n\u001b[33m\u001b[1m │\u001b[22m\u001b[39m used inside of asserts.\n\n\u001b[1m Examples\u001b[22m\n\u001b[1m ≡≡≡≡≡≡≡≡≡≡\u001b[22m\n\n\u001b[36m julia> @assert iseven(3) \"3 is an odd number!\"\u001b[39m\n\u001b[36m ERROR: AssertionError: 3 is an odd number!\u001b[39m\n\u001b[36m \u001b[39m\n\u001b[36m julia> @assert isodd(3) \"What even are numbers?\"\u001b[39m",
"text/markdown": "```\n@assert cond [text]\n```\n\nThrow an [`AssertionError`](@ref) if `cond` is `false`. Preferred syntax for writing assertions. Message `text` is optionally displayed upon assertion failure.\n\n!!! warning\n An assert might be disabled at various optimization levels. Assert should therefore only be used as a debugging tool and not used for authentication verification (e.g., verifying passwords), nor should side effects needed for the function to work correctly be used inside of asserts.\n\n\n# Examples\n\n```jldoctest\njulia> @assert iseven(3) \"3 is an odd number!\"\nERROR: AssertionError: 3 is an odd number!\n\njulia> @assert isodd(3) \"What even are numbers?\"\n```\n",
"text/latex": "\\begin{verbatim}\n@assert cond [text]\n\\end{verbatim}\nThrow an \\href{@ref}{\\texttt{AssertionError}} if \\texttt{cond} is \\texttt{false}. Preferred syntax for writing assertions. Message \\texttt{text} is optionally displayed upon assertion failure.\n\n\\begin{quote}\n\\textbf{warning}\n\nWarning\n\nAn assert might be disabled at various optimization levels. Assert should therefore only be used as a debugging tool and not used for authentication verification (e.g., verifying passwords), nor should side effects needed for the function to work correctly be used inside of asserts.\n\n\\end{quote}\n\\section{Examples}\n\\begin{verbatim}\njulia> @assert iseven(3) \"3 is an odd number!\"\nERROR: AssertionError: 3 is an odd number!\n\njulia> @assert isodd(3) \"What even are numbers?\"\n\\end{verbatim}\n"
},
"metadata": {}
}
],
"execution_count": 33,
"metadata": {
"collapsed": true,
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"execution": {
"iopub.status.busy": "2020-12-27T21:16:11.212Z",
"iopub.execute_input": "2020-12-27T21:16:11.308Z",
"iopub.status.idle": "2020-12-27T21:16:13.903Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"#### 1.3 \n",
"See what happens when you execute\n",
"\n",
"```julia\n",
"convert(Int64, \"1\")\n",
"```\n",
"and\n",
"\n",
"```julia\n",
"parse(Int64, \"1\")\n",
"```"
],
"metadata": {}
},
{
"cell_type": "code",
"source": [
"convert(Int64, \"1\")"
],
"outputs": [
{
"output_type": "error",
"ename": "MethodError",
"evalue": "MethodError: Cannot `convert` an object of type String to an object of type Int64\nClosest candidates are:\n convert(::Type{T}, !Matched::T) where T<:Number at number.jl:6\n convert(::Type{T}, !Matched::Number) where T<:Number at number.jl:7\n convert(::Type{T}, !Matched::Ptr) where T<:Integer at pointer.jl:23\n ...",
"traceback": [
"MethodError: Cannot `convert` an object of type String to an object of type Int64\nClosest candidates are:\n convert(::Type{T}, !Matched::T) where T<:Number at number.jl:6\n convert(::Type{T}, !Matched::Number) where T<:Number at number.jl:7\n convert(::Type{T}, !Matched::Ptr) where T<:Integer at pointer.jl:23\n ...",
"",
"Stacktrace:",
" [1] top-level scope at In[35]:1"
]
}
],
"execution_count": 35,
"metadata": {
"execution": {
"iopub.status.busy": "2020-12-27T21:17:53.289Z",
"iopub.execute_input": "2020-12-27T21:17:53.366Z"
}
}
},
{
"cell_type": "code",
"source": [
"parse(Int64, \"1\")"
],
"outputs": [
{
"output_type": "execute_result",
"execution_count": 36,
"data": {
"text/plain": "1"
},
"metadata": {}
}
],
"execution_count": 36,
"metadata": {
"collapsed": true,
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"execution": {
"iopub.status.busy": "2020-12-27T21:18:23.368Z",
"iopub.execute_input": "2020-12-27T21:18:23.443Z",
"iopub.status.idle": "2020-12-27T21:18:23.617Z"
}
}
},
{
"cell_type": "code",
"source": [
"?parse"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"search: \u001b[0m\u001b[1mp\u001b[22m\u001b[0m\u001b[1ma\u001b[22m\u001b[0m\u001b[1mr\u001b[22m\u001b[0m\u001b[1ms\u001b[22m\u001b[0m\u001b[1me\u001b[22m try\u001b[0m\u001b[1mp\u001b[22m\u001b[0m\u001b[1ma\u001b[22m\u001b[0m\u001b[1mr\u001b[22m\u001b[0m\u001b[1ms\u001b[22m\u001b[0m\u001b[1me\u001b[22m \u001b[0m\u001b[1mp\u001b[22m\u001b[0m\u001b[1ma\u001b[22m\u001b[0m\u001b[1mr\u001b[22mtial\u001b[0m\u001b[1ms\u001b[22mortp\u001b[0m\u001b[1me\u001b[22mrm \u001b[0m\u001b[1mp\u001b[22m\u001b[0m\u001b[1ma\u001b[22m\u001b[0m\u001b[1mr\u001b[22mtial\u001b[0m\u001b[1ms\u001b[22mortp\u001b[0m\u001b[1me\u001b[22mrm! \u001b[0m\u001b[1mp\u001b[22m\u001b[0m\u001b[1ma\u001b[22mi\u001b[0m\u001b[1mr\u001b[22m\u001b[0m\u001b[1ms\u001b[22m ski\u001b[0m\u001b[1mp\u001b[22mch\u001b[0m\u001b[1ma\u001b[22m\u001b[0m\u001b[1mr\u001b[22m\u001b[0m\u001b[1ms\u001b[22m\n",
"\n"
]
},
{
"output_type": "execute_result",
"execution_count": 37,
"data": {
"text/plain": "\u001b[36m parse(type, str; base)\u001b[39m\n\n Parse a string as a number. For \u001b[36mInteger\u001b[39m types, a base can be specified (the\n default is 10). For floating-point types, the string is parsed as a decimal\n floating-point number. \u001b[36mComplex\u001b[39m types are parsed from decimal strings of the\n form \u001b[36m\"R±Iim\"\u001b[39m as a \u001b[36mComplex(R,I)\u001b[39m of the requested type; \u001b[36m\"i\"\u001b[39m or \u001b[36m\"j\"\u001b[39m can also be\n used instead of \u001b[36m\"im\"\u001b[39m, and \u001b[36m\"R\"\u001b[39m or \u001b[36m\"Iim\"\u001b[39m are also permitted. If the string\n does not contain a valid number, an error is raised.\n\n\u001b[39m\u001b[1m │ \u001b[22m\u001b[39m\u001b[1mJulia 1.1\u001b[22m\n\u001b[39m\u001b[1m │\u001b[22m\n\u001b[39m\u001b[1m │\u001b[22m \u001b[36mparse(Bool, str)\u001b[39m requires at least Julia 1.1.\n\n\u001b[1m Examples\u001b[22m\n\u001b[1m ≡≡≡≡≡≡≡≡≡≡\u001b[22m\n\n\u001b[36m julia> parse(Int, \"1234\")\u001b[39m\n\u001b[36m 1234\u001b[39m\n\u001b[36m \u001b[39m\n\u001b[36m julia> parse(Int, \"1234\", base = 5)\u001b[39m\n\u001b[36m 194\u001b[39m\n\u001b[36m \u001b[39m\n\u001b[36m julia> parse(Int, \"afc\", base = 16)\u001b[39m\n\u001b[36m 2812\u001b[39m\n\u001b[36m \u001b[39m\n\u001b[36m julia> parse(Float64, \"1.2e-3\")\u001b[39m\n\u001b[36m 0.0012\u001b[39m\n\u001b[36m \u001b[39m\n\u001b[36m julia> parse(Complex{Float64}, \"3.2e-1 + 4.5im\")\u001b[39m\n\u001b[36m 0.32 + 4.5im\u001b[39m",
"text/markdown": "```\nparse(type, str; base)\n```\n\nParse a string as a number. For `Integer` types, a base can be specified (the default is 10). For floating-point types, the string is parsed as a decimal floating-point number. `Complex` types are parsed from decimal strings of the form `\"R±Iim\"` as a `Complex(R,I)` of the requested type; `\"i\"` or `\"j\"` can also be used instead of `\"im\"`, and `\"R\"` or `\"Iim\"` are also permitted. If the string does not contain a valid number, an error is raised.\n\n!!! compat \"Julia 1.1\"\n `parse(Bool, str)` requires at least Julia 1.1.\n\n\n# Examples\n\n```jldoctest\njulia> parse(Int, \"1234\")\n1234\n\njulia> parse(Int, \"1234\", base = 5)\n194\n\njulia> parse(Int, \"afc\", base = 16)\n2812\n\njulia> parse(Float64, \"1.2e-3\")\n0.0012\n\njulia> parse(Complex{Float64}, \"3.2e-1 + 4.5im\")\n0.32 + 4.5im\n```\n",
"text/latex": "\\begin{verbatim}\nparse(type, str; base)\n\\end{verbatim}\nParse a string as a number. For \\texttt{Integer} types, a base can be specified (the default is 10). For floating-point types, the string is parsed as a decimal floating-point number. \\texttt{Complex} types are parsed from decimal strings of the form \\texttt{\"R±Iim\"} as a \\texttt{Complex(R,I)} of the requested type; \\texttt{\"i\"} or \\texttt{\"j\"} can also be used instead of \\texttt{\"im\"}, and \\texttt{\"R\"} or \\texttt{\"Iim\"} are also permitted. If the string does not contain a valid number, an error is raised.\n\n\\begin{quote}\n\\textbf{compat}\n\nJulia 1.1\n\n\\texttt{parse(Bool, str)} requires at least Julia 1.1.\n\n\\end{quote}\n\\section{Examples}\n\\begin{verbatim}\njulia> parse(Int, \"1234\")\n1234\n\njulia> parse(Int, \"1234\", base = 5)\n194\n\njulia> parse(Int, \"afc\", base = 16)\n2812\n\njulia> parse(Float64, \"1.2e-3\")\n0.0012\n\njulia> parse(Complex{Float64}, \"3.2e-1 + 4.5im\")\n0.32 + 4.5im\n\\end{verbatim}\n"
},
"metadata": {}
}
],
"execution_count": 37,
"metadata": {
"collapsed": true,
"jupyter": {
"source_hidden": false,
"outputs_hidden": false
},
"nteract": {
"transient": {
"deleting": false
}
},
"execution": {
"iopub.status.busy": "2020-12-27T21:18:43.528Z",
"iopub.execute_input": "2020-12-27T21:18:43.595Z",
"iopub.status.idle": "2020-12-27T21:18:44.040Z"
}
}
},
{
"cell_type": "code",
"source": [
"Please click on `Validate` on the top, once you are done with the exercises."
],
"outputs": [
{
"output_type": "error",
"ename": "LoadError",
"evalue": "syntax: extra token \"click\" after end of expression",
"traceback": [
"syntax: extra token \"click\" after end of expression",
""
]
}
],
"execution_count": 38,
"metadata": {
"execution": {
"iopub.status.busy": "2020-12-27T21:21:43.329Z",
"iopub.execute_input": "2020-12-27T21:21:43.547Z"
}
}
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.0.0",
"language": "julia",
"name": "julia-1.0"
},
"language_info": {
"file_extension": ".jl",
"name": "julia",
"mimetype": "application/julia",
"version": "1.4.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment