NumPy Simple Arithmetic
🔢 NumPy Simple Arithmetic
NumPy allows you to perform fast, element-wise arithmetic operations on arrays without writing loops.
This is one of the biggest advantages of NumPy over normal Python lists.
1️⃣ Import NumPy
2️⃣ Create NumPy Arrays
3️⃣ Addition ➕
Output
📌 Performed element-wise
4️⃣ Subtraction ➖
Output
5️⃣ Multiplication ✖
Output
📌 This is NOT matrix multiplication (that uses dot() or @)
6️⃣ Division ➗
Output
✔ Result is float
7️⃣ Power / Exponentiation 🔺
Output
8️⃣ Arithmetic with a Scalar ⭐
Output
📌 This is called broadcasting
9️⃣ Using NumPy Functions (Recommended) ⭐
✔ Cleaner
✔ Interview-friendly
🔟 Arithmetic on 2D Arrays
Output
1️⃣1️⃣ Comparison with Python Lists ❌ vs ✅
Python List ❌
NumPy Array ✅
1️⃣2️⃣ Common Mistakes ❌
❌ Different array sizes (shape mismatch)
❌ Expecting list-style behavior
❌ Confusing * with matrix multiplication
❌ Division by zero
📌 Interview Questions & MCQs
Q1. NumPy arithmetic is performed:
A) Row-wise
B) Column-wise
C) Element-wise
D) Random
✅ Answer: C
Q2. What does a * b do in NumPy?
A) Matrix multiplication
B) Element-wise multiplication
C) Error
D) Addition
✅ Answer: B
Q3. Which operator is used for power?
A) ^
B) **
C) pow()
D) exp()
✅ Answer: B
Q4. What is broadcasting?
A) Looping
B) Type casting
C) Scalar expansion
D) Sorting
✅ Answer: C
🔥 Real-Life Use Cases
✔ Data analysis
✔ Machine learning calculations
✔ Scientific computing
✔ Image processing
✔ Financial modeling
✅ Summary
-
NumPy supports fast element-wise arithmetic
-
Operations work on arrays of same shape
-
Scalars are broadcast automatically
-
Much faster than Python lists
-
Core concept for Data Science & ML
