diff --git a/3_Advanced_Data_Types.ipynb b/3_Advanced_Data_Types.ipynb index fcaa96e9c5e6d1a3d24ba16a8cf92e25d323f595..abaa7dde6bc1af46dd742b80d74b6fc62691fe64 100644 --- a/3_Advanced_Data_Types.ipynb +++ b/3_Advanced_Data_Types.ipynb @@ -8,181 +8,6 @@ "# Advanced Data Types" ] }, - { - "cell_type": "markdown", - "id": "ffd395d4-fbc2-4c81-b9ce-68b4babd901b", - "metadata": {}, - "source": [ - "## Arrays" - ] - }, - { - "cell_type": "markdown", - "id": "1aedb913-e484-43d8-b21d-0bc57f1a22c5", - "metadata": {}, - "source": [ - "An array is a sequence of values. The values in an array are called elements or sometimes items and they can be of any type. We will see more about arrays in the next lesson, for now we simply consider the basics.\n", - "There are several ways to create a new array; the simplest is to enclose the elements in square brackets (`[ ]`):" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "ddfba3ab-f520-4617-a937-3809cc1f06b6", - "metadata": {}, - "outputs": [], - "source": [ - "a1 = [10, 20, 30, 40]\n", - "@show a1\n", - "a2 = [\"a\", \"b\", \"c\"]\n", - "@show a2;" - ] - }, - { - "cell_type": "markdown", - "id": "cb15aba4-a6d4-45db-9783-501ac4b8fb63", - "metadata": {}, - "source": [ - "The first example is an array of four integers. The second is an array of three strings. The elements of an array don’t have to be the same type. The following array contains a string, a float, an integer, and another array:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "56814d39-2d81-4f2d-bf63-a1a3030d2ccf", - "metadata": {}, - "outputs": [], - "source": [ - "a3 = [\"spam\", 2.0, 5, [10, 20]]\n", - "@show a3;" - ] - }, - { - "cell_type": "markdown", - "id": "7a18ac29-c477-4d58-bb22-2566b2248d7e", - "metadata": {}, - "source": [ - "An array within another array is nested.\n", - "An array that contains no elements is called an empty array; you can create one with empty brackets, `[]`.\n", - "\n", - "The function typeof can be used to find out the kind of the array:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "f53616f1-910d-47ab-be2f-68127dd6f2e6", - "metadata": {}, - "outputs": [], - "source": [ - "@show typeof(a1)\n", - "@show typeof(a2)\n", - "@show typeof(a3);" - ] - }, - { - "cell_type": "markdown", - "id": "fd99f8ca-a174-4b25-a12b-764d7807f3a6", - "metadata": {}, - "source": [ - "The syntax for accessing the elements of an array is the same as for accessing the characters of a string—the bracket operator. The expression inside the brackets specifies the index. Remember that the indices start at 1:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "4b1262f3-672d-421f-bf1a-4dd860407a7a", - "metadata": {}, - "outputs": [], - "source": [ - "a1[1]" - ] - }, - { - "cell_type": "markdown", - "id": "8dcd70d7-467f-497a-b94e-e9aae0ec738a", - "metadata": {}, - "source": [ - "Arrays are mutable. When the bracket operator appears on the left side of an assignment, it identifies the element of the array that will be assigned:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "8442b5f4-bc86-4620-a4bc-a10322b254da", - "metadata": {}, - "outputs": [], - "source": [ - "a1[1] = 5\n", - "@show a1;" - ] - }, - { - "cell_type": "markdown", - "id": "26d27784-832e-45eb-b421-03acb522a619", - "metadata": {}, - "source": [ - "Array indices work according to these rules:\n", - " - Any integer expression can be used as an index.\n", - " - If you try to read or write an element that does not exist, you get a BoundsError.\n", - " - The keyword `end` points to the last index of the array.\n", - "\n", - "The `∈` operator also works on arrays:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "58f736f7-adfb-43ed-b825-3e031638d760", - "metadata": {}, - "outputs": [], - "source": [ - "@show 10 in a1;" - ] - }, - { - "cell_type": "markdown", - "id": "db829f89-95f0-478e-9193-938ca27691c1", - "metadata": {}, - "source": [ - "The most common way to traverse the elements of an array is with a `for` loop." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "7bd5fdef-3212-46de-af51-44f1407e53f0", - "metadata": {}, - "outputs": [], - "source": [ - "for element in a1\n", - " println(element)\n", - "end" - ] - }, - { - "cell_type": "markdown", - "id": "720bf843-5d4d-4c50-8917-193046dcaa3b", - "metadata": {}, - "source": [ - "This works well if you only need to read the elements of the array. As changing the variable `element` has no effect on the array elements.\n", - "If you want to write or update the elements, you need the indices. A common way to do that is to use the built-in function eachindex:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "a6415603-4d90-4cc2-bd07-0a93f135db33", - "metadata": {}, - "outputs": [], - "source": [ - "@show a1\n", - "for i in eachindex(a1)\n", - " a1[i] = a1[i] * 2\n", - "end\n", - "@show a1;" - ] - }, { "cell_type": "markdown", "id": "7969b8f9-335a-4bd2-82f7-1d9795234ed5",