{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Getting started\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" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## How to print\n", "\n", "In Julia we usually use `println()` to print" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "println(\"I'm excited to learn Julia!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## How to assign variables\n", "\n", "All we need is a variable name, value, and an equal's sign!
\n", "Julia will figure out types for us." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "my_answer = 42\n", "typeof(my_answer)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "my_pi = 3.14159\n", "typeof(my_pi)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "😺 = \"smiley cat!\"\n", "typeof(😺)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To type a smiley cat, use tab completion to select the emoji name and then tab again" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# \\:smi + --> select with down arrow + ---> + to complete" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "After assigning a value to a variable, we can reassign a value of a different type to that variable without any issue." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "😺 = 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "typeof(😺)" ] }, { "cell_type": "markdown", "metadata": {}, "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" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "😀 = 0\n", "😞 = -1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "😺 + 😞 == 😀" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## How to comment" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# You can leave comments on a single line using the pound/hash key" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#=\n", "\n", "For multi-line comments, \n", "use the '#= =#' sequence.\n", "\n", "=#" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Syntax for basic math" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sum = 3 + 7" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "difference = 10 - 3" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "product = 20 * 5" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "quotient = 100 / 10" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "power = 10 ^ 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "modulus = 101 % 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercises\n", "\n", "#### 1.1 \n", "Look up docs for the `convert` function." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 1.2 \n", "Assign `365` to a variable named `days`. Convert `days` to a float and assign it to variable `days_float`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "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 } }, "outputs": [], "source": [ "@assert days == 365\n", "@assert days_float == 365.0\n" ] }, { "cell_type": "markdown", "metadata": {}, "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", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Please click on `Validate` on the top, once you are done with the exercises." ] } ], "metadata": { "kernelspec": { "display_name": "Julia 1.0.0", "language": "julia", "name": "julia-1.0" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.0.0" } }, "nbformat": 4, "nbformat_minor": 2 }