Skip to content

Instantly share code, notes, and snippets.

@reesepathak
Created September 27, 2016 20:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reesepathak/023a82fe70d75c0b3114b4f6af200392 to your computer and use it in GitHub Desktop.
Save reesepathak/023a82fe70d75c0b3114b4f6af200392 to your computer and use it in GitHub Desktop.
Julia Intro
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Introduction to Julia\n",
"__EE103, Autumn 2016-2017__\n",
"\n",
"In this notebook, we introduce some basic programming concepts in $\\verb|Julia|$. \n",
"\n",
"Please see the accompanying slides [here](http://web.stanford.edu/class/ee103/slides/julia_intro_slides.pdf)."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# This is a comment.\n",
"# Use them to write short notes and descriptions in your code."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Input/Output"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In a Julia notebook, the last item in a cell is printed:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"\"Hello!\""
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"3 # won't print\n",
"\"Hello!\" # will print"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To print multiple things out, use $\\verb|println()|$"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n",
"Hello!\n"
]
}
],
"source": [
"println(3)\n",
"println(\"Hello!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To supress the output of a cell, add a semicolon"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"3;\n",
"\"Hello\"; "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Data in Julia\n",
"We can write numbers, booleans, and strings."
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"1.0"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"1.0 # decimals work"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"2 # integers"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"-349.04"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"-349.04 # negative numbers work"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"5.5e9"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"5.5e9 # scientific notation is acceptable"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"\"Hello World!\""
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"Hello World!\" # strings"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"true"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"true # booleans work (true)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"false"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"false # booleans work (false)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Assignment of variables\n",
"We use $\\verb|=|$ to assign values to variables"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n",
"bob\n"
]
}
],
"source": [
"x = 3\n",
"name = \"bob\"\n",
"println(x)\n",
"println(name)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can test whether one variable equals another variable or value using $\\verb|==|$. "
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"true"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = 4\n",
"b = 4\n",
"a == b"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"false"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"name = \"bob\"\n",
"name == \"Bob\" # should return false (case sensitive)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Julia as a calculator\n",
"Julia supports all the arithmetic operators you could want:"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"7"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"3 + 4"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"-6"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"4 - 10"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"0.75"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"3/4"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"900"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"10*90"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"16"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"4^2 # exponentiation (4 squared) "
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"1.0000000000000002"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"exp(0) + sin(pi) + log(1) # other functions are automatically supported"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Boolean Operations\n",
"There are many operations which return true or false:\n",
"- $\\verb|a == b|$: tests whether a and b are equal \n",
"- $\\verb|a != b|$: tests whether a and b are NOT equal \n",
"- $\\verb|a < b|$: tests whether a is (strictly) less than b\n",
"- $\\verb|a > b|$: tests whether a is (strictly) greater than b\n",
"- $\\verb|a <= b|$: tests whether a is less than or equal to b \n",
"- $\\verb|a >= b|$: tests whether a is greater than or equal to b"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"false"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"3 == 4"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"true"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(1 + 1) >= 2"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"true"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"Wizard\" > \"Dragon\" # alphabetic ordering (lexicographic)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can join two boolean expressions using either:\n",
"- $\\verb|&&|$ (and): this checks if multiple expressions are ALL true\n",
"- $\\verb||||$ (or): this checks if *at least* one expression is true"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"true"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(1 + 1 == 2) || (\"Hi\" != \"Hi\") || (59 == 3) # only one of these has to be true"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"false"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(1 + 1 == 2) && (\"Hi\" != \"Hi\") && (59 == 3) # now all need to be true"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Controlling code with logic and flow statements\n",
"We can use $\\verb|if-elseif-else|$ statements to control the functionality of code"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"20"
]
}
],
"source": [
"value = 9\n",
"# Controlling what happens next w/ if-elseif-else\n",
"if (value < 5)\n",
"value = 10\n",
"elseif (value == 5)\n",
"value = 15\n",
"else\n",
"value = 20\n",
"end\n",
"print(value) # what should this be?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Ranges\n",
"We can define a list of discrete values on a range"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"1:5"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"1:5 # is stored compactly"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"10-element Array{Int64,1}:\n",
" 1\n",
" 2\n",
" 3\n",
" 4\n",
" 5\n",
" 6\n",
" 7\n",
" 8\n",
" 9\n",
" 10"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"collect(1:10) # shows us what is inside the range"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"7-element Array{Float64,1}:\n",
" 1.0\n",
" 1.5\n",
" 2.0\n",
" 2.5\n",
" 3.0\n",
" 3.5\n",
" 4.0"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"collect(1:0.5:4) # we can (optionally) define the step size"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Lists\n",
"We can use lists to store: \n",
"- mixed data (for example, strings, and numbers)\n",
"- only numbers (our main usage - vectors)\n",
"- multiple lists (for example, a list of vectors)"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"3-element Array{Int64,1}:\n",
" 3\n",
" 4\n",
" 5"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"vector = [3; 4; 5]"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"vector[2] # accessing second element of vector"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"length(vector)"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"3-element Array{Array{Int64,1},1}:\n",
" [3,4,5] \n",
" [-1,-2,-3] \n",
" [0,-10,-20]"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list_of_vectors = [[3;4;5], [-1;-2;-3], [0, -10, -20]]"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"3-element Array{Int64,1}:\n",
" -1\n",
" -2\n",
" -3"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list_of_vectors[2] # returns 2nd vector"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list_of_vectors[1][3] # returns 3rd element of 1st vector "
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 64,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"length(list_of_vectors) # returns the number of vectors"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Loops\n",
"We can use loops to repeat a block of code under certain conditions\n",
"- $\\verb|for|$-loops: repeat code through iteration\n",
"- $\\verb|while|$-loops: repeat code under some (boolean) condition"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 2 3 4 5 6 7 8 9 10 "
]
}
],
"source": [
"for i in 1:10\n",
" print(i)\n",
" print(\" \")\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"5-element Array{Int64,1}:\n",
" 3\n",
" 4\n",
" 10\n",
" -2\n",
" 9"
]
},
"execution_count": 68,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"vector = [3, 4, 10, -2, 9]"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n",
"4\n",
"10\n",
"-2\n",
"9\n"
]
}
],
"source": [
"for entry in vector\n",
" println(entry)\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n",
"4\n",
"10\n",
"-2\n",
"9\n"
]
}
],
"source": [
"counter = 1\n",
"while(counter <= length(vector))\n",
" println(vector[counter])\n",
" counter += 1\n",
"end"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"### Functions"
]
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"WARNING: Method definition f(Any) in module Main at In[77]:1 overwritten at In[81]:1.\n"
]
}
],
"source": [
"f(x) = x^2; "
]
},
{
"cell_type": "code",
"execution_count": 82,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"9"
]
},
"execution_count": 82,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f(3) "
]
},
{
"cell_type": "code",
"execution_count": 83,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"WARNING: Method definition power_function(Any, Any) in module Main at In[80]:3 overwritten at In[83]:3.\n"
]
},
{
"data": {
"text/plain": [
"power_function (generic function with 1 method)"
]
},
"execution_count": 83,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# long form definition\n",
"function power_function(x, power)\n",
" result = x^power\n",
" return result\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 84,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"32"
]
},
"execution_count": 84,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"power_function(2, 5)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 0.6.0-dev",
"language": "julia",
"name": "julia-0.6"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "0.6.0"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment