Julia Cheat Sheet
Essential Julia syntax for scientific computing, including arrays, broadcasting, multiple dispatch, and the type system.
2 PagesIntermediateMar 22, 2026
Basics
Variables, strings, and functions.
julia
# Variables and printingx = 42name = "Julia"println("Value: ", x)s = "Hello, $name!" # string interpolationfunction greet(n) return "Hi, $n"endsquare(x) = x^2 # one-line function
Multiple Dispatch
Methods selected by the types of all arguments.
julia
area(r::Float64) = pi * r^2 # circlearea(w::Float64, h::Float64) = w * h # rectanglefunction describe(x::Int) println("$x is an integer")endfunction describe(x::AbstractString) println("$x is a string")end
Arrays
Working with Julia's 1-indexed arrays.
- zeros(3)- Creates a 3-element array of zeros
- arr[1]- Arrays are 1-indexed, not 0-indexed
- push!(arr, x)- Appends x; trailing ! marks a mutating function
- map(f, arr)- Applies function f to every element
- arr .+ 1- Broadcasting: applies + elementwise via dot syntax
- size(arr)- Returns a tuple of the array's dimensions
Type System
Defining and inspecting types.
- Int64 / Float64- Built-in concrete numeric types
- abstract type- Declares a node in the type hierarchy, cannot be instantiated
- struct- struct Point x::Float64 y::Float64 end defines an immutable type
- mutable struct- Same as struct but fields can be reassigned
- typeof(x)- Returns the runtime type of a value
- Union{Int, Nothing}- A type that is either Int or Nothing
Broadcasting & Vectorization
Element-wise operations with the dot syntax.
julia
x = [1, 2, 3]y = [10, 20, 30]z = x .+ y # element-wise add -> [11, 22, 33]w = sqrt.(x) # apply sqrt to each elementz .= x .* 2 # in-place broadcast assignment# fuse multiple broadcasts into one loopresult = @. x^2 + y - 1
Composite Types & Constructors
Defining structs and inner constructors.
julia
struct Point x::Float64 y::Float64endmutable struct Counter n::Int Counter() = new(0) # inner constructorendp = Point(1.0, 2.0)c = Counter()c.n += 1
Standard Library Modules
Built-in modules you load with using.
- LinearAlgebra- matrix ops: dot, cross, det, inv, eigen, norm
- Statistics- mean, median, std, var, cor over collections
- Random- rand, randn, shuffle, and seeding with Random.seed!
- Dates- Date, DateTime, and calendar arithmetic
- Printf- C-style @printf and @sprintf formatting macros
- Test- @test and @testset for unit testing
Macros & Metaprogramming
Working with expressions and defining macros.
julia
ex = :(a + b * 2) # quote an expressiondump(ex) # inspect the ASTmacro sayhello(name) return :( println("Hello, ", $name) )end@sayhello "Julia"@time sum(rand(1000)) # built-in timing macro
Pro Tip
Type-annotate hot-path function arguments (e.g. x::Float64) so Julia can specialize and compile fast machine code instead of falling back to slower generic dispatch.
Was this cheat sheet helpful?
Explore Topics
#Julia#JuliaCheatSheet#Programming#Intermediate#MultipleDispatch#Arrays#TypeSystem#BroadcastingVectorization#DataStructures#CheatSheet#SkillVeris