typeof(1) 1+2 typeof(1.0) 1//3 typeof(1//3) 1/3 typeof(1/3) 3.2+1im typeof(3.2+1im) supertype(Int64) pi # pi sqrt(3) sin(pi/2) cos(pi) 2^100 BigInt(2)^100 a = 3.243 isa(a,Float64) isa(a,Bool) typeof('c') typeof("c") Int64('a') # character to integer Int64(2.312) # no conversion done would be inexact Int64(2.0) # conversion done, is exact Float64(3) ceil(2.312) floor(2.312) Int64(floor(2.312)) trunc(2.312) floor(-2.312) trunc(-2.312) Char(89) Bool(1) Bool(0) Bool(2) # error string(true) string(123, true, 2.0) # string concatenates stuff zero(12.32) # zero of the type of 12.32 one(Bool) # one of type Bool parse(Float32, "1.002") parse(Int64, "-1") parse(Bool, "true") print(1, 2, 15) print(1," ",2,", ",15) println(1,2,15) print(1) print("lalala") println(1) println("lalala") "print this string" s = "text" "this is a s" "this is a \$s" # interpolation t = "next" s*t # concatenation of strings via * s*" "*t u = """Hello World""" u "Ho" ^3 () (1) typeof((1)) (1,) typeof((1,)) t = ("a", 1, 2.3, true) typeof(t) t[1] t[3] u,v = t u, v A = Array{Int64}(undef, 2, 3, 5) zeros(5) zeros(Int64, 5) trues(2) for x in 1:10 println(x) end for x in 1:2:10 println(x) end [x*y for x in 1:2, y in 4:6] [1 2; 4 5] [1 2; 4 5]' [1;2] [1 2]' [1;2] == [1 2]' [(1,2)] collect((1,2)) [[1 2] 3] [[1;2];3] ndims(A) length(A) size(A) axes(A) V = vec(A) ndims(V) size(V) a = 0:0.2:3 a[3] length(a) a[16] a[end] for x in a print(x,", ") end a = collect(reshape(1:8, 2, 4)) # dimensions must fit A = Array{Any}(undef, 3) A[1] = ones(3) A[2] = trues(4) A a = A b = copy(A) # shallow copy, only the outer structure is copied c = deepcopy(A) # deep copy, every entry is copied A[1] = "new stuff" A[2][1] = false a b c mutable struct Pt x::Int64 y::Int64 name::String end p = Pt(2,1,"first point") p.x p.name ="second point" p fieldnames(Pt) d = Dict("a"=>1, "b"=>2) d["a"] d["c"] haskey(d,"b") keys(d), values(d) get(d,"c", "no such key") x = (a=1; 2*a) x, a y = begin b = 4 3 * b end y, b if false e = 2 elseif 1 == 3 e = -14 else f = 42 end println(f) println(e) i = 1 while true i += 1 if i > 10 break end end println(i) for j in 1:10 if 3 < j < 9 continue end println(j) end 1 == 2 ? "A" : "B" true || false true && false true & false [1 1] .& [1 2] 1 < 40 < 500 1 < 40 < 20 [1 2] .< [2 1] x = [2 4 6] 2x 2x+1 2x.+1 y = [1 2 3] x .+ y x + y x * y x .* y x * y' z = reshape(1:9, 3, 3) z + x z .+ x function f(x, y=15) x+y end f(2,3) f(2) g(x, y=12) = x-y g(8) (x -> x^3)(4) h(x...) = sum(x)/length(x) h(1,2,4) l(f::Function, x=10) = 2*f(x) l(f) k(x,y) = println(x, " and ", y) k(x::Int, y::Int) = x+y ?k() k(1,2) k("hello", "goodbye") k(1.0, true) methods(k) show(collect(1:90)) collect(1:90) nextfloat(3.1) ?NaN collect(enumerate("abc")) 'b' in "abc" argmax("hello again") findmax("hello again") sort(rand(10)) filter(x -> mod(x, 2)==0, 1:100) div(-7,3) # returns quotient rem(-7,3) -7 % 3 # same as rem(-7,3) mod(-7,3) # computes reduction of -7 by 3 divrem(-7,3) rand() rand(2:16, 10) randn(10) @assert 1 == 2 "oh no!" @assert 1 == 1 "oh no!" @time [x^3 for x in 1:10^8]