Chapter 2
Chapter 2
strings
INTRODUCTION TO JULIA
James Fulton
Climate informatics researcher
Strings
# Strings are surrounded by " "
name = "Jane" # Cannot use 'Jane'
println(name)
println(book)
Jane
It is a truth universally acknowledged, ...
INTRODUCTION TO JULIA
Triple quotes
# Triple quotes
poem = """Beware the Jabberwock, my son!
println(poem)
INTRODUCTION TO JULIA
Triple quotes
greeting = """ " Well hello there " """ greeting = " " Well hello there " "
println(greeting)
INTRODUCTION TO JULIA
Concatenating strings
name = "James"
INTRODUCTION TO JULIA
String interpolation
name = "James"
println(greeting)
INTRODUCTION TO JULIA
String interpolation
x_int = 10 x_float = 1.0
INTRODUCTION TO JULIA
String interpolation
x = 10
y = 3
INTRODUCTION TO JULIA
String interpolation
x = 10
y = 3
INTRODUCTION TO JULIA
Indexing strings
# Customer's seat
seat = "E5"
# Select character
row = seat[1] # this returns 'E'
println(row)
println(typeof(row))
E
Char
INTRODUCTION TO JULIA
Indexing strings
# Customer's seat
seat = "E5"
# Select characters
row = seat[1] # this returns 'E'
number = seat[2] # this returns '5'
INTRODUCTION TO JULIA
Indexing strings
# Customer's seat
seat = "E5"
# Select characters
row = seat[1] # this returns 'E'
number = seat[end] # this returns '5'
INTRODUCTION TO JULIA
Indexing strings
# Customer's seat
seat = "E5"
# Select characters
row = seat[end-1] # this returns 'E'
number = seat[end] # this returns '5'
INTRODUCTION TO JULIA
Slicing strings
println(receipt)
INTRODUCTION TO JULIA
Slicing strings
# Index position:
# 12345...
receipt = "08:30 - coffee - \$3.50"
println(time)
08:30
INTRODUCTION TO JULIA
Slicing strings
# Index position from end:
# 4321end
receipt = "08:30 - coffee - \$3.50"
println(time)
println(price)
08:30
$3.50
INTRODUCTION TO JULIA
Let's practice!
INTRODUCTION TO JULIA
Introduction to
arrays
INTRODUCTION TO JULIA
James Fulton
Climate informatics researcher
What is an array?
# Store run times with many variables # Store runtimes in array
runtime1 = 33.1 runtimes = [33.1, 32.7, 34.2, 31.9]
runtime2 = 32.7
runtime3 = 34.2 List of values
runtime4 = 31.9 Surrounded by [ ]
INTRODUCTION TO JULIA
Arrays vs. vectors vs. matrices
# Store runtimes in array
runtimes = [33.1, 32.7, 34.2, 31.9]
println(typeof(runtimes))
Vector{Float64}
INTRODUCTION TO JULIA
Arrays vs. vectors vs. matrices
INTRODUCTION TO JULIA
Arrays vs. vectors vs. matrices
INTRODUCTION TO JULIA
Arrays vs. vectors vs. matrices
INTRODUCTION TO JULIA
Array data types
# Store runtimes in array
runtimes = [33.1, 32.7, 34.2, 31.9]
println(typeof(runtimes))
Vector{Float64}
INTRODUCTION TO JULIA
Array data types
# Store runtimes in array
runtimes = [33.1, 32.7, 34.2, 31.9]
println(eltype(runtimes))
Float64
INTRODUCTION TO JULIA
Array data types
# Store integers in array # Store strings in array
number_of_customers = [11, 19, 31, 27] names = ["Amit", "Barbara", "Carlos"]
println(typeof(number_of_customers)) println(typeof(names))
Vector{Int64} Vector{String}
println(typeof(grades)) println(typeof(correct_answers))
Vector{Char} Vector{Bool}
INTRODUCTION TO JULIA
Mixed data types
# Store multiple types in array
# string bool int char float
items = ["James", true, 10, 'B', -20.3]
println(typeof(items))
Vector{Any}
INTRODUCTION TO JULIA
Mixed data types
# Store array of item names
item_names = ["chalk", "cheese", "eggs", "ham"]
println(typeof(item_names))
println(typeof(item_prices))
Vector{String}
Vector{Float64}
INTRODUCTION TO JULIA
Indexing arrays
# Index: 1 2 3 4
item_names = ["chalk", "cheese", "eggs", "ham"]
# Index: 1 2 3 4
item_prices = [2.30, 3.50, 4.25, 2.00]
println(item_names[1])
println(item_prices[1])
chalk
2.30
INTRODUCTION TO JULIA
Indexing arrays
# Index: 1 2 3 4
item_names = ["chalk", "cheese", "eggs", "ham"]
# Index: 1 2 3 4
item_prices = [2.30, 3.50, 4.25, 2.00]
println(item_names[end])
println(item_prices[end])
ham
2.00
INTRODUCTION TO JULIA
Indexing arrays
# Index: 1 2 3 4
item_names = ["chalk", "cheese", "eggs", "ham"]
# Index: 1 2 3 4
item_prices = [2.30, 3.50, 4.25, 2.00]
println(item_names[end-1])
println(item_prices[end-1])
eggs
4.25
INTRODUCTION TO JULIA
Slicing arrays
# Index: 1 2 3 4
item_names = ["chalk", "cheese", "eggs", "ham"]
# Index: 1 2 3 4
item_prices = [2.30, 3.50, 4.25, 2.00]
println(item_names[1:2])
["chalk", "cheese"]
INTRODUCTION TO JULIA
Let's practice!
INTRODUCTION TO JULIA
Working with arrays
INTRODUCTION TO JULIA
James Fulton
Climate informatics researcher
Adding an element to the end of an array
# Predefine array
x = [1,2,3,4]
println(x)
[1,2,3,4,5]
INTRODUCTION TO JULIA
Adding an element to the end of an array
# Predefine array
x = [1,2,3,4]
println(x)
print(eltype(x))
[1,2,3,4,5]
Int64
INTRODUCTION TO JULIA
Adding an element to the end of an array
# Predefine array
x = [1,2,3,4]
INTRODUCTION TO JULIA
Creating an array of given type
# Create float array
x = Float64[1,2,3,4]
print(typeof(x))
println(x)
Vector{Float64}
[1.0, 2.0, 3.0, 4.0, 5.2]
INTRODUCTION TO JULIA
Creating an array of given type
# Create empty float array
x = Float64[]
print(typeof(x))
println(x)
Vector{Float64}
Float64[]
INTRODUCTION TO JULIA
Creating an array of given type
# Create empty string array
x = String[]
print(typeof(x))
println(x)
Vector{String}
String[]
INTRODUCTION TO JULIA
Adding elements to the end of an array
# Create empty string array # Create empty string array
x = String[] x = String[]
# Add some elements to the array # Add some elements to the array
push!(x, "one") append!(x, ["one", "two", "three"])
push!(x, "two")
push!(x, "three")
println(x) println(x)
INTRODUCTION TO JULIA
Removing the last element
x = [1,2,3,4] x = [1,2,3,4]
println(x) println(x)
println(last_element)
[1, 2, 3] [1, 2, 3]
4
INTRODUCTION TO JULIA
Creating array of defined length
# Create integer array with 4 zeros
x = zeros(Int64, 4)
println(x)
[0, 0, 0, 0]
INTRODUCTION TO JULIA
Replacing an element
# Create integer array with 4 zeros
x = zeros(Int64, 4)
println(x)
[0, 0, 1, 0]
INTRODUCTION TO JULIA
Replacing many elements
# Create integer array with 4 zeros
x = zeros(Int64, 4)
println(x)
[0, 2, 3, 0]
INTRODUCTION TO JULIA
Cheatsheet
Add single element - push!(x, 1)
INTRODUCTION TO JULIA
Let's practice!
INTRODUCTION TO JULIA
Operating on arrays
INTRODUCTION TO JULIA
James Fulton
Climate informatics researcher
Basic array functions
# An array with 6 elements
x = ["a", "b", "sea", "d", "e", "f"]
println(l)
INTRODUCTION TO JULIA
Basic array functions
# An array with 6 elements # An array with 6 elements
x = ["a", "b", "sea", "d", "e", "f"] x = [100, 95, 9, 22, 75, 58]
["a", "b", "d", "e", "f", "sea"] [9, 22, 58, 75, 95, 100]
INTRODUCTION TO JULIA
Vectorized operations
# Gradient and intercept
m = -0.32
c = 34.8
println(y)
29.68
INTRODUCTION TO JULIA
Vectorized operations
# Gradient and intercept
m = -0.32
c = 34.8
INTRODUCTION TO JULIA
Array addition
Add scalar Add array
a = [1,2,3] a = [1,2,3]
b = [1,2,3]
[3, 4, 5] [2, 4, 6]
INTRODUCTION TO JULIA
Array addition
Add scalar Add array
a = [1,2,3] a = [1,2,3]
b = [1,2,3]
INTRODUCTION TO JULIA
Array subtraction
# Subtract scalar # Subtract array
println(a .- 1) println(a .- b)
[0, 1, 2] [0, 0, 0]
INTRODUCTION TO JULIA
Array multiplication
Multiply by scalar Multiply by array
a = [1,2,3] a = [1,2,3]
b = [1,2,3]
INTRODUCTION TO JULIA
Array multiplication
Multiply by scalar Multiply by array
a = [1,2,3] a = [1,2,3]
b = [1,2,3]
INTRODUCTION TO JULIA
Array division
# Divide by scalar # Divide by array
println(a ./ 2) println(a ./ b)
INTRODUCTION TO JULIA
Vectorized operations
# Gradient and intercept
m = -0.32
c = 34.8
INTRODUCTION TO JULIA
Vectorized operations
# Gradient and intercept
m = -0.32
c = 34.8
println(y)
INTRODUCTION TO JULIA
Cheatsheet
For arrays a and b
Addition a .+ 1 a .+ b or a + b
Subtraction a .- 1 a .- b or a - b
Multiplication 2 .* a or 2 * a a .* b
Division a ./ 2 or a / 2 a ./ b
INTRODUCTION TO JULIA
Let's practice!
INTRODUCTION TO JULIA