NumPy Array Length vs Size Comparison

Quick NumPy question 👀 What will this print? meals = np.array([[1, 2], [3, 4], [5, 6]]) print(len(meals) == meals.size) 🧠 Step-by-step: In this line: meals is a NumPy array that looks like: [[1, 2], [3, 4], [5, 6]] So we have: • 3 rows • 2 columns 🔹 Step 1: len(meals) In NumPy: ➡️ len() returns the number of rows So: len(meals) = 3 🔹 Step 2: meals.size returns the total number of elements in the array 3 × 2 = 6 → meals.size = 6 🔹 Step 3: Comparison: 3 == 6 → False ✅ Final Output: False 💡 Note (important): If it was a 1D array: np.array([1, 2, 3]) Then: len = size = 3 Because it’s a 1D array, len() returns the number of elements (same as size), since there is only one dimension. 📌 Summary: • In 1D arrays: len() == size • In 2D arrays: • len() → number of rows • size → total elements #Python #NumPy #DataScience #CodingTips #LearnPython

To view or add a comment, sign in

Explore content categories