Intermediate Julia
Anthony Markham
Quantitative Developer
stock = ["AAPL", 131.86, 100000]
println(stock[1])
AAPL
# Dictionary definition
stock = Dict("ticker" => "AAPL", "price" => 131.86)
println(stock["ticker"])
AAPL
stock = Dict("ticker" => "AAPL", "price" => 131.86)
Dict{String, Any} with 2 entries:
"ticker" => "AAPL"
"price" => 131.86
stock_typed = Dict{String, Integer}("ticker" => "AAPL", "price" => 131.86)
MethodError: Cannot `convert` an object of type String to an object of type Integer
stock_typed = Dict{String, Any}("ticker" => "AAPL", "age" => 131.86)
Dict{String, Any} with 2 entries:
"ticker" => "AAPL"
"age" => 131.86
stock = Dict("ticker" => "AAPL", "price" => 131.86)
for i in stock
println(i)
end
Pair{String, Any}("ticker", "AAPL")
Pair{String, Any}("price", 131.86)
keys()
and values()
return the keys and values of a dictionary.for i in keys(stock)
println(i)
end
ticker
price
for (ticker, price) in stock
println(ticker, " ", price)
end
ticker AAPL
price 131.86
get()
to get the value for a given key.get(dictionary_name, dictionary_key, default_value)
stock = Dict("ticker" => "AAPL", "price" => 131.86)
println(get(stock, "ticker", "Key not found."))
AAPL
# Add a new key
stock["volume"] = 62128300
println(stock)
Dict{String, Any}("ticker" => "AAPL", "price" => 131.86, "volume" => 62128300)
# Modify an existing value
stock["price"] = 125.27
println(stock)
Dict{String, Any}("ticker" => "AAPL", "price" => 125.27, "volume" => 62128300)
Intermediate Julia