Basics

Installation

Modern Approach: Web

Less Modern: IDE

Old School: Terminal FTW

  • install CLI
  • use your favorite editor (vim, emacs)
  • (optional) install syntax highlighting
  • enjoy

Data Types

Defining Variables


In [30]:
local x::Bool = true


Out[30]:
true

In [33]:
let
    local x::Bool = 2
end


InexactError()

Stacktrace:
 [1] convert(::Type{Bool}, ::Int64) at ./bool.jl:7

Boolean


In [7]:
x = true; typeof(x)


Out[7]:
Bool

In [8]:
1 < 2


Out[8]:
true

In [11]:
# Chaining of (in)equalities is possible
1 < 2 == 2 > 0


Out[11]:
true

Numeric


In [2]:
x = 1; typeof(x)


Out[2]:
Int64

In [3]:
x = 1.0; typeof(x)


Out[3]:
Float64

In [6]:
x = 1.0 + 1.0im; typeof(x)


Out[6]:
Complex{Float64}

In [12]:
x = 1 + 1im; typeof(x)


Out[12]:
Complex{Int64}

In [4]:
x = 2//3; typeof(x)


Out[4]:
Rational{Int64}

Strings


In [20]:
x = "Foo"; typeof(x)


Out[20]:
String

In [17]:
y = 'F'; typeof(y)


Out[17]:
Char

In [24]:
x * "Bar"


Out[24]:
"FooBar"

Basic Operations

Numeric


In [35]:
1 + 2


Out[35]:
3

In [36]:
1 - 2


Out[36]:
-1

In [37]:
1 / 2


Out[37]:
0.5

In [38]:
1 * 2


Out[38]:
2

In [42]:
5 % 2


Out[42]:
1

In [44]:
sin(2)


Out[44]:
0.9092974268256817

In [46]:
log(1)


Out[46]:
0.0

In [48]:
exp(1)


Out[48]:
2.718281828459045

In [51]:
sqrt(3)


Out[51]:
1.7320508075688772

String

Concatenation


In [53]:
"Foo" * "Bar"


Out[53]:
"FooBar"

In [54]:
x = 1.0; "x = $(x)"


Out[54]:
"x = 1.0"

In [56]:
"x + 1 = $(x + 1)"


Out[56]:
"x + 1 = 2.0"

Modification


In [57]:
split("Euler,Gauss,Newton",",")


Out[57]:
3-element Array{SubString{String},1}:
 "Euler" 
 "Gauss" 
 "Newton"

In [60]:
strip(" Julia   
               ")


Out[60]:
"Julia"

In [61]:
replace("This is soooo lame!","lame","cool")


Out[61]:
"This is soooo cool!"

Regular Expression


In [89]:
re = r"x=\d+"; typeof(re)


Out[89]:
Regex

In [90]:
ismatch(re,"It might be true, that x=123 and y=4;")


Out[90]:
true

In [91]:
ismatch(re,"It might be true, that x>0 and y=4;")


Out[91]:
false

In [1]:
replace( "x=1.0; y=2.123; z=2",
        r"y=(\d+)(\.?\d?)"    ,
        s"y1=\1; y2=0\2"      )


Out[1]:
"x=1.0; y1=2; y2=0.123; z=2"

Excercises

Task 1

Install/Open Julia using one of the mentioned options.

Task 2

Calculate the solution of $$x - 10 + e^\pi = \sin(2.5)$$

Task 3

Calculate the exact value of y for $$ y = \frac13 + \frac35 + \frac57 + \frac{7}{11} + \frac{11}{13} $$ and convert the exact value to a floating point number.

Task 4

Create a string according to the following template and replace any occurence of '?' with the appropriate value.

"For x = 4.2 it holds that $\sin(x/2*\pi)$ = ? and $\cos(x/3*\pi)$ = ?"

Task 5

We picked up the following encrypted message from a super secret agency who must not be named.

"Whc1atzcd,th3mc1and5zcc1anzc1ac1an5w,th3r,zc1abc1utzcc1a5k5zcn,thozc1aqc1u,th35t,thi,thon5?"

Fortunately we recently aquired the instructions how to decipher the message. The required steps have to be performed in the given order and are as follows:

  • convert numbers to letters
    • 1 -> l, 3 -> e, 5 -> s
    • remove all remaining numbers
  • remove n letters in front of vowels with n being:
    • 2 for a,u
    • 3 for o,e,i
  • replace za and zc with spaces

Decipher the message.

Hint: r"[aou]" matches the vowels a,o or u. r".{3}" matches any three characters.


In [15]:
riddle = replace("What demands an  answer,  but asks no  questions?","  ","za")
riddle = replace(riddle," ","zc")
riddle = replace(riddle,r"([au])",s"cl\1")
riddle = replace(riddle,r"([oei])",s"nth\1")
riddle = replace(riddle,"l","1")
riddle = replace(riddle,"e","3")
riddle = replace(riddle,"s","5")
display(riddle)
riddle = replace(riddle,"1","l")
riddle = replace(riddle,"3","e")
riddle = replace(riddle,"5","s")
riddle = replace(riddle,r".{2}([au])",s"\1")
riddle = replace(riddle,r".{3}([oei])",s"\1")
riddle = replace(riddle,r"z[ac]"," ")


"Whc1atzcdnth3mc1and5zcc1anzc1ac1an5wnth3r,zc1abc1utzcc1a5k5zcnnthozc1aqc1unth35tnthinthon5?"
Out[15]:
"What demands an answer, but asks no questions?"

Task 6

Find a string for which the regular expression r"bb|[^b]{2}" matches.

Bonus: What does this regular expression actually mean?