Scalar operations let you add, subtract, multiply, or divide every number in an array by a single number easily. This helps you change all values quickly without writing loops.
Scalar operations on arrays in NumPy
import numpy as np array = np.array([values]) # Add a scalar result = array + scalar # Subtract a scalar result = array - scalar # Multiply by a scalar result = array * scalar # Divide by a scalar result = array / scalar
Scalar means a single number, not a list or array.
Operations apply to every element in the array automatically.
import numpy as np array = np.array([1, 2, 3]) result = array + 5 print(result)
import numpy as np array = np.array([]) result = array * 10 print(result)
import numpy as np array = np.array([10]) result = array - 3 print(result)
import numpy as np array = np.array([2, 4, 6]) result = array / 2 print(result)
This program shows how to add, multiply, and divide all elements in a numpy array by a scalar number. It prints the array before and after each operation.
import numpy as np # Create an array of exam scores exam_scores = np.array([70, 85, 90, 60, 75]) print("Original exam scores:", exam_scores) # Add 5 bonus points to all scores bonus_points = 5 updated_scores = exam_scores + bonus_points print("Scores after adding bonus points:", updated_scores) # Multiply all scores by 1.1 to give a 10% increase increased_scores = updated_scores * 1.1 print("Scores after 10% increase:", increased_scores) # Divide all scores by 2 to find half scores half_scores = increased_scores / 2 print("Half of the increased scores:", half_scores)
Time complexity is O(n) where n is the number of elements, because each element is processed once.
Space complexity is O(n) for the new array created by the operation.
Common mistake: Trying to add a list or array instead of a scalar causes element-wise addition, not scalar operation.
Use scalar operations when you want to apply the same change to every element quickly without loops.
Scalar operations apply a single number operation to every element in an array.
They are simple and fast ways to change all values at once.
Remember: the scalar is a single number, not a list or array.
Practice
arr = np.array([1, 2, 3])
result = arr + 5Solution
Step 1: Understand scalar addition on arrays
Adding a scalar to a NumPy array adds that scalar to every element individually.Step 2: Apply addition to each element
Each element in [1, 2, 3] gets 5 added, resulting in [6, 7, 8].Final Answer:
5 is added to each element of the array -> Option CQuick Check:
Scalar + array = element-wise addition [OK]
- Thinking scalar adds only to first element
- Expecting an error when adding scalar
- Confusing scalar with array addition
arr by 3?Solution
Step 1: Identify scalar multiplication syntax
Multiplying an array by a scalar uses the * operator with a number, like arr * 3.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.Final Answer:
arr * 3 -> Option AQuick Check:
Scalar multiplication uses * scalar [OK]
- Using list or tuple instead of scalar
- Accessing array element incorrectly
- Confusing scalar with array
import numpy as np arr = np.array([2, 4, 6]) result = arr - 1 print(result)
Solution
Step 1: Understand scalar subtraction on array
Subtracting 1 from each element in [2, 4, 6] results in [1, 3, 5].Step 2: Confirm output matches expected array
The print statement outputs the array as [1 3 5] without commas.Final Answer:
[1 3 5] -> Option BQuick Check:
Array - scalar subtracts from each element [OK]
- Expecting commas in printed array
- Adding instead of subtracting
- Thinking operation causes error
import numpy as np arr = np.array([1, 2, 3]) result = arr + '5' print(result)
Solution
Step 1: Identify data types involved in addition
arr is numeric array, '5' is a string scalar. NumPy cannot add string to numbers.Step 2: Understand error cause
Adding string to numeric array causes TypeError because types mismatch.Final Answer:
Cannot add string scalar to numeric array -> Option AQuick Check:
Scalar type must match array type [OK]
- Thinking import is missing
- Confusing syntax error with type error
- Assuming array is undefined
arr = np.array([10, 20, 30, 40]). You want to reduce each element by 10% using scalar operations. Which code correctly does this?Solution
Step 1: Understand reducing by 10%
Reducing by 10% means multiplying by 90% or 0.9.Step 2: Apply scalar multiplication
Multiplying arr by 0.9 scales each element down by 10%.Step 3: Check other options
Subtracting 0.1 reduces by fixed amount, dividing by 10 reduces by 90%, adding 0.9 increases values.Final Answer:
arr * 0.9 -> Option DQuick Check:
Multiply by 0.9 to reduce by 10% [OK]
- Subtracting 0.1 instead of 10%
- Dividing instead of multiplying
- Adding instead of reducing
Ready to prove it?
Pass the test and this pattern moves unknown
