0% found this document useful (0 votes)
11 views62 pages

Chapter 2

Uploaded by

lucy heartfilia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views62 pages

Chapter 2

Uploaded by

lucy heartfilia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 62

Introduction to

strings
INTRODUCTION TO JULIA

James Fulton
Climate informatics researcher
Strings
# Strings are surrounded by " "
name = "Jane" # Cannot use 'Jane'

# Strings can be any length


book = "It is a truth universally acknowledged, ..."

println(name)
println(book)

Jane
It is a truth universally acknowledged, ...

INTRODUCTION TO JULIA
Triple quotes
# Triple quotes
poem = """Beware the Jabberwock, my son!

The jaws that bite, the claws that catch!"""

println(poem)

Beware the Jabberwock, my son!

The jaws that bite, the claws that catch!

INTRODUCTION TO JULIA
Triple quotes
greeting = """ " Well hello there " """ greeting = " " Well hello there " "

println(greeting)

" Well hello there " ERROR: syntax: ...

INTRODUCTION TO JULIA
Concatenating strings
name = "James"

greeting = "Well hello there, "

# Concatenate two strings


println(greeting*name)

Well hello there, James

INTRODUCTION TO JULIA
String interpolation
name = "James"

# Interpolate with $ symbol


greeting = "Well hello there, $name"

println(greeting)

Well hello there, James

INTRODUCTION TO JULIA
String interpolation
x_int = 10 x_float = 1.0

# Insert integer into string # Insert float into string


println("The value is $x_int") println("The value is $x_float")

The value is 10 The value is 1.0

x_bool = true x_char = 'A'

# Insert boolean into string # Insert character into string


println("The value is $x_bool") println("The value is $x_char")

The value is true The value is A

INTRODUCTION TO JULIA
String interpolation
x = 10
y = 3

# Insert x*y into string


println("The product of x and y is $(x*y)")

The product of x and y is 30

INTRODUCTION TO JULIA
String interpolation
x = 10
y = 3

# Insert x*y into string


println("The product of x and y is \$(x*y)")

The product of x and y is $(x*y)

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'

println("Your seat is in row $row, seat number $number.")

Your seat is in row E, seat number 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'

println("Your seat is in row $row, seat number $number.")

Your seat is in row E, seat number 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'

println("Your seat is in row $row, seat number $number.")

Your seat is in row E, seat number 5.

INTRODUCTION TO JULIA
Slicing strings

receipt = "08:30 - coffee - \$3.50"

println(receipt)

08:30 - coffee - $3.50

INTRODUCTION TO JULIA
Slicing strings
# Index position:
# 12345...
receipt = "08:30 - coffee - \$3.50"

time = receipt[1:5] # Select first 5 characters

println(time)

08:30

INTRODUCTION TO JULIA
Slicing strings
# Index position from end:
# 4321end
receipt = "08:30 - coffee - \$3.50"

time = receipt[1:5] # Select first 5 characters


price = receipt[end-4:end] # Select last 5 characters

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 [ ]

With , in beween values

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}

# Store characters in array # Store booleans in array


grades = ['A', 'B', 'B', 'A'] correct_answers = [true, false, true]

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"]

# Store array of item prices


item_prices = [2.30, 3.50, 4.25, 2.00]

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]

# Add the number 5 to end of array


push!(x, 5)

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]

# Add the float 5.0 to end of array


push!(x, 5.0)

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]

# Add the float 5.2 to end of array


push!(x, 5.2)

ERROR: InexactError: Int64(5.2)

INTRODUCTION TO JULIA
Creating an array of given type
# Create float array
x = Float64[1,2,3,4]

print(typeof(x))

# Add the float 5.2 to end of array


push!(x, 5.2)

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)

["one", "two", "three"] ["one", "two", "three"]

INTRODUCTION TO JULIA
Removing the last element
x = [1,2,3,4] x = [1,2,3,4]

# Remove 1 element from end # Remove 1 element from end


x = x[1:end-1] last_element = pop!(x)

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)

# Replace element in position 3 with value 1


x[3] = 1

println(x)

[0, 0, 1, 0]

INTRODUCTION TO JULIA
Replacing many elements
# Create integer array with 4 zeros
x = zeros(Int64, 4)

# Replace many elements


x[2:3] = [2,3]

println(x)

[0, 2, 3, 0]

INTRODUCTION TO JULIA
Cheatsheet
Add single element - push!(x, 1)

Add many elements - append!(x, [1,2,3])

Remove last element - pop!(x)

Create array of given type - Int64[1,2,3] , Float64[1,2,3] , etc.

Create empty array of given type - Int64[] , Float64[] , etc.

Create an array full of zeros - zeros(Int64, n)

Replace element - x[index] = value

Replace many elements - x[a:b] = [value1, value2, ...]

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"]

# Find the length of the array


l = length(x)

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]

x_sorted = sort(x) x_sorted = sort(x)


println(x_sorted) println(x_sorted)

["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

# Next run number is 16


x = 16

# Predict next run time


y = m * x + c

println(y)

29.68

INTRODUCTION TO JULIA
Vectorized operations
# Gradient and intercept
m = -0.32
c = 34.8

# Next run numbers


x = [16, 17, 18, 19, 20]

# Predict next run time


y = m * x + c

ERROR: MethodError: ...

INTRODUCTION TO JULIA
Array addition
Add scalar Add array

a = [1,2,3] a = [1,2,3]
b = [1,2,3]

# Answer we expect is [3,4,5] # Answer we expect is [2,4,6]


println(a .+ 2) println(a .+ b)

[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]

# Answer we expect is [3,4,5] # Answer we expect is [2,4,6]


println(a + 2) println(a + b)

ERROR: MethodError: ... [2, 4, 6]

INTRODUCTION TO JULIA
Array subtraction
# Subtract scalar # Subtract array
println(a .- 1) println(a .- b)

[0, 1, 2] [0, 0, 0]

# Subtract scalar # Subtract array


println(a - 1) println(a - b)

ERROR: MethodError: ... [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]

# Answer we expect is [5,10,15] # Answer we expect is [1,4,9]


println(a .* 5) println(a .* b)

[5, 10, 15] [1, 4, 9]

INTRODUCTION TO JULIA
Array multiplication
Multiply by scalar Multiply by array

a = [1,2,3] a = [1,2,3]
b = [1,2,3]

# Answer we expect is [5,10,15] # Answer we expect is [1,4,9]


println(a * 5) println(a * b)

[5, 10, 15] ERROR: MethodError: ...

INTRODUCTION TO JULIA
Array division
# Divide by scalar # Divide by array
println(a ./ 2) println(a ./ b)

[0.5, 1.0, 1.5] [1.0, 1.0, 1.0]

# Divide by scalar # Divide by array


println(a / 2) println(a / b)

[0.5, 1.0, 1.5] 0.071 0.142 0.214


0.142 0.285 0.428
0.214 0.428 0.642

INTRODUCTION TO JULIA
Vectorized operations
# Gradient and intercept
m = -0.32
c = 34.8

# Next run numbers


x = [16, 17, 18, 19, 20]

# Predict next run time


y = m * x + c

INTRODUCTION TO JULIA
Vectorized operations
# Gradient and intercept
m = -0.32
c = 34.8

# Next run numbers


x = [16, 17, 18, 19, 20]

# Predict next run time


y = m .* x .+ c

println(y)

[29.68, 29.36, 29.04, 28.72, 28.40]

INTRODUCTION TO JULIA
Cheatsheet
For arrays a and b

Operation Scalar example Array example

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy