Like any regular Python array, you can access the contents of a NumPy array using indexing. The indexing method, which uses square brackets, lets you see one item in a list or a particular part of a list. If you try to retrieve an item from a NumPy array using round brackets, you will encounter an error.
By completing and submitting this form, you agree that Career Karma Platform, LLC may deliver or cause to be delivered
information, advertisements, and telemarketing messages regarding their services by email, call, text, recording, and
message using a telephone system, dialer, automated technology or system, artificial or prerecorded voice or message
device to your email and/or telephone number(s) (and not any other person’s email or telephone number) that you
entered. Consent is not a condition of receiving information, receiving Career Karma services, or using the website,
and you may obtain information by emailing
[email protected]
. Message & Data rates may
apply. Message frequency may vary. Text STOP to unsubscribe.
Terms of Service
Privacy Policy
govern the processing and handling of your data.
By completing and submitting this form, you agree that Career Karma Platform, LLC may deliver or cause to be delivered
information, advertisements, and telemarketing messages regarding their services by email, call, text, recording, and
message using a telephone system, dialer, automated technology or system, artificial or prerecorded voice or message
device to your email and/or telephone number(s) (and not any other person’s email or telephone number) that you
entered. Consent is not a condition of receiving information, receiving Career Karma services, or using the website,
and you may obtain information by emailing
[email protected]
. Message & Data rates may
apply. Message frequency may vary. Text STOP to unsubscribe.
Terms of Service
Privacy Policy
govern the processing and handling of your data.
exam_grade = student(-1)
print("This student earned {} on their exam.".format(exam_grade))
average_grade = np.sum(student) / student.size
print("This student received an average {} mark on each test.".format(average_grade))
The first line of code finds the last item in our list. This item represents the mark a student received on their final exam. The next line displays the grade the student earned on their exam on the Python console. We then calculate the average grade the student earned and print that value to the console.
Let’s run our code:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'numpy.ndarray' object is not callable
Our code returns an error.
The Solution
When we try to access the last item in our student list, we use:
exam_grade = student(-1)
print("This student earned {} on their exam.".format(exam_grade))
This line of code causes an error because you cannot use round brackets to access an item from a list. You must use square brackets. To fix our earlier code, we can replace the round brackets with square brackets:
exam_grade = student[-1]
print("This student earned {} on their exam.".format(exam_grade))
Our code now returns:
This student earned 91 on their exam.
This student received an average 79.8 mark on each test.
Our program retrieves the grade the student earned on their final exam. Then our program calculates the average grade a student received. Our program works as intended.
Conclusion
The Python
'numpy.ndarray' object is not callable
error is caused by using round brackets instead of square brackets to access an item from a NumPy array. To fix this error, use array_name[index_number] syntax to access an item from an array.
Do you want to learn more about coding in Python? Read our
How to Learn Python
guide. This guide contains expert learning advice to help you expand your knowledge of the Python programming language.