</>
CodeLearn Pro
Start Learning Free โ†’

Julia โ€” Complete Beginner Tutorial

6 modules ยท 12 examples ยท Click โ–ถ Run โ€” See Output to see exact console output

๐Ÿ“ฆ
Module 01

Variables & Types

Julia uses dynamic typing โ€” you do not need to declare types up front, Julia figures them out automatically. But unlike Python, Julia is a compiled language, so when it knows the types, it generates fast native code. Variables are assigned with = and there are no keywords like val or var โ€” just the name and the value.

Variables and Basic Types

Assign a value to a name with =. Julia infers the type automatically. You can check the type of anything with typeof(). Important: Julia uses 1-based indexing โ€” arrays start at index 1, not 0. This is different from Python, C and Java but matches mathematical notation.

๐Ÿ“š Teaches: Variable assignment, Int, Float64, String, Bool, typeof()

variables.jlJulia
1"color:#6b7280"># Basic variable assignment โ€” no keywords needed
2name = "Alice"
3age = 25
4height = 1.72 "color:#6b7280"># Float64 by default
5isStudent = "color:#9558B2">true
6 
7"color:#9558B2">println("Name: ", name)
8"color:#9558B2">println("Age: ", age)
9"color:#9558B2">println("Height: ", height, "m")
10"color:#9558B2">println("Student: ", isStudent)
11 
12"color:#6b7280"># Check types
13"color:#9558B2">println("color:#9558B2">typeof(name)) "color:#6b7280"># String
14"color:#9558B2">println("color:#9558B2">typeof(age)) "color:#6b7280"># Int64
15"color:#9558B2">println("color:#9558B2">typeof(height)) "color:#6b7280"># Float64
16"color:#9558B2">println("color:#9558B2">typeof(isStudent)) "color:#6b7280"># Bool
17 
18"color:#6b7280"># Type annotations (optional but useful)
19score::Float64 = 92.5
20"color:#9558B2">println("Score: ", score)
๐Ÿ’ก

Pro tip: Julia uses Int64 (64-bit integers) and Float64 (64-bit floats) by default on 64-bit systems. Type annotations with :: are optional โ€” use them when you want to enforce a specific type or when it helps the compiler generate faster code.

Strings and String Interpolation

Julia strings are always double-quoted. Single quotes are for characters (Char), not strings. String interpolation uses $ followed by the variable name, or $(expression) for any expression. This is cleaner than string concatenation and works inside any double-quoted string.

๐Ÿ“š Teaches: String interpolation, $var, $(expr), string methods, Char vs String

strings.jlJulia
1name = "Julia"
2version = 1.10
3score = 87.456
4 
5"color:#6b7280"># String interpolation with $
6"color:#9558B2">println("Hello, $name!")
7"color:#9558B2">println("Version: $version")
8 
9"color:#6b7280"># Expression interpolation with $()
10"color:#9558B2">println("Score rounded: $(round(score))")
11"color:#9558B2">println("Pass: $(score >= 50)")
12"color:#9558B2">println("Upper: $(uppercase(name))")
13 
14"color:#6b7280"># String methods
15text = " Hello, World! "
16"color:#9558B2">println(strip(text)) "color:#6b7280"># trim whitespace
17"color:#9558B2">println("color:#9558B2">length(strip(text))) "color:#6b7280"># "color:#9558B2">length
18"color:#9558B2">println(replace(text, "World" => "Julia"))
19"color:#9558B2">println(startswith("Julia", "Ju"))
20"color:#9558B2">println(split("a,b,c", ",")) "color:#6b7280"># split to array
21 
22"color:#6b7280"># Multi-line strings
23poem = """
24Julia is fast,
25Julia is fun,
26One language "color:#9558B2">for all,
27When the computing is done.
28"""
29"color:#9558B2">println(poem)
๐Ÿ’ก

Pro tip: In Julia, a Char (character) uses single quotes: 'A'. A String uses double quotes: "Alice". They are different types โ€” typeof('A') is Char, typeof("A") is String. Mixing them up is a common beginner mistake.

๐Ÿ”€
Module 02

Control Flow

Julia control flow is very similar to Python and MATLAB โ€” clean and readable. Every block is ended with the end keyword. Unlike Python, there is no colon (:) at the end of if/for/while headers. Julia also has elseif (one word, not else if). Like Kotlin and Scala, if returns a value so you can use it as an expression.

if / elseif / else

Julia if statements use elseif (one word) instead of Python's elif or Java's else if. Each block ends with end. You can also use if as an expression โ€” assign its result to a variable. The ternary operator condition ? true_val : false_val works too.

๐Ÿ“š Teaches: if, elseif, else, end, if as expression, ternary operator

if-else.jlJulia
1score = 78
2 
3"color:#6b7280"># Standard "color:#9558B2">if / "color:#9558B2">elseif / "color:#9558B2">else
4"color:#9558B2">if score >= 90
5 "color:#9558B2">println("Grade: A")
6"color:#9558B2">elseif score >= 75
7 "color:#9558B2">println("Grade: B")
8"color:#9558B2">elseif score >= 60
9 "color:#9558B2">println("Grade: C")
10"color:#9558B2">else
11 "color:#9558B2">println("Grade: F")
12"color:#9558B2">end
13 
14"color:#6b7280"># "color:#9558B2">if as an expression (returns a value)
15grade = "color:#9558B2">if score >= 90
16 "A"
17"color:#9558B2">elseif score >= 75
18 "B"
19"color:#9558B2">elseif score >= 60
20 "C"
21"color:#9558B2">else
22 "F"
23"color:#9558B2">end
24"color:#9558B2">println("Grade: $grade")
25 
26"color:#6b7280"># Ternary operator
27status = score >= 50 ? "Pass" : "Fail"
28"color:#9558B2">println("Status: $status")
29 
30"color:#6b7280"># Comparison operators
31x = 15
32"color:#9558B2">println(x > 10 && x < 20) "color:#6b7280"># and
33"color:#9558B2">println(x < 5 || x > 10) "color:#6b7280"># or
34"color:#9558B2">println(!(x == 15)) "color:#6b7280"># not
๐Ÿ’ก

Pro tip: In Julia, == checks equality and === checks identity (same object in memory). For most comparisons you want ==. Also, Julia has โ‰  (\ne), โ‰ค (\le) and โ‰ฅ (\ge) as Unicode operators โ€” type them in the REPL and they work as you would expect mathematically.

for and while Loops

Julia's for loop iterates over any iterable โ€” a range, array, string or custom collection. Ranges use the : syntax: 1:10 means 1 to 10 inclusive. Use 1:2:10 for a step of 2. The while loop runs until a condition becomes false. Both use end to close the block.

๐Ÿ“š Teaches: for loop, ranges (1:n), step ranges, while loop, break, continue

loops.jlJulia
1"color:#6b7280"># "color:#9558B2">for loop with range
2"color:#9558B2">println("Counting up:")
3"color:#9558B2">for i "color:#9558B2">in 1:5
4 "color:#9558B2">print("$i ")
5"color:#9558B2">end
6"color:#9558B2">println()
7 
8"color:#6b7280"># Step range โ€” every 2 numbers
9"color:#9558B2">println("Even numbers:")
10"color:#9558B2">for i "color:#9558B2">in 2:2:10
11 "color:#9558B2">print("$i ")
12"color:#9558B2">end
13"color:#9558B2">println()
14 
15"color:#6b7280"># Loop over an array
16fruits = ["apple", "mango", "grape"]
17"color:#9558B2">println("Fruits:")
18"color:#9558B2">for fruit "color:#9558B2">in fruits
19 "color:#9558B2">println(" - $fruit")
20"color:#9558B2">end
21 
22"color:#6b7280"># "color:#9558B2">while loop
23"color:#9558B2">println("Countdown:")
24n = 5
25"color:#9558B2">while n > 0
26 "color:#9558B2">print("$n ")
27 n -= 1
28"color:#9558B2">end
29"color:#9558B2">println("Go!")
30 
31"color:#6b7280"># break and continue
32"color:#9558B2">println("Skip 3:")
33"color:#9558B2">for i "color:#9558B2">in 1:6
34 i == 3 && continue "color:#6b7280"># skip 3
35 i == 6 && break "color:#6b7280"># stop at 6
36 "color:#9558B2">print("$i ")
37"color:#9558B2">end
38"color:#9558B2">println()
๐Ÿ’ก

Pro tip: 1:10 creates a UnitRange โ€” a lazy object that does not allocate memory. It is very efficient for large ranges. collect(1:10) converts it to an actual Vector if you need a concrete array.

๐Ÿ”ง
Module 03

Functions & Multiple Dispatch

Functions in Julia are defined with the function keyword or, for short expressions, with a single-line = syntax. What makes Julia unique is multiple dispatch โ€” you can define multiple versions of the same function for different argument types, and Julia automatically calls the right version. This is Julia's superpower for scientific code.

Defining Functions

Use function...end for multi-line functions. The last expression in a function is automatically returned โ€” no return keyword needed (though you can use it). For simple one-liners use the short f(x) = expression syntax. Functions can have optional keyword arguments with default values.

๐Ÿ“š Teaches: function, end, return, single-line functions, default arguments

basic-functions.jlJulia
1"color:#6b7280"># Standard "color:#9558B2">function definition
2"color:#9558B2">function greet(name)
3 "color:#9558B2">println("Hello, $name!") "color:#6b7280"># last line auto-returned
4"color:#9558B2">end
5 
6"color:#6b7280"># With explicit "color:#9558B2">return and type annotation
7"color:#9558B2">function add(a::Int, b::Int)::Int
8 "color:#9558B2">return a + b
9"color:#9558B2">end
10 
11"color:#6b7280"># Single-line shorthand
12square(x) = x^2
13cube(x) = x^3
14 
15"color:#6b7280"># Default argument "color:#9558B2">values
16"color:#9558B2">function introduce(name, age=18, city="Unknown")
17 "color:#9558B2">println("$name, age $age, from $city")
18"color:#9558B2">end
19 
20greet("Alice")
21"color:#9558B2">println(add(10, 5))
22"color:#9558B2">println(square(7))
23"color:#9558B2">println(cube(3))
24 
25introduce("Bob")
26introduce("Carol", 25)
27introduce("Dave", 30, "Cape Town")
28 
29"color:#6b7280"># Anonymous "color:#9558B2">function (lambda)
30double = x -> x * 2
31"color:#9558B2">println(double(6))
32 
33"color:#6b7280"># Map โ€” apply "color:#9558B2">function to every element
34nums = [1, 2, 3, 4, 5]
35"color:#9558B2">println("color:#9558B2">map(square, nums))
36"color:#9558B2">println("color:#9558B2">map(x -> x + 10, nums))
๐Ÿ’ก

Pro tip: In Julia, function names that end with ! (like push!, sort!, pop!) are a convention meaning the function modifies its first argument in place. Functions without ! return a new value. Always follow this convention in your own code.

Multiple Dispatch

Multiple dispatch is Julia's defining feature. You define the same function name multiple times with different argument types. Julia automatically chooses the right version based on the types of ALL arguments at call time. This is more powerful than OOP where dispatch only happens on the first argument (self).

๐Ÿ“š Teaches: multiple dispatch, method specialisation, type-based dispatch, methods()

multiple-dispatch.jlJulia
1"color:#6b7280"># Define the same "color:#9558B2">function "color:#9558B2">for different types
2"color:#9558B2">function describe(x::Int)
3 "color:#9558B2">println("Integer: $x")
4"color:#9558B2">end
5 
6"color:#9558B2">function describe(x::Float64)
7 "color:#9558B2">println("Float: $x (rounded: $(round(x, digits=2)))")
8"color:#9558B2">end
9 
10"color:#9558B2">function describe(x::String)
11 "color:#9558B2">println("String: '$x' ($(">length(x)) chars)")
12"color:#9558B2">end
13 
14"color:#9558B2">function describe(x::Bool)
15 "color:#9558B2">println("Boolean: $x")
16"color:#9558B2">end
17 
18"color:#6b7280"># Julia calls the right version automatically
19describe(42)
20describe(3.14159)
21describe("Hello, Julia!")
22describe("color:#9558B2">true)
23 
24"color:#6b7280"># Multiple dispatch on multiple arguments
25"color:#9558B2">function combine(a::Int, b::Int) = "color:#9558B2">println("Int + Int = $(a + b)")
26"color:#9558B2">function combine(a::String, b::String) = "color:#9558B2">println("String * 2: $(a * b)")
27"color:#9558B2">function combine(a::Int, b::String) = "color:#9558B2">println("Repeat: $(b^a)")
28 
29combine(3, 7)
30combine("Hello", " World")
31combine(3, "Ha")
32 
33"color:#6b7280"># See all methods "color:#9558B2">for a "color:#9558B2">function
34"color:#9558B2">println("Methods of describe: ", "color:#9558B2">length(methods(describe)))
๐Ÿ’ก

Pro tip: Multiple dispatch is why Julia packages compose so elegantly. When two packages define methods for the same function, they work together automatically โ€” no inheritance hierarchies or adapter patterns needed. This is called the expression problem, and Julia solves it natively.

๐Ÿ“Š
Module 04

Arrays & Matrices

Arrays are Julia's most important data structure โ€” they are the foundation of all numerical computing. Julia arrays are 1-indexed (first element is at index 1) and support powerful broadcasting with the dot (.) syntax. Matrices are 2D arrays and Julia has native linear algebra operations built into the standard library.

Arrays โ€” Creation, Access and Operations

Create arrays with square brackets. Access elements with arr[index] โ€” remembering Julia starts at 1. Use end to refer to the last element. Julia arrays are mutable โ€” you can push!, pop!, append! and sort! them. The dot syntax broadcasts operations over all elements without writing a loop.

๐Ÿ“š Teaches: 1-indexed arrays, push!, pop!, broadcasting (.), comprehensions, slicing

arrays-basic.jlJulia
1"color:#6b7280"># Create arrays
2nums = [10, 20, 30, 40, 50]
3names = ["Alice", "Bob", "Carol"]
4 
5"color:#6b7280"># Access โ€” Julia is 1-indexed!
6"color:#9558B2">println(nums[1]) "color:#6b7280"># first element
7"color:#9558B2">println(nums["color:#9558B2">end]) "color:#6b7280"># last element
8"color:#9558B2">println(nums[2:4]) "color:#6b7280"># slice: elements 2, 3, 4
9 
10"color:#6b7280"># Modify arrays
11push!(nums, 60) "color:#6b7280"># add to "color:#9558B2">end
12"color:#9558B2">println(nums)
13 
14pop!(nums) "color:#6b7280"># remove from "color:#9558B2">end
15"color:#9558B2">println(nums)
16 
17"color:#6b7280"># Broadcasting โ€” apply operation to every element
18doubled = nums .* 2
19squares = nums .^ 2
20"color:#9558B2">println(doubled)
21"color:#9558B2">println(squares)
22 
23"color:#6b7280"># Element-wise comparison
24big = nums .> 25
25"color:#9558B2">println(big)
26 
27"color:#6b7280"># Array comprehension
28cubes = [x^3 "color:#9558B2">for x "color:#9558B2">in 1:5]
29"color:#9558B2">println(cubes)
30 
31evens = [x "color:#9558B2">for x "color:#9558B2">in 1:20 "color:#9558B2">if x % 2 == 0]
32"color:#9558B2">println(evens)
33 
34"color:#6b7280"># Useful functions
35"color:#9558B2">println("color:#9558B2">length(nums))
36"color:#9558B2">println("color:#9558B2">sum(nums))
37"color:#9558B2">println("color:#9558B2">maximum(nums))
38"color:#9558B2">println("color:#9558B2">minimum(nums))
39"color:#9558B2">println("color:#9558B2">sort(nums, rev="color:#9558B2">true))
๐Ÿ’ก

Pro tip: The dot syntax in Julia is called broadcasting. f.(arr) applies f to every element. arr .+ 1 adds 1 to every element. arr .> 25 compares each element to 25. Broadcasting eliminates most array loops and is highly optimised by the Julia compiler.

Matrices and Linear Algebra

Matrices in Julia are 2D arrays. Use spaces to separate columns and semicolons to separate rows. Julia has world-class built-in linear algebra โ€” matrix multiplication with *, transpose with ', inverse with inv() and determinant with det(). All from the standard LinearAlgebra library.

๐Ÿ“š Teaches: Matrix creation, indexing [row, col], matrix operations, LinearAlgebra

matrices.jlJulia
1"color:#6b7280"># Create a 2x3 matrix
2A = [1 2 3;
3 4 5 6]
4 
5"color:#9558B2">println("Matrix A:")
6"color:#9558B2">println(A)
7"color:#9558B2">println("Size: ", "color:#9558B2">size(A)) "color:#6b7280"># (rows, cols)
8"color:#9558B2">println("Rows: ", "color:#9558B2">size(A, 1))
9"color:#9558B2">println("Cols: ", "color:#9558B2">size(A, 2))
10 
11"color:#6b7280"># Access elements [row, col]
12"color:#9558B2">println(A[1, 2]) "color:#6b7280"># row 1, col 2 โ†’ 2
13"color:#9558B2">println(A[2, 3]) "color:#6b7280"># row 2, col 3 โ†’ 6
14 
15"color:#6b7280"># Access whole row or column
16"color:#9558B2">println(A[1, :]) "color:#6b7280"># first row
17"color:#9558B2">println(A[:, 2]) "color:#6b7280"># second column
18 
19"color:#6b7280"># Matrix arithmetic
20B = [1 0; 0 1; 2 2] "color:#6b7280"># 3x2 matrix
21 
22"color:#6b7280"># Matrix multiplication (sizes must be compatible)
23C = A * B "color:#6b7280"># (2x3) * (3x2) = 2x2
24"color:#9558B2">println("A * B =")
25"color:#9558B2">println(C)
26 
27"color:#6b7280"># Element-wise operations
28D = [1 2; 3 4]
29"color:#9558B2">println("D .^ 2 =")
30"color:#9558B2">println(D .^ 2)
31 
32"color:#6b7280"># Transpose
33"color:#9558B2">println("D transpose =")
34"color:#9558B2">println(D')
๐Ÿ’ก

Pro tip: Julia uses column-major order for matrices โ€” columns are stored contiguously in memory, like MATLAB and Fortran (not like C or Python which are row-major). For maximum performance when iterating over a matrix, iterate over columns in the outer loop and rows in the inner loop.

๐Ÿ“‹
Module 05

Dicts, Tuples & Sets

Beyond arrays, Julia has Dicts (key-value pairs like Python dicts), Tuples (fixed immutable sequences) and Sets (unique values, no duplicates). Dicts use => for key-value pairs and are extremely useful for counting, mapping and configuration. Tuples are lightweight and immutable โ€” great for returning multiple values from functions.

Dictionaries

A Dict maps keys to values. Create one with Dict() and key => value pairs. Access with dict[key] โ€” this throws an error if the key is not there. Use get(dict, key, default) for safe access. Iterate over a Dict with for (key, value) in dict.

๐Ÿ“š Teaches: Dict creation, access, get(), haskey(), keys(), values(), iteration

dicts.jlJulia
1"color:#6b7280"># Create a Dict
2grades = Dict(
3 "Alice" => "A",
4 "Bob" => "B",
5 "Carol" => "A",
6 "Dave" => "C"
7)
8 
9"color:#6b7280"># Access
10"color:#9558B2">println(grades["Alice"])
11 
12"color:#6b7280"># Safe access โ€” returns default "color:#9558B2">if key missing
13"color:#9558B2">println("color:#9558B2">get(grades, "Eve", "Not enrolled"))
14 
15"color:#6b7280"># Check "color:#9558B2">if key exists
16"color:#9558B2">println("color:#9558B2">haskey(grades, "Bob"))
17"color:#9558B2">println("color:#9558B2">haskey(grades, "Zara"))
18 
19"color:#6b7280"># Add / update
20grades["Eve"] = "B"
21grades["Bob"] = "A"
22 
23"color:#6b7280"># Iterate
24"color:#9558B2">println("All grades:")
25"color:#9558B2">for (name, grade) "color:#9558B2">in grades
26 "color:#9558B2">println(" $name โ†’ $grade")
27"color:#9558B2">end
28 
29"color:#6b7280"># Keys and "color:#9558B2">values
30"color:#9558B2">println("Students: ", "color:#9558B2">collect("color:#9558B2">keys(grades)))
31 
32"color:#6b7280"># Count words
33words = ["julia", "is", "fast", "julia", "is", "great"]
34counts = Dict{String, Int}()
35"color:#9558B2">for w "color:#9558B2">in words
36 counts[w] = "color:#9558B2">get(counts, w, 0) + 1
37"color:#9558B2">end
38"color:#9558B2">println("Word counts: $counts")
๐Ÿ’ก

Pro tip: Dict{String, Int}() creates a typed Dict that only accepts String keys and Int values. This is more efficient than an untyped Dict() because Julia can specialise the memory layout and avoid type checks at runtime.

Tuples and Sets

Tuples are fixed, immutable sequences โ€” you cannot change them after creation. They are perfect for returning multiple values from a function. Sets store unique values with no duplicates and support fast membership testing with in. Union, intersection and difference operations work on Sets.

๐Ÿ“š Teaches: Tuple creation, destructuring, Set, union(), intersect(), in

tuples-sets.jlJulia
1"color:#6b7280"># Tuples โ€” immutable, fixed "color:#9558B2">size
2point = (3.0, 4.0)
3"color:#9558B2">println("Point: $point")
4"color:#9558B2">println("x = $(point[1]), y = $(point[2])")
5 
6"color:#6b7280"># Destructuring โ€” unpack tuple to variables
7x, y = point
8"color:#9558B2">println("Unpacked: x=$x, y=$y")
9 
10"color:#6b7280"># Named tuples
11person = (name="Alice", age=25, city="Durban")
12"color:#9558B2">println(person.name)
13"color:#9558B2">println(person.age)
14 
15"color:#6b7280"># Functions can "color:#9558B2">return multiple "color:#9558B2">values via tuple
16"color:#9558B2">function min_max(arr)
17 "color:#9558B2">return "color:#9558B2">minimum(arr), "color:#9558B2">maximum(arr)
18"color:#9558B2">end
19 
20lo, hi = min_max([5, 2, 9, 1, 7])
21"color:#9558B2">println("Min: $lo, Max: $hi")
22 
23"color:#6b7280"># Sets โ€” unique "color:#9558B2">values only
24a = Set([1, 2, 3, 2, 1, 4])
25b = Set([3, 4, 5, 6])
26 
27"color:#9558B2">println("Set a: $a")
28"color:#9558B2">println("Union: $(">union(a, b))")
29"color:#9558B2">println("Intersect: $(">intersect(a, b))")
30"color:#9558B2">println("Difference: $(">setdiff(a, b))")
31"color:#9558B2">println("3 ">in a: $(3 ">in a)")
32"color:#9558B2">println("7 ">in a: $(7 ">in a)")
๐Ÿ’ก

Pro tip: Returning multiple values from a function using a tuple is idiomatic Julia. lo, hi = min_max(arr) works because Julia automatically destructures the returned tuple. This avoids the need for out parameters or wrapper structs just to return two values.

๐Ÿ”ฌ
Module 06

Structs & Scientific Computing

Julia uses struct to define custom composite types โ€” similar to classes in Python but faster because Julia can fully specialise the code for each type. The real power of Julia comes in scientific computing: built-in statistics, linear algebra, random number generation and mathematical functions that work at C speed without any imports.

Structs โ€” Custom Types

Define custom types with struct. By default they are immutable โ€” fields cannot change. Use mutable struct if you need to change fields. Structs work perfectly with multiple dispatch โ€” you can define specialised functions for your custom types.

๐Ÿ“š Teaches: struct, mutable struct, fields, constructor, multiple dispatch with structs

structs.jlJulia
1"color:#6b7280"># Immutable "color:#9558B2">struct (default)
2"color:#9558B2">struct Point
3 x::Float64
4 y::Float64
5"color:#9558B2">end
6 
7"color:#6b7280"># Mutable "color:#9558B2">struct โ€” fields can change
8"color:#9558B2">mutable "color:#9558B2">struct Student
9 name::String
10 score::Float64
11 grade::String
12"color:#9558B2">end
13 
14"color:#6b7280"># Create instances
15p1 = Point(3.0, 4.0)
16p2 = Point(0.0, 0.0)
17 
18"color:#9558B2">println("Point: ($(p1.x), $(p1.y))")
19 
20"color:#6b7280"># Function that uses our "color:#9558B2">struct
21"color:#9558B2">function distance(a::Point, b::Point)
22 sqrt((a.x - b.x)^2 + (a.y - b.y)^2)
23"color:#9558B2">end
24 
25"color:#9558B2">println("Distance: $(distance(p1, p2))")
26 
27"color:#6b7280"># Student "color:#9558B2">struct
28alice = Student("Alice", 88.5, "B")
29"color:#9558B2">println("$(alice.name): $(alice.score) โ†’ $(alice.grade)")
30 
31"color:#6b7280"># Update "color:#9558B2">mutable "color:#9558B2">struct
32alice.score = 95.0
33alice.grade = "A"
34"color:#9558B2">println("Updated: $(alice.name): $(alice.score) โ†’ $(alice.grade)")
35 
36"color:#6b7280"># Multiple dispatch with "color:#9558B2">struct
37"color:#9558B2">function describe(p::Point)
38 "color:#9558B2">println("Point at ($(p.x), $(p.y))")
39"color:#9558B2">end
40"color:#9558B2">function describe(s::Student)
41 "color:#9558B2">println("Student $(s.name) scored $(s.score)")
42"color:#9558B2">end
43 
44describe(p1)
45describe(alice)
๐Ÿ’ก

Pro tip: Immutable structs are faster than mutable ones because Julia can stack-allocate them and inline them into other data structures. Prefer immutable struct by default and only add mutable when you genuinely need to change a field after creation.

Scientific Computing Built-ins

Julia's standard library includes world-class mathematical tools with no imports needed. Comprehensive math functions, statistics, random numbers and linear algebra are all built-in. This is what makes Julia so productive for scientists โ€” the tools you need are right there.

๐Ÿ“š Teaches: Math functions, statistics (mean, std), random numbers, useful built-ins

scientific.jlJulia
1"color:#6b7280"># Built-"color:#9558B2">in math functions
2"color:#9558B2">println(sqrt(144)) "color:#6b7280"># 12.0
3"color:#9558B2">println(abs(-42)) "color:#6b7280"># 42
4"color:#9558B2">println(floor(3.7)) "color:#6b7280"># 3.0
5"color:#9558B2">println(ceil(3.2)) "color:#6b7280"># 4.0
6"color:#9558B2">println(round(3.567, digits=2)) "color:#6b7280"># 3.57
7"color:#9558B2">println(log(โ„ฏ)) "color:#6b7280"># 1.0 (natural log)
8"color:#9558B2">println(log10(1000)) "color:#6b7280"># 3.0
9"color:#9558B2">println(sin(ฯ€/2)) "color:#6b7280"># 1.0
10"color:#9558B2">println(cos(0)) "color:#6b7280"># 1.0
11 
12"color:#6b7280"># Statistics โ€” no "color:#9558B2">import needed
13data = [72, 85, 91, 68, 79, 93, 88, 76, 82, 95]
14"color:#9558B2">println("
15--- Statistics ---")
16"color:#9558B2">println("Mean: $(">sum(data)/">length(data))")
17"color:#9558B2">println("Min: $(">minimum(data))")
18"color:#9558B2">println("Max: $(">maximum(data))")
19"color:#9558B2">println("Range: $(">maximum(data) - ">minimum(data))")
20"color:#9558B2">println("Sorted: $(">sort(data))")
21 
22"color:#6b7280"># Random numbers
23"color:#9558B2">import Random
24Random.seed!(42) "color:#6b7280"># reproducible results
25"color:#9558B2">println("
26--- Random ---")
27"color:#9558B2">println("color:#9558B2">rand()) "color:#6b7280"># Float64 "color:#9558B2">in [0,1)
28"color:#9558B2">println("color:#9558B2">rand(1:100)) "color:#6b7280"># Int "color:#9558B2">in [1,100]
29"color:#9558B2">println("color:#9558B2">rand(["A","B","C"])) "color:#6b7280"># random from array
30"color:#9558B2">println(["color:#9558B2">rand(1:10) "color:#9558B2">for _ "color:#9558B2">in 1:5]) "color:#6b7280"># 5 random ints
๐Ÿ’ก

Pro tip: Julia has the mathematical constants โ„ฏ (Euler's number, type \euler), ฯ€ (pi, type \pi) and im (imaginary unit) built in. You can use ฯ€ and โ„ฏ directly in expressions: 2ฯ€ is 6.283..., sin(ฯ€/4) is 0.707.... This makes mathematical code look exactly like the textbook formulas.

๐ŸŽ‰

You finished the Julia tutorial!

You now know Julia variables, strings, control flow, functions, multiple dispatch, arrays, matrices, collections and scientific computing. Your next step is to install Julia and explore Plots.jl for visualisation or DataFrames.jl for data analysis.