In [1]:
function sum(a)
    s = 0.0
    for x in a
        s += x
    end
    return s
end

sum([1, 2, 3])


Out[1]:
6.0

In [2]:
1 + 2 + 3


Out[2]:
6

In [9]:
?println


search: println printstyled print sprint isprint

Out[9]:
println([io::IO], xs...)

Print (using print) xs followed by a newline. If io is not supplied, prints to stdout.

Examples

jldoctest
julia> println("Hello, world")
Hello, world

julia> io = IOBuffer();

julia> println(io, "Hello, world")

julia> String(take!(io))
"Hello, world\n"

In [10]:
print("hello world!")

In [11]:
28 + 58


Out[11]:
86

In [5]:
;ls


learning-julia.ipynb

In [6]:
;ls -la


total 80
drwxr-xr-x   4 calex  staff    128 Oct 13 15:14 .
drwxr-xr-x  13 calex  staff    416 Sep  5 21:50 ..
drwxr-xr-x   3 calex  staff     96 Sep  6 20:59 .ipynb_checkpoints
-rw-r--r--   1 calex  staff  39138 Oct 13 15:14 learning-julia.ipynb

In [7]:
println("I'm excited to learn Julia!")


I'm excited to learn Julia!

In [8]:
my_answer = 42
typeof(my_answer)


Out[8]:
Int64

In [12]:
asdf = "some string"
typeof(asdf)


Out[12]:
String

In [13]:
jkl = 0.45
typeof(jkl)


Out[13]:
Float64

In [14]:
# \:smi + <tab> --> select wtih down arrow + <enter> --> <tab> + <enter>
😺 = 1
typeof(😺)


Out[14]:
Int64

In [15]:
#=
multiline comments
another line
=#

a = 4


Out[15]:
4

In [16]:
100 % 3


Out[16]:
1

In [17]:
10^2


Out[17]:
100

In [18]:
?convert


search: convert code_native @code_native

Out[18]:
convert(T, x)

Convert x to a value of type T.

If T is an Integer type, an InexactError will be raised if x is not representable by T, for example if x is not integer-valued, or is outside the range supported by T.

Examples

jldoctest
julia> convert(Int, 3.0)
3

julia> convert(Int, 3.5)
ERROR: InexactError: Int64(Int64, 3.5)
Stacktrace:
[...]

If T is a AbstractFloat or Rational type, then it will return the closest value to x representable by T.

jldoctest
julia> x = 1/3
0.3333333333333333

julia> convert(Float32, x)
0.33333334f0

julia> convert(Rational{Int32}, x)
1//3

julia> convert(Rational{Int64}, x)
6004799503160661//18014398509481984

If T is a collection type and x a collection, the result of convert(T, x) may alias all or part of x.

jldoctest
julia> x = Int[1, 2, 3];

julia> y = convert(Vector{Int}, x);

julia> y === x
true

In [19]:
convert(Float32, 4)


Out[19]:
4.0f0

In [20]:
s1 = "I am a string."


Out[20]:
"I am a string."

In [21]:
s2 = """I am also a

a string."""


Out[21]:
"I am also a\n\na string."

In [22]:
typeof('a')


Out[22]:
Char

In [23]:
"""Hooray! No "errors" anymore!"""


Out[23]:
"Hooray! No \"errors\" anymore!"

In [24]:
name = "Curtis"
toes = 10
fingers = 10

println("Hello, my name is $name and I have $fingers fingers and $toes toes and, of course, $(fingers + toes) digits.")


Hello, my name is Curtis and I have 10 fingers and 10 toes and, of course, 20 digits.

In [25]:
s5 = string("a", "b")


Out[25]:
"ab"

In [26]:
s4 = "yes"
s5 = "no"

s4*s5


Out[26]:
"yesno"

In [27]:
s4 ^ 3


Out[27]:
"yesyesyes"

Data Structures


In [28]:
(1,2)


Out[28]:
(1, 2)

In [29]:
myanimals = ("dog", "cat")


Out[29]:
("dog", "cat")

In [30]:
typeof(myanimals)


Out[30]:
Tuple{String,String}

In [31]:
myanimals[1]


Out[31]:
"dog"

In [32]:
myanimals[1] = "hamster"


MethodError: no method matching setindex!(::Tuple{String,String}, ::String, ::Int64)

Stacktrace:
 [1] top-level scope at In[32]:1

In [33]:
mypets = (dog = "london", otherdog = "belle")


Out[33]:
(dog = "london", otherdog = "belle")

In [34]:
mypets.dog


Out[34]:
"london"

In [35]:
mypets[1]


Out[35]:
"london"

In [36]:
myphonebook = Dict("Curtis" => "817-372-6396", "Amanda" => "817-773-1966")


Out[36]:
Dict{String,String} with 2 entries:
  "Amanda" => "817-773-1966"
  "Curtis" => "817-372-6396"

In [37]:
myphonebook["Curtis"]


Out[37]:
"817-372-6396"

In [38]:
myphonebook["Curtis work"] = "817-302-7885"


Out[38]:
"817-302-7885"

In [39]:
myphonebook


Out[39]:
Dict{String,String} with 3 entries:
  "Amanda"      => "817-773-1966"
  "Curtis work" => "817-302-7885"
  "Curtis"      => "817-372-6396"

In [40]:
pop!(myphonebook, "Curtis work")


Out[40]:
"817-302-7885"

In [41]:
myphonebook


Out[41]:
Dict{String,String} with 2 entries:
  "Amanda" => "817-773-1966"
  "Curtis" => "817-372-6396"

In [42]:
mypets = ["belle", "london"]


Out[42]:
2-element Array{String,1}:
 "belle" 
 "london"

In [43]:
myanimals[1]


Out[43]:
"dog"

In [44]:
myanimals[3]


BoundsError: attempt to access ("dog", "cat")
  at index [3]

Stacktrace:
 [1] getindex(::Tuple{String,String}, ::Int64) at ./tuple.jl:24
 [2] top-level scope at In[44]:1

In [45]:
mypets[3] = "abigail"


BoundsError: attempt to access 2-element Array{String,1} at index [3]

Stacktrace:
 [1] setindex!(::Array{String,1}, ::String, ::Int64) at ./array.jl:769
 [2] top-level scope at In[45]:1

In [ ]:


In [46]:
push!(mypets, "abigail")


Out[46]:
3-element Array{String,1}:
 "belle"  
 "london" 
 "abigail"

In [47]:
mymixture = [1, 2, "test", (1,2)]


Out[47]:
4-element Array{Any,1}:
 1      
 2      
  "test"
  (1, 2)

In [48]:
push!(mymixture, "100")


Out[48]:
5-element Array{Any,1}:
 1      
 2      
  "test"
  (1, 2)
  "100" 

In [49]:
pop!(mymixture)


Out[49]:
"100"

In [50]:
some2darray = [[1, 2, 3], [4, 5, 6]]


Out[50]:
2-element Array{Array{Int64,1},1}:
 [1, 2, 3]
 [4, 5, 6]

In [51]:
myanimals


Out[51]:
("dog", "cat")

In [52]:
rand(4, 3)


Out[52]:
4×3 Array{Float64,2}:
 0.91113   0.75755   0.342478 
 0.405434  0.66534   0.333705 
 0.812506  0.717287  0.0657091
 0.887517  0.104117  0.716284 

In [53]:
rand(4, 2, 3)


Out[53]:
4×2×3 Array{Float64,3}:
[:, :, 1] =
 0.65426   0.658638
 0.940582  0.81416 
 0.187049  0.106244
 0.699707  0.166108

[:, :, 2] =
 0.399608   0.78573   
 0.0552701  0.00743789
 0.98959    0.210478  
 0.228895   0.19035   

[:, :, 3] =
 0.72393   0.976559
 0.637967  0.455296
 0.858354  0.909283
 0.113602  0.890636

In [54]:
mymixture


Out[54]:
4-element Array{Any,1}:
 1      
 2      
  "test"
  (1, 2)

In [55]:
mymixture2 = mymixture


Out[55]:
4-element Array{Any,1}:
 1      
 2      
  "test"
  (1, 2)

In [56]:
pop!(mymixture)


Out[56]:
(1, 2)

In [57]:
mymixture2


Out[57]:
3-element Array{Any,1}:
 1      
 2      
  "test"

In [58]:
mymixture3 = copy(mymixture)


Out[58]:
3-element Array{Any,1}:
 1      
 2      
  "test"

In [61]:
push!(mymixture3, "yes")


Out[61]:
4-element Array{Any,1}:
 1      
 2      
  "test"
  "yes" 

In [62]:
mymixture


Out[62]:
3-element Array{Any,1}:
 1      
 2      
  "test"

In [63]:
y = Dict{Any, Any}


Out[63]:
Dict{Any,Any}

In [64]:
y[1] = "yes"


MethodError: no method matching setindex!(::Type{Dict{Any,Any}}, ::String, ::Int64)

Stacktrace:
 [1] top-level scope at In[64]:1

In [65]:
y["ah"] = 1


MethodError: no method matching setindex!(::Type{Dict{Any,Any}}, ::Int64, ::String)

Stacktrace:
 [1] top-level scope at In[65]:1

In [66]:
y


Out[66]:
Dict{Any,Any}

In [67]:
y["ah"] = "1"


MethodError: no method matching setindex!(::Type{Dict{Any,Any}}, ::String, ::String)

Stacktrace:
 [1] top-level scope at In[67]:1

In [68]:
somedict = Dict{Any, Any}


Out[68]:
Dict{Any,Any}

In [70]:
somedict["a"] = "a"


MethodError: no method matching setindex!(::Type{Dict{Any,Any}}, ::String, ::String)

Stacktrace:
 [1] top-level scope at In[70]:1

In [71]:
length(somedict)


MethodError: no method matching length(::Type{Dict{Any,Any}})
Closest candidates are:
  length(!Matched::Core.SimpleVector) at essentials.jl:571
  length(!Matched::Base.MethodList) at reflection.jl:728
  length(!Matched::Core.MethodTable) at reflection.jl:802
  ...

Stacktrace:
 [1] top-level scope at In[71]:1

In [72]:
typeof(somedict)


Out[72]:
DataType

In [73]:
somedict = Dict{Any, Any}()


Out[73]:
Dict{Any,Any} with 0 entries

In [74]:
somedict["a"] = 1


Out[74]:
1

In [75]:
somedict


Out[75]:
Dict{Any,Any} with 1 entry:
  "a" => 1

Loops


In [1]:
n = 0
while n < 10
   println("n = $n")
    n += 1
end


n = 0
n = 1
n = 2
n = 3
n = 4
n = 5
n = 6
n = 7
n = 8
n = 9

In [4]:
for n in 1:2:10
    println("n = $n")
end


n = 1
n = 3
n = 5
n = 7
n = 9

In [5]:
myfriends = ["Amanda", "Alene", "London", "Belle"]


Out[5]:
4-element Array{String,1}:
 "Amanda"
 "Alene" 
 "London"
 "Belle" 

In [10]:
for f in myfriends
    println("Hello, $(f)!")
end


Hello, Amanda!
Hello, Alene!
Hello, London!
Hello, Belle!

In [14]:
m, n = 5, 5
A = fill(0, (m, n))


Out[14]:
5×5 Array{Int64,2}:
 0  0  0  0  0
 0  0  0  0  0
 0  0  0  0  0
 0  0  0  0  0
 0  0  0  0  0

In [12]:
for i in 1:m
    for j in 1:n
        A[i, j] = i + j
    end
end

In [13]:
A


Out[13]:
5×5 Array{Int64,2}:
 2  3  4  5   6
 3  4  5  6   7
 4  5  6  7   8
 5  6  7  8   9
 6  7  8  9  10

In [20]:
for i in 1:m, j in 1:n
    A[i, j] = i + j
end
A


Out[20]:
5×5 Array{Int64,2}:
 2  3  4  5   6
 3  4  5  6   7
 4  5  6  7   8
 5  6  7  8   9
 6  7  8  9  10

In [22]:
C = fill(0, (m, n))


Out[22]:
5×5 Array{Int64,2}:
 0  0  0  0  0
 0  0  0  0  0
 0  0  0  0  0
 0  0  0  0  0
 0  0  0  0  0

In [23]:
C = [i + j for i in 1:m, j in 1:n]


Out[23]:
5×5 Array{Int64,2}:
 2  3  4  5   6
 3  4  5  6   7
 4  5  6  7   8
 5  6  7  8   9
 6  7  8  9  10

In [25]:
a = ["hello my $f" for f in myfriends]


Out[25]:
4-element Array{String,1}:
 "hello my Amanda"
 "hello my Alene" 
 "hello my London"
 "hello my Belle" 

In [ ]: