Julia language Basics cheat sheet

Categories

String Theory! :
Use ” or “”” for strings and ‘ for characters.
println(“Hello, my name is $name.”)
 println(“That is $(num_fingers + num_toes) digits in all!!”)
string(“I don’t know, but “, 😺, ” is too few.”)
s3*s4

To handle errors:
@assert *condition* “Error message”

Tuple:
myfavoriteanimals = (“penguins”, “cats”, “sugargliders”)
myfavoriteanimals[1]
You cannot do this: myfavoriteanimals[1] = “otters”

Named Tuple:
myfavoriteanimals = (bird = “penguins”, mammal = “cats”, marsupial = “sugargliders”)
myfavoriteanimals.bird
myfavoriteanimals[1]

Dictionary:
myphonebook = Dict(“Jenny” => “867-5309”, “Ghostbusters” => “555-2368”)
myphonebook[“Jenny”]
pop!(myphonebook, “Kramer”)
You cannot: myphonebook[1]

Arrays:
myfriends = [“Ted”, “Robyn”, “Barney”, “Lily”, “Marshall”]
myfriends[3]
myfriends[3] = “Baby Bop”
push!(fibonacci, 21)
pop!(fibonacci)
somemorenumbers = copy(fibonacci)

myfriends = ["Ted", "Robyn", "Barney", "Lily", "Marshall"]

i = 1
while i <= length(myfriends)
    friend = myfriends[i]
    println("Hi $friend, it's great to see you!")
    i += 1
end
for n in 1:10
    println(n)
end


myfriends = ["Ted", "Robyn", "Barney", "Lily", "Marshall"]

for friend in myfriends
    println("Hi $friend, it's great to see you!")
end

m, n = 5, 5
A = fill(0, (m, n))
for j in 1:n
    for i in 1:m
        A[i, j] = i + j
    end
end
A

B = fill(0, (m, n))
for j in 1:n, i in 1:m
    B[i, j] = i + j
end
B

C = [i + j for i in 1:m, j in 1:n]
if (N % 3 == 0) && (N % 5 == 0) # `&&` means "AND"; % computes the remainder after division
    println("FizzBuzz")
elseif N % 3 == 0
    println("Fizz")
elseif N % 5 == 0
    println("Buzz")
else
    println(N)
end


(x > y) ? x : y

true && (println("hi"); true)
>>hi
>>true

(x > 0) && error("x cannot be greater than 0")

true || println("hi")
>>TRUE
false || println("hi")
>>hi
function sayhi(name)
    println("Hi $name, it's great to see you!")
end


sayhi2(name) = println("Hi $name, it's great to see you!")

sayhi3 = name -> println("Hi $name, it's great to see you!")

# By convention, functions followed by `!` alter (change) their contents and functions lacking `!` do not.

map(f, [1, 2, 3]) ::: [f(1), f(2), f(3)]

map(x -> x^3, [1, 2, 3])
broadcast(f, [1, 2, 3]) ::: f.([1, 2, 3])

A = [i + 3*j for j in 0:2, i in 1:3]
A .+ 2 .* f.(A) ./ A ::: broadcast(x -> x + 2 * f(x) / x, A)

using Pkg
Pkg.add("Plots")
using Plots
globaltemperatures = [14.4, 14.5, 14.8, 15.2, 15.5, 15.8]
numpirates = [45000, 20000, 15000, 5000, 400, 17];
gr()
plot(numpirates, globaltemperatures, label="line")  
scatter!(numpirates, globaltemperatures, label="points") 
xlabel!("Number of Pirates [Approximate]")
ylabel!("Global Temperature (C)")
title!("Influence of pirate population on global warming")
xflip!() # change the X other side

Pkg.add("UnicodePlots")
unicodeplots()
plot(numpirates, globaltemperatures, label="line")  
scatter!(numpirates, globaltemperatures, label="points") 
xlabel!("Number of Pirates [Approximate]")
ylabel!("Global Temperature (C)")
title!("Influence of pirate population on global warming")