NumPy II
Array Arithmetic
Learn how to perform element-wise arithmetic on NumPy arrays — adding, subtracting, multiplying, and more — without writing a single loop.
NumPy arrays support arithmetic directly — operations apply to every element at once, with no loop needed.
Adding a number to an array shifts every element by that amount:
Python
Output
The same works for -, *, /, and **. The value on the right applies to every element.
What will be the output?
Python
Two arrays of the same shape can be combined element by element:
Python
Output
What will be the output?
Python
The ** operator raises each element to a power:
Python
Output
What will be the output?
Python
Division always returns floats, even when values divide evenly:
Python
Output
What will be the output?
Python
You can chain operations — NumPy follows standard order of evaluation just like Python.
What will be the output?
Python
What will be the output?
Python