Julia Lab Examples

Twenty-one single-file programs, each short enough to read in a minute and complete enough to run. They follow the lesson order of the track: foundations first, then functions and scope, collections and types, and finally exceptions and files. Open a file, run it with julia file.jl, then change one thing and run it again — that second run is where the learning happens.

Every example is self-contained: no package to install, no project to activate, nothing but the standard library. Where a lesson introduced a concept — implicit types, multiple dispatch, the type tree, lexical scope — there is a program here that shows it working, and one line you can break to see what happens when it is wrong.

Foundations

Variables, expressions, and the ways Julia decides what to do next. Start here: these files introduce the values (Int, Float64, Bool, String, Char) that every later example uses.

#DescriptionLink
1Variables and implicit typesView
2Literals and operatorsView
3Boolean values and logicView
4Single-branch conditionalView
5Comparison and branching on inputView
6Character input with an if/elseif ladderView
7Conditional loop with whileView
8Counting loop with continueView
9Nested iteration over two rangesView

Functions & Scope

A function is the unit of reuse and the unit of method dispatch. These four programs show declaration, a declared return type, and the difference between global, local, and let-block scope — the rule that explains most "why is my variable nothing?" questions.

#DescriptionLink
10Declaring and calling a functionView
11A function with a declared return typeView
12Global versus local variablesView
13Nested let blocksView

Collections & Types

Arrays, tuples, dictionaries, and the type machinery underneath them: abstract and primitive types, composite structs, and parametric types. Run these with the REPL open and inspect typeof(x) after each line.

#DescriptionLink
14Arrays: one and two dimensionalView
15Tuples: indexing and destructuringView
16Dictionaries: keys to valuesView
17The type tree: abstract and primitive typesView
18Composite types with mutable structView
19Parametric types: Point{T}View

Errors & Files

Two programs about failure: how to guarantee a file is closed when something throws, and how to catch an error and return a useful value instead of crashing. Delete the try in either one and watch the behaviour change.

#DescriptionLink
20Opening and closing a file with try/finallyView
21Recovering from an error with try/catchView
How to use these examples. Run each file straight from the track's demo folder — julia roadmap/julia/demo/01_variables.jl — or open it in the code viewer and copy it out. Files 5 and 6 read from standard input, so they expect you to type a character and press Enter. Nothing here needs a package, which makes this folder a good place to practise without touching a project environment.

Larger, multi-file programs follow in Study Projects.