Julia, lesson 1
Julia is a language for fast numerical and scientific work that still reads like a scripting
language. Your first tool is println, which writes its arguments to the screen and then ends
the line:
Text in double quotes is a string. Julia only uses double quotes for strings; single quotes mean something else (a single character, which you will meet in the next lesson).
print writes its arguments but does not end the line, so the next output continues on the
same line. println is just print plus a newline at the end.
Both accept several arguments and write them one after another, with no space added between them. Add spaces yourself where you want them:
Numbers are printed in their normal form. Notice that whole numbers and decimal numbers look
different: println(10) prints 10, but prints . Julia keeps track of
which kind of number you have, and it shows you.
2 exercises to try what you just read.
println(10.0)10.0Inside a string, a backslash starts an escape: \n is a newline, \t a tab, \" a double quote
and \\ a backslash. A dollar sign also has a special job (inserting values, coming next
lesson), so write \$ when you want a literal $.
A # starts a comment that runs to the end of the line. For longer notes, Julia also has block
comments between #= and =#, which can span many lines.
When you run a file, Julia executes it from the first line to the last. If something goes
wrong, it stops and prints an ERROR: message followed by a stack trace that lists where the
problem happened. Read the first line of the error first; it says what went wrong.
The first run of a Julia program takes a moment, because Julia compiles your code to fast machine code just before running it. That startup cost is normal.
Write a program that prints exactly these two lines:
The program reads no input.
Print these two lines:
Build the first line from three calls: print("Saving"), then a print for the dots, then a
println for done. Print the second line with one println call that receives the
arguments "Files saved: ", 3, " of " and 3.
println("Hello, World!")
println("Julia runs one line at a time, from top to bottom.")print("Loading")
print("...")
println("done") # Loading...doneprintln("Total: ", 42) # Total: 42
println("a", "b", "c") # abcprintln("Line one\nLine two")
println("Price: \$5")# A single-line comment
println("This runs") # a comment after code
#=
A block comment.
Julia ignores all of this.
=#Hello, World!
I am learning Julia.Saving...done
Files saved: 3 of 3