Bird
Raised Fist0
NumPydata~10 mins

Scalar operations on arrays in NumPy - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Scalar operations on arrays
Start with array
↓
Choose scalar operation
↓
Apply operation element-wise
↓
Create new array with results
↓
Use or display new array
We start with an array, pick a scalar operation like add or multiply, apply it to each element, and get a new array with the results.
Execution Sample
NumPy
import numpy as np
arr = np.array([1, 2, 3])
result = arr * 3
print(result)
Multiply each element of the array by 3 and print the new array.
Execution Table
StepActionArray StateScalarResulting Array
1Create array[1, 2, 3]-[1, 2, 3]
2Choose scalar operation[1, 2, 3]3 (multiply)[1, 2, 3]
3Multiply each element by 3[1, 2, 3]3[3, 6, 9]
4Print result[1, 2, 3]3[3, 6, 9]
💡 All elements multiplied by scalar 3, operation complete.
Variable Tracker
VariableStartAfter Step 1After Step 3Final
arr-[1, 2, 3][1, 2, 3][1, 2, 3]
scalar--33
result--[3, 6, 9][3, 6, 9]
Key Moments - 2 Insights
Why does the original array 'arr' not change after the operation?
Because scalar operations create a new array with results, they do not modify the original array 'arr' (see execution_table step 3 and variable_tracker).
How does numpy apply the scalar operation to the array elements?
Numpy applies the scalar operation element-wise automatically, multiplying each element by the scalar one by one (see execution_table step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'result' after step 3?
A[3, 6, 9]
B[1, 2, 3]
C[4, 5, 6]
D3
💡 Hint
Check the 'Resulting Array' column at step 3 in the execution_table.
At which step does the scalar get applied to the array elements?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for the step where the 'Action' says 'Multiply each element by 3' in the execution_table.
If we change the scalar from 3 to 2, how would the 'result' array change after step 3?
A[3, 6, 9]
B[2, 4, 6]
C[1, 2, 3]
D[6, 12, 18]
💡 Hint
Multiply each element of [1, 2, 3] by 2 instead of 3, as shown in variable_tracker.
Concept Snapshot
Scalar operations on arrays:
- Apply a scalar (single number) to each element of a numpy array.
- Operations include add, subtract, multiply, divide.
- Result is a new array; original array stays unchanged.
- Numpy handles element-wise operation automatically.
- Example: arr * 3 multiplies every element by 3.
Full Transcript
This visual execution shows how scalar operations work on numpy arrays. We start with an array of numbers. Then we pick a scalar number to operate with, like 3. Numpy multiplies each element of the array by 3, creating a new array with the results. The original array remains the same. This process happens step-by-step: creating the array, choosing the scalar, applying the operation element-wise, and printing the result. The variable tracker shows how variables change or stay the same. The key moments clarify why the original array does not change and how numpy applies the operation to each element automatically.

Practice

(1/5)
1. What happens when you add a scalar value to a NumPy array?
arr = np.array([1, 2, 3])
result = arr + 5
easy
A. 5 is added only to the first element
B. The array becomes a scalar value 5
C. 5 is added to each element of the array
D. An error occurs because you cannot add a scalar to an array

Solution

  1. Step 1: Understand scalar addition on arrays

    Adding a scalar to a NumPy array adds that scalar to every element individually.
  2. Step 2: Apply addition to each element

    Each element in [1, 2, 3] gets 5 added, resulting in [6, 7, 8].
  3. Final Answer:

    5 is added to each element of the array -> Option C
  4. Quick Check:

    Scalar + array = element-wise addition [OK]
Hint: Scalar operations apply to every element individually [OK]
Common Mistakes:
  • Thinking scalar adds only to first element
  • Expecting an error when adding scalar
  • Confusing scalar with array addition
2. Which of the following is the correct syntax to multiply every element of a NumPy array arr by 3?
easy
A. arr * 3
B. arr * [3]
C. arr * arr[3]
D. arr * (3,)

Solution

  1. Step 1: Identify scalar multiplication syntax

    Multiplying an array by a scalar uses the * operator with a number, like arr * 3.
  2. Step 2: Check other options

    arr * [3] tries to multiply by a list, arr * arr[3] accesses an element (may cause error if index out of range), arr * (3,) is a tuple, not scalar.
  3. Final Answer:

    arr * 3 -> Option A
  4. Quick Check:

    Scalar multiplication uses * scalar [OK]
Hint: Use * with a number, not list or tuple [OK]
Common Mistakes:
  • Using list or tuple instead of scalar
  • Accessing array element incorrectly
  • Confusing scalar with array
3. What is the output of the following code?
import numpy as np
arr = np.array([2, 4, 6])
result = arr - 1
print(result)
medium
A. [3 5 7]
B. [1 3 5]
C. [2 4 6]
D. Error: unsupported operand

Solution

  1. Step 1: Understand scalar subtraction on array

    Subtracting 1 from each element in [2, 4, 6] results in [1, 3, 5].
  2. Step 2: Confirm output matches expected array

    The print statement outputs the array as [1 3 5] without commas.
  3. Final Answer:

    [1 3 5] -> Option B
  4. Quick Check:

    Array - scalar subtracts from each element [OK]
Hint: Subtract scalar from each element for result [OK]
Common Mistakes:
  • Expecting commas in printed array
  • Adding instead of subtracting
  • Thinking operation causes error
4. The following code throws an error. What is the cause?
import numpy as np
arr = np.array([1, 2, 3])
result = arr + '5'
print(result)
medium
A. Cannot add string scalar to numeric array
B. Array is not defined
C. Missing import statement
D. Syntax error in addition

Solution

  1. Step 1: Identify data types involved in addition

    arr is numeric array, '5' is a string scalar. NumPy cannot add string to numbers.
  2. Step 2: Understand error cause

    Adding string to numeric array causes TypeError because types mismatch.
  3. Final Answer:

    Cannot add string scalar to numeric array -> Option A
  4. Quick Check:

    Scalar type must match array type [OK]
Hint: Scalar must be number, not string, for numeric arrays [OK]
Common Mistakes:
  • Thinking import is missing
  • Confusing syntax error with type error
  • Assuming array is undefined
5. You have a NumPy array arr = np.array([10, 20, 30, 40]). You want to reduce each element by 10% using scalar operations. Which code correctly does this?
hard
A. arr + 0.9
B. arr - 0.1
C. arr / 10
D. arr * 0.9

Solution

  1. Step 1: Understand reducing by 10%

    Reducing by 10% means multiplying by 90% or 0.9.
  2. Step 2: Apply scalar multiplication

    Multiplying arr by 0.9 scales each element down by 10%.
  3. Step 3: Check other options

    Subtracting 0.1 reduces by fixed amount, dividing by 10 reduces by 90%, adding 0.9 increases values.
  4. Final Answer:

    arr * 0.9 -> Option D
  5. Quick Check:

    Multiply by 0.9 to reduce by 10% [OK]
Hint: Multiply by 0.9 to reduce by 10% [OK]
Common Mistakes:
  • Subtracting 0.1 instead of 10%
  • Dividing instead of multiplying
  • Adding instead of reducing
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard