FAQ: Introduction to NumPy - Operations with NumPy Arrays II

This community-built FAQ covers the “Operations with NumPy Arrays II” exercise from the lesson “Introduction to NumPy”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Data Science

Introduction to Statistics with NumPy

FAQs on the exercise Operations with NumPy Arrays II

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Why does NumPy not show significant digits (unless of course I assume there is a way for you ask for them to be shown). In the test exercise, the numbers were not even rounded to the nearest number, they just ignored the decimal.

Why am I getting this error on the website with a red box?

import numpy as np

test_1 = np.array([92, 94, 88, 91, 87])
test_2 = np.array([79, 100, 86, 93, 91])
test_3 = np.array([87, 85, 72, 90, 92])
test_3_fixed = test_3 + 2

# Let’s find the average of each student’s test scores to calculate their final grade for the semester.
# Start by adding the three arrays together and save the answer to the variable total_grade.
# Remember to use the fixed scores for test three.
total_grade = test_1 + test_2 + test_3_fixed

# Now, divide total_grade by the number of tests taken to find the average score for each student.
# Save the answer to the variable final_grade.
final_grade = total_grade / 3

# Print the results of final_grade to the terminal.

It’s probably not Numpy’s issue, but an issue of the Python version used in this exercise (it seems to be 2.7). In python 2.x, the operator / is floor division (which is the same as //) when both args are int. See the answers of the following Stack Overflow question:

To get floating point results, make either of args to float.

final_grade = total_grade / 3.0
# [ 86.66666667  93.66666667  82.66666667  92.          90.66666667]

As the error message says, it’s probably because a Non-ASCII character is used on line 8. It seems that apostrophe included in Let’s and student’s is the Non-ASCII character.

Why would it care what’s in a comment block? The point of a comment block is for it to be ignored.

1 Like

Maybe I need a deeper understanding of how Python’s compiler works to answer your question correctly, but I don’t understand that much. However, I think that the error occurred during the earlier lexical analysis stage, as comments are not tokens and will be ignored by a parser.

Python Comments

Everything that comes after # is ignored. So, we can also write the above program in a single line as:

print('Hello world') #printing a string

I am a
multiline comment!

print("Hello World")

Here, the multiline string isn’t assigned to any variable, so it is ignored by the interpreter. Even though it is not technically a multiline comment, it can be used as one.


Comments are ignored by parser. But before the program (tokens) is passed to the parser, there is a lexical analysis where the comments are not ignored.

The important point is that the lexical analyzer distinguishes comments in a program and let parser ignore them, but the lexical analyzer itself doesn’t ignore the comments.

Tough the default encoding which Python 2.7 uses is ASCII, it seems that we can declare the character code with a comment on the first or second line of the script (see section 2.1.4 of the document). For example if you put the following comment on the first line of your script, the SyntaxError will not be raised.

# -*- coding: utf-8 -*-

In Python 3, this declaration is not needed since the default encoding is UTF-8 (see the document of the lexical analysis of Python 3).

What would be a way to count the amount of input arrays into the calculation of excercise Nr. 6 in “Operations with NumPy Arrays II”?

It says: “divide total_grade by the number of tests taken” which I can write as a plain 3. but what if I want to build it in a way that it checks it’s length?

import numpy as np

test_1 = np.array([92, 94, 88, 91, 87])
test_2 = np.array([79, 100, 86, 93, 91])
test_3 = np.array([87, 85, 72, 90, 92])
test_3_fixed = test_3 + 2

total_grade = test_1 + test_2 + test_3_fixed
final_grade = total_grade / 3

You’d need some way of counting how many arrays are added together. You’d probably want to restructure a few parts of the code in this case to generalise it to any number of test inputs.

Perhaps adding the original arrays to a list or a dictionary or something might be the neatest, a 2D numpy array would be nice but it’d be less flexible to change.

The line test_1 + test_2 +test_3_fixed would then need to be edited in some way e.g. with a loop or a built-in.

1 Like