Data Analysis with Python: Numpy Operations

It says I chose the incorrect answer, for the exercise before moving to the next lesson. However, I ran the code separately to make sure I was wrong, but it turns out I was right.

“What is the value of a after you run the following code?”
a = np.arange(5)
a + 20
[20, 21, 22, 24, 24]
[0, 1, 2, 3, 4]
[25, 26, 27, 28, 29]

The answer that it accepts is the second, [0, 1, 2, 3, 4]. This is cool but the code has a + 20, which I automatically read as “array([20, 21, 22, 23, 24])”. I tested it by running it separately to make sure and it returned “array([20, 21, 22, 23, 24])”.

Maybe I am still wrong and not seeing it. That’s completely fine by me, but if I’m not wrong I don’t want other users to go by and automatically think that [0, 1, 2, 3, 4] is the correct answer and not question it.

1 Like

This is the key phrase

3 Likes

Similar problem in vanilla Python:

a = 0
a + 40
print(a)

What is the output and why is it not 40?

3 Likes

Ah I see. Thank you for this. I guess running it in colab in separate code lines did me a little dirty. Running the code all in the same code section in colab ended up with the correct answer after I ran the vanilla one. This makes complete sense now that I see a + 20 is not saying that’s what a is equal to. However, running in separate lines on colab definitely will make it where its similar to a += 20, which is where I got confused. Thank you all for the help!

1 Like

Yes, I knew that part. I see now where I was running into my problem. I wish it would let me add screenshots to show where my problem was in colab, but outside of colab running it all as one code instead of sections of code I see where I am wrong. Running it in colab in two different code sections it makes the a + 20 act as a += 20. Running in the same code block together it runs as normal though. Starting to not like colab for that reason now lol :sweat_smile:

1 Like

Also, thank you for replying both you and Jagaya responded quickly to this and I appreciate that a lot.

1 Like

it should never run as a +=. You will see the output of the sum, but it should never be saved back into a.

1 Like

That’s fine. I understand it shouldn’t but it did. I try added screenshots to show it from Google Colab, but freecodecamp denied me to upload them. I can see if it’ll let me today.

1 Like

Works as it should for me:
image

1 Like

1 Like

That screenshot does not show a as modified. Try print(a).

1 Like

Bruh I’ve got it figured out now. I was just replying to you this morning out of being nice. I understand what’s going on as of this morning. That was in colab and just running it as normal, which in return messed me up because I got complacent using colab. Using Sublime Text Editor for the same thing is where I found the mistake. I’m not trying to say you’re wrong in fact I’m agreeing with what should happen and understanding now why it didn’t. Because of complacency and trying to move too fast on my part. Thank you for the replies though and trying to help out…

1 Like

I’m not only telling you what should happen but what is actually happening. In your screenshot, a is not modified by the command a + 20.

1 Like

I understand what’s happening. Run just
a = numpy.arange(5)
a + 20
in colab. It’ll automatically print out a + 20, which the exercise question is what is the value of a. I understand now why I was being stupid yesterday and got complacent using colab. because when I run just though two lines of code colab prints out the last one. You are correct, if I run print(a) at the bottom it shows what a really is. However, yesterday when I was running this I went stupid and didn’t realize it until after I made the post and used my text editor instead of colab. I like colab for a lot of things, but I don’t like how complacent I feel and get on it.

1 Like

What JeremyLT is telling you, is that the colab is NOT changing the value of a.
If you write a cell without it’s own output, which in it’s final line has something that “could” be put out, it will show the output.

So if you write “5” in a cell, it will show “5” below it.
It’s usefull for quick calculations like writing “16%3” will put out “1”, despite no print and no variable assigned to it.
It’s more useful for things like matplotlib, which will show graphs inline, instead of needing to write “plt.show()” and such.
But even if you write it in seperate cells, if your print(a) somewhere, it’s value will not be altered.

3 Likes

Very helpful topic, seem like i have a lot to think about. Thanks!