Powers, Floor Division, and Remainders
Introduction
Welcome back to Vectorized Operations. We’re now on Lesson 2 of 5. In the previous lesson, we used +, -, *, and / to work on every value in an array at once. Now we’ll add three more operators: ** for powers, // for floor division, and % for remainders. By the end, we’ll use them to calculate tile areas, split video lengths into hours and leftover minutes, and turn a count into a repeating weekly pattern.
Raise Values to a Power with **
When we need to square several numbers, we could calculate each one separately. With NumPy, we can use ** once and apply it to every element in the array. Let’s use tile side lengths as an example: the area of each square tile is its side length raised to the second power. As always, we import NumPy as np first; the later snippets in this lesson use the same import.
Here, sides ** 2 squares each value, returning a new array. The print statement displays those areas:
For example, the first value is , and the last is . We wrote one expression, and NumPy applied it to all four side lengths.
Split Quantities with Floor Division //
Sometimes a quantity contains complete units plus a partial unit. For example, a video that lasts 135 minutes contains 2 full hours. The // operator divides and then rounds the result down to the nearest whole number. We can apply it to an array of video lengths in minutes:
Each number is divided by 60 and rounded down. Thus, 45 minutes gives 0 full hours, while 200 minutes gives 3. The printed array contains one whole-hour count for each video:
The hours array keeps the same element order as minutes, making each result easy to match to its video.
For positive numbers like these, rounding down simply drops the fractional part. For negative numbers it does not: -5 // 2 is -3, because -2.5 rounded down is -3. This is different from the astype conversion you met earlier, which drops the decimal part and would turn -2.5 into -2. All the quantities in this lesson are positive, so // gives the number of complete units.

