for product in ["apple", "banana", "cherry"]
println("We sell: $product")
endWe sell: apple
We sell: banana
We sell: cherry
Applied Optimization with Julia
After this lecture, you will:
= operatorYou can use the typeof function to check the type of a variable.
[ and ] operatorsYou can use the push! function to add elements to a vector or the pop! function to remove elements from a vector.
== checks if two values are equal!= checks if two values are not equal< and > check if one value is smaller or greater than the other<= and >= also allow the two values to be equal&& is true if both conditions are true|| is true if at least one of two conditions is truefor loop repeats code for a fixed number of timeswhile loop repeats code as long as a condition is truefor product in ["apple", "banana", "cherry"]
println("We sell: $product")
endWe sell: apple
We sell: banana
We sell: cherry
if executes code if a condition is trueelseif checks a further condition if the previous one was falseelse executes code if none of the conditions were trueConditionals are not loops: they decide whether code runs, while loops decide how often it runs.
keys and values return all keys and valuesstudent_ids = Dict("Elio" => 1001, "Bob" => 1002)
println("Elio's ID: ", student_ids["Elio"])Elio's ID: 1001
Later in the course, we will use dictionaries to store and look up the data of our optimization models.
You can ask questions anytime in class or via email!
Question: Do we have to write all code ourselves?
DataFrames.jl for tables or Plots.jl for chartsPkg installs them for us:using Pkg
Pkg.add("DataFrames")DataFrames.jl stores tabular data, like a spreadsheet in codeusing DataFrames
sales = DataFrame(
product = ["apple", "banana", "cherry"],
price = [0.50, 0.30, 2.00],
sold = [120, 90, 30]
)| Row | product | price | sold |
|---|---|---|---|
| String | Float64 | Int64 | |
| 1 | apple | 0.5 | 120 |
| 2 | banana | 0.3 | 90 |
| 3 | cherry | 2.0 | 30 |
The remaining time we will already start working on this week’s tutorials.
Lauwens, B., & Downey, A. B. (2019). Think Julia: How to think like a computer scientist (First edition). O’Reilly®. Link to the free book website.
For more interesting literature to learn more about Julia, take a look at the literature list of this course.