# Introduction to Julia course - loops

**URL:** <https://discourse.julialang.org/t/introduction-to-julia-course-loops/117522>\
**Category:** New to Julia\
**Created:** [July 26, 2024, 3:52pm UTC](https://discourse.julialang.org/t/introduction-to-julia-course-loops/117522 "2024-07-26T15:52:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daves\_Cordova](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daves_cordova/32/210931_2.png) [@Daves\_Cordova](https://discourse.julialang.org/u/Daves_Cordova)\
**Post date:** [July 26, 2024, 3:52pm UTC](https://discourse.julialang.org/t/introduction-to-julia-course-loops/117522/1 "2024-07-26T15:52:42Z")

</div>

The course in Julia Academy have the following exercise:

_##exercise 4.1_  
_##Loop over integers between 1 and 100 and print their squares_

I have made the code:  
for a in 1:100  
A = a^2  
end  
a

Nothwisthanfd, I did not achieve to reach the towo following exercises:

_##Add to the code above a bit to create a dictionary, squares that holds integers and their squares as key, value pairs such that squares[10] == 100_  
_##Use an array comprehension to create an an array squares\_arr that stores the squares for all integers between 1 and 100._

Could you aid me to solve this in order to compreend Julia function of dictionaires?

---

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [July 26, 2024, 4:18pm UTC](https://discourse.julialang.org/t/introduction-to-julia-course-loops/117522/2 "2024-07-26T16:18:57Z")

</div>

Can you show us the things you tried that didn’t work? It may help us understand where the gaps in your understanding are.

---

<div class="post-metadata">

**Author:** ![Daves\_Cordova](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daves_cordova/32/210931_2.png) [@Daves\_Cordova](https://discourse.julialang.org/u/Daves_Cordova)\
**Post date:** [July 26, 2024, 5:04pm UTC](https://discourse.julialang.org/t/introduction-to-julia-course-loops/117522/3 "2024-07-26T17:04:04Z")

</div>

Thanks for your help. I initiated with this code, but it brings me a dicionario with only one vector:

**## Making A with a\_values**

```julia
B = hcat( collect(1:100), A)
B

```

_100×2 Matrix{Int64}:_

- 1 1\*
- 2 4\*
- 3 9\*
- 4 16\*
- 5 25\*
- 6 36\*
- 7 49\*
- 8 64\*
- 9 81\*
- 10 100\*
- 11 121\*
- 12 144\*
- 13 169\*
- ⋮ \*
- 89 7921\*
- 90 8100\*
- 91 8281\*
- 92 8464\*
- 93 8649\*
- 94 8836\*
- 95 9025\*
- 96 9216\*
- 97 9409\*
- 98 9604\*
- 99 9801\*
- 100 10000\*

**##Making a dictionario**

`squares= Dict( collect(1:100) => B[i, :] for i in 1:length( collect(1:100)))`

_Dict{Vector{Int64}, Vector{Int64}} with 1 entry: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10 … 91, 92, 93, 94, 95, 96, 97… =\> [100, 10000]_

**##testing**  
`@assert squares[10] == 100`

KeyError: key 10 not found

Stacktrace:  
[1] getindex(h::Dict{Vector{Int64}, Vector{Int64}}, key::Int64)  
@ Base .\dict.jl:498  
[2] top-level scope  
@ In[25]:1

---

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [July 26, 2024, 5:17pm UTC](https://discourse.julialang.org/t/introduction-to-julia-course-loops/117522/4 "2024-07-26T17:17:23Z")

</div>

I see you have a lot of the right ideas, just not how to put the ideas together.

I don’t like this second exercise because while the first exercise is a simple for loop, the second exercise calls for a combination of comprehensions, `Dict`s, and `Pair`s all at once.

Would you prefer I give you the answer (and explain it)? Or take you one step at a time through these three concepts? (`Pair`, then comprehension, then `Dict`.)

Edit: That feels like a silly question for me to ask… I don’t know how to teach well, so I’ll do the former 😅

One way of getting your answer is:

```julia
Dict(n => n^2 for n in 1:100)

```

Notice that:

1. The `=>` has on its left just the variable `n`. It represents a single number. In your nice attempt, you have the vector of numbers in the right hand side. And what’s happening is, your `for` loop keeps rewriting what the vector is pointing to on its left hand side.

2. The `=>` has on its right the square number: `n^2`. In your attempt, you have the call to `B[i, :]` which will result in, e.g. for `i = 2`: `B[i, :] == [2, 4]` which means that inside the dictionary, you will have the pair `collect(1:100) => [2, 4]`, when the form you are looking for is `2 => 4` instead.

3. The comprehension I made (the `for` loop inside the `Dict`) is `for n in 1:100`. I see your attempt has `i in 1:length(collect(1:100))`, which is a lot of hard work that can be simplified down. The `length(collect(1:100))` is of value `100` so you can just make that replacement. Then your `for` loop will become `1:100` which becomes like my loop.

4. Notice I haven’t used any `collect`s, you rarely need to use `collect` in Julia, and using the iteration notation like `1:100` everywhere should be fine.

5. And lastly, the dictionary: Note how it’s just a simple call with the comprehension inside.

Now, that second exercise says to add your dictionary creation after the first exercise’s answer, i.e. use `A` to create the `Dict`. So my provided answer won’t be the answer to that second exercise, I’ll let you give it a shot. 😊

Let me know if anything doesn’t make sense.

---

<div class="post-metadata">

**Author:** ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Post date:** [July 26, 2024, 5:41pm UTC](https://discourse.julialang.org/t/introduction-to-julia-course-loops/117522/5 "2024-07-26T17:41:25Z")

</div>

Maybe also worth mentioning that since they write “print their squares”, I think your answer to the first question should probably be something like

```julia
for a in 1:100
    A = a^2 
    println(A)
end

```

---

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [July 26, 2024, 5:42pm UTC](https://discourse.julialang.org/t/introduction-to-julia-course-loops/117522/6 "2024-07-26T17:42:37Z")

</div>

I had that thought too, thanks for bringing it up. I made the assumption it might’ve been fine because it seems he passed that first exercise.

But yes, `print` and `println` are invaluable Julia functions for beginners to learn.

---

<div class="post-metadata">

**Author:** ![Daves\_Cordova](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daves_cordova/32/210931_2.png) [@Daves\_Cordova](https://discourse.julialang.org/u/Daves_Cordova)\
**Post date:** [July 26, 2024, 7:28pm UTC](https://discourse.julialang.org/t/introduction-to-julia-course-loops/117522/7 "2024-07-26T19:28:57Z")

</div>

Thank you @kapple and @DanielVandH to sharing your time and knowledge with me.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 26, 2024, 9:27pm UTC](https://discourse.julialang.org/t/introduction-to-julia-course-loops/117522/8 "2024-07-26T21:27:45Z")

</div>

PS. See [PSA: how to quote code with backticks](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530)
