= "secret123"
user_input = (user_input == "secret123") password_correct
true
Applied Optimization with Julia
Imagine you’re teaching a computer to make decisions. Just like we compare things in everyday life (“Is it raining?”, “Do I have enough money?”), computers need ways to compare values and make choices. This tutorial will show you how to help computers make these comparisons!
Follow the instructions, write your code in the designated code blocks, and execute the corresponding code cell.
In everyday life, we make comparisons all the time:
In Julia, we use special symbols to make these comparisons. Comparisons return a boolean value (true or false). You can use these directly or store them in a variable. For example:
= "secret123"
user_input = (user_input == "secret123") password_correct
true
= 75
coffee_temp = (coffee_temp <= 70) temperature_safe
false
The following are potential real-world examples:
# Do I have enough money?
= (wallet >= price)
enough_money # Is the person an adult?
= (age >= 18)
is_adult # Is this person Elio?
= (name == "Elio") same_name
Here are all the comparison operators:
Symbol | Meaning | Real-world Example |
---|---|---|
== |
Equal to | Is my password correct? |
!= |
Not equal to | Is this a different person? |
< |
Less than | Is it colder than freezing? |
> |
Greater than | Do I have more than $10? |
<= |
Less than or equal to | Can this ride fit in my garage? |
>= |
Greater than or equal to | Am I old enough to vote? |
Let’s try some examples:
# Temperature comparison
= 25
temp = (temp <= 0)
is_freezing println("Is it freezing? $is_freezing")
Is it freezing? false
# Price comparison
= 50
budget = 45
price = (budget >= price)
can_afford println("Can I afford it? $can_afford")
Can I afford it? true
Compare if 10 is greater than 5 and store the result in comparison1
.
# YOUR CODE BELOW
# Test your answer
@assert comparison1 == true
println("comparison1 is ", comparison1)
Define variables x
with value "Hello"
and y
with value "world"
. Compare if x
is not equal to y
and store the result in comparison2
.
# YOUR CODE BELOW
# Test your answer
@assert x == "Hello"
@assert y == "world"
@assert comparison2 == true
println("Comparison2 is ", comparison2)
Sometimes we need to combine multiple comparisons in our code, just like in real life decisions:
Julia uses three main logical operators:
# AND (&&): Both conditions must be true
= (is_weekend && homework_done)
can_go_to_party
# OR (||): At least one condition must be true
= (is_raining || forecast_says_rain)
need_umbrella
# NOT (!): Opposite of the condition
= !is_open is_closed
Real-world Examples:
# Can I buy this game?
= 25
age = 60
money = 50
game_price = (age >= 18) && (money >= game_price)
can_buy println("Can I buy the game? $can_buy")
Can I buy the game? true
# Should I wear a coat?
= 5
temperature = true
is_raining = (temperature < 10) || is_raining
need_coat println("Should I wear a coat? $need_coat")
Should I wear a coat? true
Think of those logical operators as follows:
&&
(AND) as a strict condition - everything must be perfect||
(OR) as a lenient condition - any good reason is enough!
(NOT) as the opposite of somethingUse the AND
operator to check if 10
is greater than 5
and hello
is equal to hello
. Store the result in logic1
.
# YOUR CODE BELOW
# Test your answer
@assert logic1 == true
println("logic1 is ", logic1)
Use the OR
operator to check if 10
is less than 5
or hello
is equal to hello
. Store the result in logic2
.
# YOUR CODE BELOW
# Test your answer
@assert logic2 == true
println("logic2 is ", logic2)
Julia uses short-circuit evaluation for &&
and ||
operators. This means that the second operand is only evaluated if necessary.
Check whether 10
is greater than 5
and store the result in logic3
. Then, use the NOT
operator to invert logic3
. Store the result in logic4
.
# YOUR CODE BELOW
# Test your answer
@assert logic3 == true
@assert logic4 == false
println("logic3 is ", logic3," and logic4 is", logic4)
Julia has a neat feature that lets you write comparisons the way you think about them:
# Instead of writing:
>= 13 && age <= 19 # Is age between 13 and 19?
age
# You can write:
13 <= age <= 19 # Much more natural!
Real-world examples:
# Is the body temperature normal?
= (36.5 <= body_temp <= 37.5)
normal_temperature
# Is the current hour within working hours?
= (9 <= current_hour < 17) working_hours
Check if x
is between 1
and 10
(exclusive) using a chained comparison. Store the result in chained_comparison
.
= 5
x # YOUR CODE BELOW
# Test your answer
@assert chained_comparison == true
println("chained_comparison is ", chained_comparison)
Excellent work! You’ve completed the tutorial on Comparisons and logical operators in Julia. You’ve learned to compare values and use logical operators to combine or invert boolean values. Experiment with the code, try different operations, and understand how Julia handles logic. 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.