= 30 # A box labeled "age" containing the number 30 age
30
Applied Optimization with Julia
Welcome to this interactive Julia tutorial which introduces the basics of variables and types. Understanding variables and their types is crucial as they are the building blocks of any program. They determine how data is stored, manipulated, and how efficiently your code runs.
This script is designed to be interactive. Follow the instructions, write your code in the designated code blocks, and then execute the corresponding code. Each exercise is followed by an @assert
statement that checks your solution.
There are two ways to run the code:
The easiest way to run the code is by using VS Code. First, install the Julia and the Jupyter Extension. Then, you can open the downloaded .ipynb
files and run the code from there.
The second way is by using IJulia. Start Julia, and type using IJulia; notebook()
in the Julia prompt. This will open a new browser window where you can run the code. If you have not installed IJulia yet, you can do so by typing ]
in the Julia prompt to open the package manager, and then installing IJulia by typing add IJulia
.
Always replace ‘YOUR CODE BELOW’ with your actual code.
Think of variables as labeled containers. Just like you might label a box “Books” to store books, in programming we label our data with variable names. For example:
= 30 # A box labeled "age" containing the number 30 age
30
= "Tobias" # A box labeled "name" containing the text "Tobias" name
"Tobias"
Declare a variable named x
and assign it the value 1
.
# YOUR CODE BELOW
# Test your answer
@assert x == 1 "Check again, the value of x should be 1. Remember to assign the value directly to x."
println("Great, you have correctly assigned the value $x to the variable 'x'.")
Declare a variable named hi
and assign it the string "Hello, Optimization!"
.
# YOUR CODE BELOW
# Test your answer
@assert hi == "Hello, Optimization!" "Make sure the variable 'hi' contains the exact string \"Hello, Optimization\"!"
println("Good, the variable 'hi' now states \"$hi\".")
Just like real containers come in different types (like boxes for books, refrigerators for food, etc.), variables in Julia have different types depending on what they store:
1
, 42
, -10
3.14
, -0.5
true
, false
"Hello"
You can check what type of “container” a variable is using typeof()
. Try this:
= 25
age typeof(age) # Will show Int64 (integer type)
Int64
= 19.99
price typeof(price) # Will show Float64 (decimal number type)
Float64
Create an Integer variable answerUniverse
and set it to 42
.
# YOUR CODE BELOW
# Test your answer
@assert answerUniverse == 42 "The variable 'answerUniverse' should hold 42."
println("Great, the answer to all questions on the universe is $answerUniverse now.")
Create a Float variable money
and set it to 1.35
.
# YOUR CODE BELOW
# Test your answer
@assert money == 1.35 "The variable 'money' should hold the Float64 1.35."
println("Perfect, the you have stored $money in the variable 'money'.")
Create a Boolean variable isStudent
and set it to true
.
# YOUR CODE BELOW
# Test your answer
@assert isStudent == true "The variable 'isStudent' should be set to true."
println("Correct, you are a student now.")
Sometimes we want to specify exactly what kind of “container” we want to use. In Julia, we can do this using type annotations:
::Float64 = 98.6 # Specifically saying we want a decimal number temperature
98.6
::Int64 = 100 # Specifically saying we want a whole number count
100
Declare a variable y
with an explicit type annotation of Int64
and assign it the value 5
.
# YOUR CODE BELOW
# Test your answer
@assert y == 5 && typeof(y) == Int64 "Make sure 'y' is of type Int64 and has the value 5."
println("Great! You've created an Int64 variable 'y' with the value $y.")
String interpolation is like filling in blanks in a sentence. Instead of writing:
= "Tobias"
name = 30
age # The hard way:
= "My name is " * name * " and I am " * string(age) * " years old" message
"My name is Tobias and I am 30 years old"
We can use the $
symbol to insert variables directly into our text:
= "My name is $name and I am $age years old" message
"My name is Tobias and I am 30 years old"
It’s like having a template where Julia automatically fills in the values for you! If you have paid attention to the previous excercise, you have already seen this in action. The following example illustrates this again:
= "Julia"
language println("I'm learning $language")
I'm learning Julia
Create a string message
that says "y is [value of y]"
using string interpolation.
# YOUR CODE BELOW
# Test your answer
@assert message == "y is 5" "Make sure your string includes the correct value of y."
println("Excellent! Your interpolated string is: $message")
Congratulations! You have completed the first tutorial on Variables and Types. You’ve learned about the basics of variables, integers, floats, booleans, and strings. Continue to the next file to learn more.
You will likely find solutions to most exercises online. However, I strongly encourage you to work on these exercises independently without searching explicitly for the exact answers to the exercises. Understanding someone else’s solution is very different from developing your own. Use the lecture notes and try to solve the exercises on your own. This approach will significantly enhance your learning and problem-solving skills.
Remember, the goal is not just to complete the exercises, but to understand the concepts and improve your programming abilities. If you encounter difficulties, review the lecture materials, experiment with different approaches, and don’t hesitate to ask for clarification during class discussions.
Later, you will find the solutions to these exercises online in the associated GitHub repository, but we will also quickly go over them in next week’s tutorial. To access the solutions, click on the Github button on the lower right and search for the folder with today’s lecture and tutorial. Alternatively, you can ask ChatGPT or Claude to explain them to you. But please remember, the goal is not just to complete the exercises, but to understand the concepts and improve your programming abilities.