{"id":1773,"date":"2019-01-17T10:17:06","date_gmt":"2019-01-17T04:47:06","guid":{"rendered":"https:\/\/pynative.com\/?p=1773"},"modified":"2025-08-31T13:21:51","modified_gmt":"2025-08-31T07:51:51","slug":"python-numpy-exercise","status":"publish","type":"post","link":"https:\/\/pynative.com\/python-numpy-exercise\/","title":{"rendered":"Python NumPy Exercise: 50 Questions With Solutions (Beginner to Advanced)"},"content":{"rendered":"\n<p>NumPy, short for Numerical Python, is a fundamental library for data science. It&#8217;s used to create and manipulate <strong>multidimensional arrays<\/strong>, making it incredibly useful for numerical operations and data analysis.<\/p>\n\n\n\n<p>This article gives you <strong>50 NumPy coding practice problems with solution<\/strong> starting from fundamentals to linear algebra each with a <strong>hint, solution, and short explanation<\/strong> so you learn by doing, not just reading.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-below-topics-are-covered-in-this-exercise\">Below topics are covered in this exercise<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Basics:<\/strong> imports, creation routines, identity matrices, versioning<\/li>\n\n\n\n<li><strong>Intermediate<\/strong>: Array manipulation and common operations<\/li>\n\n\n\n<li><strong>Indexing &amp; Slicing:<\/strong> rows, columns, sub-arrays, boolean masks<\/li>\n\n\n\n<li><strong>Array Operations:<\/strong> element-wise math, dot product, normalization, stats<\/li>\n\n\n\n<li><strong>Random Numbers:<\/strong> random floats\/ints, shuffling, sorting<\/li>\n\n\n\n<li><strong>Reshaping &amp; Stacking:<\/strong> reshape, flatten, split, tile vs repeat<\/li>\n\n\n\n<li><strong>Boolean &amp; Filtering:<\/strong> conditional filtering, replace, unique counts, set ops<\/li>\n\n\n\n<li><strong>Advanced:<\/strong> diagonals, eigenvalues\/vectors, <code>solve<\/code>, inversion, structured arrays<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-how-to-use-this-guide\">How to use this guide<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Try the question first.<\/strong><\/li>\n\n\n\n<li><strong>Peek at the hint<\/strong> only if needed.<\/li>\n\n\n\n<li><strong>Write your own solution<\/strong>, then compare with the provided code.<\/li>\n\n\n\n<li><strong>Read the explanation<\/strong> and tweak inputs (shapes, dtypes) to deepen understanding.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-prerequisites\">Prerequisites<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Python 3.x<\/li>\n\n\n\n<li>NumPy installed: <code>pip install numpy<\/code><\/li>\n<\/ul>\n\n\n\n<p>Use <a href=\"https:\/\/pynative.com\/online-python-code-editor-to-execute-python-code\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Online Code Editor<\/strong><\/a> to solve the exercise.<\/p>\n\n\n\n<details class=\"wp-block-details toc has-background is-layout-flow wp-block-details-is-layout-flow\" style=\"background-color:#f6f6f6\"><summary>+ Table of Content (50 Exercises)<\/summary>\n<div class=\"wp-block-yoast-seo-table-of-contents yoast-table-of-contents\"><h2>Table of contents<\/h2><ul><li><a href=\"#h-exercise-1-create-a-1d-numpy-array-of-numbers-from-0-to-9\" data-level=\"2\">Exercise 1: Create a 1D NumPy array of numbers from 0 to 9<\/a><\/li><li><a href=\"#h-exercise-2-convert-1d-array-to-2d\" data-level=\"2\">Exercise 2: Convert 1D array to 2D<\/a><\/li><li><a href=\"#h-exercise-3-print-array-attributes\" data-level=\"2\">Exercise 3: Print Array Attributes<\/a><\/li><li><a href=\"#h-exercise-4-create-a-3-3-numpy-array-of-all-true\" data-level=\"2\">Exercise 4: Create a 3\u00d73 NumPy array of all True<\/a><\/li><li><a href=\"#h-exercise-5-extract-the-documentation-of-numpy-s-arange-function\" data-level=\"2\">Exercise 5: Extract the documentation of NumPy\u2019s arange() function<\/a><\/li><li><a href=\"#h-exercise-6-create-a-1d-array-filled-with-zeros-and-another-filled-with-ones\" data-level=\"2\">Exercise 6: Create a 1D array filled with zeros and another filled with ones<\/a><\/li><li><a href=\"#h-exercise-7-create-a-1d-array-of-10-evenly-spaced-values-between-5-and-50\" data-level=\"2\">Exercise 7: Create a 1D array of 10 evenly spaced values between 5 and 50<\/a><\/li><li><a href=\"#h-exercise-8-convert-a-python-list-into-a-numpy-array\" data-level=\"2\">Exercise 8: Convert a Python list into a NumPy array<\/a><\/li><li><a href=\"#h-exercise-9-find-the-memory-size-of-a-numpy-array-of-numbers-from-0-to-9\" data-level=\"2\">Exercise 9: Find the memory size of a NumPy array of numbers from 0 to 9<\/a><\/li><li><a href=\"#h-exercise-10-reverse-a-1d-numpy-array\" data-level=\"2\">Exercise 10: Reverse a 1D NumPy array<\/a><\/li><li><a href=\"#h-exercise-11-create-a-3-3-identity-matrix\" data-level=\"2\">Exercise 11: Create a 3\u00d73 identity matrix<\/a><\/li><li><a href=\"#h-exercise-12-create-a-4-4-array-and-extract-its-first-row-and-last-column\" data-level=\"2\">Exercise 12: Create a 4\u00d74 array and extract its first row and last column<\/a><\/li><li><a href=\"#h-exercise-13-extract-odd-rows-and-even-columns\" data-level=\"2\">Exercise 13: Extract Odd Rows and Even Columns<\/a><\/li><li><a href=\"#h-exercise-14-stack-arrays-horizontally\" data-level=\"2\">Exercise 14: Stack arrays horizontally<\/a><\/li><li><a href=\"#h-exercise-15-slice-the-first-two-rows-and-first-two-columns-from-a-4-4-array\" data-level=\"2\">Exercise 15: Slice the first two rows and first two columns from a 4\u00d74 array<\/a><\/li><li><a href=\"#h-exercise-16-replace-all-odd-numbers-in-a-numpy-array-with-1\" data-level=\"2\">Exercise 16: Replace all odd numbers in a NumPy array with -1<\/a><\/li><li><a href=\"#h-exercise-17-get-the-indices-of-non-zero-elements-in-an-array\" data-level=\"2\">Exercise 17: Get the indices of non-zero elements in an array<\/a><\/li><li><a href=\"#h-exercise-18-find-the-common-items-between-two-arrays\" data-level=\"2\">Exercise 18: Find the common items between two arrays<\/a><\/li><li><a href=\"#h-exercise-19-perform-arithmetic-operations-on-two-numpy-arrays-element-wise\" data-level=\"2\">Exercise 19: Perform arithmetic operations on two NumPy arrays element-wise<\/a><\/li><li><a href=\"#h-exercise-20-matrix-multiplication\" data-level=\"2\">Exercise 20: Matrix multiplication<\/a><\/li><li><a href=\"#h-exercise-21-compute-the-mean-median-and-standard-deviation-of-a-numpy-array\" data-level=\"2\">Exercise 21: Compute the mean, median, and standard deviation of a NumPy array<\/a><\/li><li><a href=\"#h-exercise-22-remove-common-items-from-array\" data-level=\"2\">Exercise 22: Remove common items from array<\/a><\/li><li><a href=\"#h-exercise-23-normalize-a-numpy-array-values-between-0-and-1\" data-level=\"2\">Exercise 23: Normalize a NumPy array (values between 0 and 1)<\/a><\/li><li><a href=\"#h-exercise-24-get-the-positions-where-elements-of-array-a-and-b-match\" data-level=\"2\">Exercise 24: Get the positions where elements of array a and b match<\/a><\/li><li><a href=\"#h-exercise-25-extract-numbers-from-an-array\" data-level=\"2\">Exercise 25: Extract numbers from an array<\/a><\/li><li><a href=\"#h-exercise-26-create-a-random-3-2-matrix-and-find-its-maximum-and-minimum-values\" data-level=\"2\">Exercise 26: Create a random 3\u00d72 matrix and find its maximum and minimum values<\/a><\/li><li><a href=\"#h-exercise-27-sorting-a-numpy-array-based-on-a-specific-column\" data-level=\"2\">Exercise 27: Sorting a NumPy array based on a specific column<\/a><\/li><li><a href=\"#h-exercise-28-delete-and-insert-a-column-in-a-numpy-array\" data-level=\"2\">Exercise 28: Delete and Insert a Column in a NumPy Array<\/a><\/li><li><a href=\"#h-exercise-29-swap-column-1-and-2-in-a-2d-array\" data-level=\"2\">Exercise 29: Swap column 1 and 2 in a 2D array<\/a><\/li><li><a href=\"#h-exercise-30-generate-10-random-integers-between-1-and-100\" data-level=\"2\">Exercise 30: Generate 10 random integers between 1 and 100<\/a><\/li><li><a href=\"#h-exercise-31-create-a-3-3-array-of-random-integers-and-sort-it-row-wise\" data-level=\"2\">Exercise 31: Create a 3\u00d73 array of random integers and sort it row-wise<\/a><\/li><li><a href=\"#h-exercise-32-shuffle-an-array-randomly\" data-level=\"2\">Exercise 32: Shuffle an array randomly<\/a><\/li><li><a href=\"#h-exercise-33-create-a-5x5-2d-array-with-1s-on-the-border-and-0s-inside\" data-level=\"2\">Exercise 33: Create a 5&#215;5 2D array with 1s on the border and 0s inside.<\/a><\/li><li><a href=\"#h-exercise-34-check-if-an-array-contains-any-nan-values\" data-level=\"2\">Exercise 34: Check if an array contains any NaN values.<\/a><\/li><li><a href=\"#h-exercise-35-sort-the-rows-of-a-2d-array-based-on-the-values-of-the-second-column\" data-level=\"2\">Exercise 35: Sort the rows of a 2D array based on the values of the second column<\/a><\/li><li><a href=\"#h-exercise-36-flatten-a-multi-dimensional-numpy-array\" data-level=\"2\">Exercise 36: Flatten a multi-dimensional NumPy array<\/a><\/li><li><a href=\"#h-exercise-37-stack-two-arrays-vertically-and-horizontally\" data-level=\"2\">Exercise 37: Stack two arrays vertically and horizontally<\/a><\/li><li><a href=\"#h-exercise-38-split-an-array-into-3-equal-parts\" data-level=\"2\">Exercise 38: Split an array into 3 equal parts<\/a><\/li><li><a href=\"#h-exercise-39-perform-addition-and-squaring-on-arrays\" data-level=\"2\">Exercise 39: Perform Addition and Squaring on Arrays<\/a><\/li><li><a href=\"#h-exercise-40-invert-a-matrix\" data-level=\"2\">Exercise 40: Invert a matrix<\/a><\/li><li><a href=\"#h-exercise-41-use-boolean-indexing-to-filter-values-less-than-a-given-number\" data-level=\"2\">Exercise 41: Use boolean indexing to filter values less than a given number<\/a><\/li><li><a href=\"#h-exercise-42-count-the-number-of-occurrences-of-each-unique-element\" data-level=\"2\">Exercise 42: Count the number of occurrences of each unique element<\/a><\/li><li><a href=\"#h-exercise-43-find-the-intersection-and-union-of-two-arrays\" data-level=\"2\">Exercise 43: Find the intersection and union of two arrays<\/a><\/li><li><a href=\"#h-exercise-44-transpose-a-matrix\" data-level=\"2\">Exercise 44: Transpose a matrix<\/a><\/li><li><a href=\"#h-exercise-45-compute-the-eigenvalues-and-eigenvectors-of-a-matrix\" data-level=\"2\">Exercise 45: Compute the eigenvalues and eigenvectors of a matrix<\/a><\/li><li><a href=\"#h-exercise-46-solve-a-linear-equation\" data-level=\"2\">Exercise 46: Solve a linear equation<\/a><\/li><li><a href=\"#h-exercise-47-create-an-8x8-checkerboard-pattern-using-0s-and-1s\" data-level=\"2\">Exercise 47: Create an 8&#215;8 checkerboard pattern using 0s and 1s<\/a><\/li><li><a href=\"#h-exercise-48-find-nearest-value\" data-level=\"2\">Exercise 48: Find nearest value<\/a><\/li><li><a href=\"#h-exercise-49-convert-to-object-array\" data-level=\"2\">Exercise 49: Convert to object array<\/a><\/li><li><a href=\"#h-exercise-50-compute-the-mean-median-and-standard-deviation-of-a-numpy-array\" data-level=\"2\">Exercise 50: Compute the mean, median, and standard deviation of a NumPy array<\/a><\/li><\/ul><\/div>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-1-create-a-1d-numpy-array-of-numbers-from-0-to-9\">Exercise 1: Create a 1D NumPy array of numbers from 0 to 9<\/h2>\n\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[0 1 2 3 4 5 6 7 8 9]<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Use the <code>np.arange()<\/code> function.<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code1\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\narr = np.arange(<span class=\"hljs-number\">10<\/span>)\nprint(arr)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code1', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code1');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation<\/strong>: <code>np.arange(10)<\/code> generates values from <code>0<\/code> to <code>9<\/code>. The default step is <code>1<\/code>.<\/p>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-2-convert-1d-array-to-2d\">Exercise 2: Convert 1D array to 2D<\/h2>\n\n\n\n<p>Write a code to convert a 1D array to a 2D array with 2 rows.<\/p>\n\n\n\n<p><strong>Given<\/strong>:<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\narr = np.arange(<span class=\"hljs-number\">6<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Original Array: [0 1 2 3 4 5]<br>Reshaped 2x3 Array:<br> [[0 1 2]<br> [3 4 5]]<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Use <code>reshape(rows, cols)<\/code> function.<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code2\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\narr = np.arange(<span class=\"hljs-number\">6<\/span>)\narr_2d = arr.reshape(<span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>)\nprint(arr_2d)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code2', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code2');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation<\/strong>: The <code>.reshape()<\/code> method is used to give a new shape to an array without changing its data. We specify the new shape as a tuple <code>(rows, columns)<\/code>. Since we want 2 rows and the original array has 6 elements, the new array must have 5 columns (<code>2 * 3 = 6<\/code>).<\/p>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-3-print-array-attributes\">Exercise 3: Print Array Attributes<\/h2>\n\n\n\n<p><strong>Instructions:<\/strong> Print the following attributes of the array:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The shape of the array.<\/li>\n\n\n\n<li>The number of array dimensions.<\/li>\n\n\n\n<li>The size of each element in bytes.<\/li>\n<\/ul>\n\n\n\n<p><strong>Given<\/strong>:<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\nmy_array = np.array(&#91;&#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>], &#91;<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>], &#91;<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>], &#91;<span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">8<\/span>]], dtype=np.uint16)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">The shape of the array is: (4, 2)<br>The number of dimensions is: 2<br>The size of each element in bytes is: 2<\/pre>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code3\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\n<span class=\"hljs-comment\"># Create the 4x2 array of type unsigned int16<\/span>\n<span class=\"hljs-comment\"># We use np.array() and specify the dtype<\/span>\nmy_array = np.array(&#91;&#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>], &#91;<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>], &#91;<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>], &#91;<span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">8<\/span>]], dtype=np.uint16)\n\n<span class=\"hljs-comment\"># Print the attributes<\/span>\nprint(<span class=\"hljs-string\">\"The shape of the array is:\"<\/span>, my_array.shape)\nprint(<span class=\"hljs-string\">\"The number of dimensions is:\"<\/span>, my_array.ndim)\nprint(<span class=\"hljs-string\">\"The size of each element in bytes is:\"<\/span>, my_array.itemsize)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code3', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code3');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation<\/strong>: <code>np.array()<\/code> converts a Python list into a NumPy array. This allows efficient mathematical operations.<\/p>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-4-create-a-3-3-numpy-array-of-all-true\">Exercise 4: Create a 3\u00d73 NumPy array of all True<\/h2>\n\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[[ True  True  True]<br> [ True  True  True]<br> [ True  True  True]]<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Use <code>np.ones()<\/code> with <code>dtype=bool<\/code>.<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code4\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\narr = np.ones((<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">3<\/span>), dtype=bool)\nprint(arr)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code4', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code4');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation<\/strong>: The <code>np.full()<\/code> function is a powerful way to create an array of any given shape filled with a specific value. The first argument <code>(3, 3)<\/code> specifies the shape (3 rows, 3 columns), and the second argument <code>True<\/code> is the value to fill the array with. The <code>dtype=bool<\/code> ensures that the array&#8217;s data type is boolean.<\/p>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-5-extract-the-documentation-of-numpy-s-arange-function\">Exercise 5: Extract the documentation of NumPy\u2019s <code>arange()<\/code> function<\/h2>\n\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">arange([start,] stop[, step,], dtype=None, *, like=None)<br>............<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Use Python\u2019s <code>np.info()<\/code> method<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code5\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\nnp.info(np.arange)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code5', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code5');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation<\/strong>: <code>np.info()<\/code> displays the docstring of a function. It helps understand usage, parameters, and return type.<\/p>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-6-create-a-1d-array-filled-with-zeros-and-another-filled-with-ones\">Exercise 6: Create a 1D array filled with zeros and another filled with ones<\/h2>\n\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Zeros: [0. 0. 0. 0. 0.]<br>Ones: [1. 1. 1. 1. 1.]<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Use <code>np.zeros()<\/code> and <code>np.ones()<\/code>.<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code6\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\nzeros_arr = np.zeros(<span class=\"hljs-number\">5<\/span>)\nones_arr = np.ones(<span class=\"hljs-number\">5<\/span>)\n\nprint(<span class=\"hljs-string\">\"Zeros:\"<\/span>, zeros_arr)\nprint(<span class=\"hljs-string\">\"Ones:\"<\/span>, ones_arr)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code6', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code6');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>np.ones(5)<\/code> \u2192 Creates <code>[1. 1. 1. 1. 1.]<\/code><\/li>\n\n\n\n<li><code>np.zeros(5)<\/code> \u2192 Creates <code>[0. 0. 0. 0. 0.]<\/code><\/li>\n<\/ul>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-7-create-a-1d-array-of-10-evenly-spaced-values-between-5-and-50\">Exercise 7: Create a 1D array of 10 evenly spaced values between 5 and 50<\/h2>\n\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[ 5. 10. 15. 20. 25. 30. 35. 40. 45. 50.]<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Use <code>np.linspace(start, stop, num)<\/code>.<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code7\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\narr = np.linspace(<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">50<\/span>, <span class=\"hljs-number\">10<\/span>)\nprint(arr)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code7', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code7');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation<\/strong>: <code>np.linspace(5, 50, 10)<\/code> generates 10 evenly spaced numbers starting from 5 to 50 (inclusive).<\/p>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-8-convert-a-python-list-into-a-numpy-array\">Exercise 8: Convert a Python list into a NumPy array<\/h2>\n\n\n\n<p><strong>Given<\/strong>:<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code labguage-python\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">py_list = &#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Use <code>np.array(list)<\/code>.<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code8\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\npy_list = &#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>]\narr = np.array(py_list)\nprint(arr)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code8', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code8');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation<\/strong>: <code>np.array()<\/code> converts a Python list into a NumPy array. This allows efficient mathematical operations.<\/p>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-9-find-the-memory-size-of-a-numpy-array-of-numbers-from-0-to-9\">Exercise 9: Find the memory size of a NumPy array of numbers from 0 to 9<\/h2>\n\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Array: [0 1 2 3 4 5 6 7 8 9]<br>Memory size in bytes: 80<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Use <code>nbytes<\/code> attribute<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code9\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\narr = np.arange(<span class=\"hljs-number\">10<\/span>)\nprint(<span class=\"hljs-string\">\"Array:\"<\/span>, arr)\nprint(<span class=\"hljs-string\">\"Memory size in bytes:\"<\/span>, arr.nbytes)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code9', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code9');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation<\/strong>: Each element in NumPy has a fixed size. <code>nbytes<\/code> gives the total memory used by the array<\/p>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-10-reverse-a-1d-numpy-array\">Exercise 10: Reverse a 1D NumPy array<\/h2>\n\n\n\n<p><strong>Given<\/strong>:<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code labguage-python\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\narr = np.arange(<span class=\"hljs-number\">10<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[9 8 7 6 5 4 3 2 1 0]<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Use slicing <code>[::-1]<\/code>.<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code10\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\narr = np.arange(<span class=\"hljs-number\">10<\/span>)\nreversed_arr = arr&#91;::<span class=\"hljs-number\">-1<\/span>]\nprint(reversed_arr)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code10', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code10');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation<\/strong>: The slice <code>[::-1]<\/code> steps backwards, effectively reversing the array.<\/p>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-11-create-a-3-3-identity-matrix\">Exercise 11: Create a 3\u00d73 identity matrix<\/h2>\n\n\n\n<p>Use tuple unpacking to swap the values of two variables without using a temporary variable.<\/p>\n\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[[1. 0. 0.]<br> [0. 1. 0.]<br> [0. 0. 1.]]<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Use <code>np.eye()<\/code> function.<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code11\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\nidentity = np.eye(<span class=\"hljs-number\">3<\/span>)\nprint(identity)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code11', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code11');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation<\/strong>: An <strong>identity matrix<\/strong> has <code>1s<\/code> on the diagonal and <code>0s<\/code> elsewhere. The <code>np.eye(3)<\/code> creates a 3\u00d73 identity matrix.<\/p>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-12-create-a-4-4-array-and-extract-its-first-row-and-last-column\">Exercise 12: Create a 4\u00d74 array and extract its first row and last column<\/h2>\n\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Array:<br> [[ 1  2  3  4]<br> [ 5  6  7  8]<br> [ 9 10 11 12]<br> [13 14 15 16]]<br>First Row: [1 2 3 4]<br>Last Column: [ 4  8 12 16]<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<ul class=\"wp-block-list\">\n<li>Use array indexing <code>[0]<\/code> for the first row.<\/li>\n\n\n\n<li>Use slicing <code>[:, -1]<\/code> to get last column.<\/li>\n<\/ul>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code12\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\narr = np.arange(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">17<\/span>).reshape(<span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">4<\/span>)\nprint(<span class=\"hljs-string\">\"Array:\\n\"<\/span>, arr)\n\nfirst_row = arr&#91;<span class=\"hljs-number\">0<\/span>]\nprint(<span class=\"hljs-string\">\"First Row:\"<\/span>, first_row)\n\nlast_col = arr&#91;:, <span class=\"hljs-number\">-1<\/span>]\nprint(<span class=\"hljs-string\">\"Last Column:\"<\/span>, last_col)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code12', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code12');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation<\/strong>:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>np.arange(1, 10).reshape(3, 3)<\/code> creates a 3\u00d73 array with numbers 1\u20139.<\/li>\n\n\n\n<li><code>arr[0]<\/code> selects the <strong>first row<\/strong> (indexing starts at 0).<\/li>\n<\/ol>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-13-extract-odd-rows-and-even-columns\">Exercise 13: Extract Odd Rows and Even Columns<\/h2>\n\n\n\n<p>Extract an array of odd rows and even columns from below NumPy array.<\/p>\n\n\n\n<p><strong>Problem Breakdown<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Rows:<\/strong> In programming, indexing starts at 0. Odd-indexed rows would be rows 1 and 3.<\/li>\n\n\n\n<li><strong>Columns:<\/strong> Even-indexed columns would be columns 0 and 2.<\/li>\n<\/ul>\n\n\n\n<p><strong>Given<\/strong>:<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\nsampleArray = np.array(&#91;\n    &#91;<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">9<\/span>, <span class=\"hljs-number\">12<\/span>], \n    &#91;<span class=\"hljs-number\">15<\/span>, <span class=\"hljs-number\">18<\/span>, <span class=\"hljs-number\">21<\/span>, <span class=\"hljs-number\">24<\/span>], \n    &#91;<span class=\"hljs-number\">27<\/span>, <span class=\"hljs-number\">30<\/span>, <span class=\"hljs-number\">33<\/span>, <span class=\"hljs-number\">36<\/span>], \n    &#91;<span class=\"hljs-number\">39<\/span>, <span class=\"hljs-number\">42<\/span>, <span class=\"hljs-number\">45<\/span>, <span class=\"hljs-number\">48<\/span>], \n    &#91;<span class=\"hljs-number\">51<\/span>, <span class=\"hljs-number\">54<\/span>, <span class=\"hljs-number\">57<\/span>, <span class=\"hljs-number\">60<\/span>]\n])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">New Array with Odd Rows and Even Columns:<br>[[15 21]<br> [39 45]]<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Use <code>np.hstack()<\/code>.<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code13\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\n<span class=\"hljs-comment\"># Original array<\/span>\nsampleArray = np.array(&#91;\n    &#91;<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">9<\/span>, <span class=\"hljs-number\">12<\/span>], \n    &#91;<span class=\"hljs-number\">15<\/span>, <span class=\"hljs-number\">18<\/span>, <span class=\"hljs-number\">21<\/span>, <span class=\"hljs-number\">24<\/span>], \n    &#91;<span class=\"hljs-number\">27<\/span>, <span class=\"hljs-number\">30<\/span>, <span class=\"hljs-number\">33<\/span>, <span class=\"hljs-number\">36<\/span>], \n    &#91;<span class=\"hljs-number\">39<\/span>, <span class=\"hljs-number\">42<\/span>, <span class=\"hljs-number\">45<\/span>, <span class=\"hljs-number\">48<\/span>], \n    &#91;<span class=\"hljs-number\">51<\/span>, <span class=\"hljs-number\">54<\/span>, <span class=\"hljs-number\">57<\/span>, <span class=\"hljs-number\">60<\/span>]\n])\n\n<span class=\"hljs-comment\"># Use slicing to select odd rows and even columns<\/span>\n<span class=\"hljs-comment\"># The slice &#91;1::2] selects every second element starting from index 1 (odd rows).<\/span>\n<span class=\"hljs-comment\"># The slice &#91;::2] selects every second element starting from index 0 (even columns).<\/span>\nnewArray = sampleArray&#91;<span class=\"hljs-number\">1<\/span>::<span class=\"hljs-number\">2<\/span>, ::<span class=\"hljs-number\">2<\/span>]\n\nprint(<span class=\"hljs-string\">\"Original Array:\"<\/span>)\nprint(sampleArray)\n\nprint(<span class=\"hljs-string\">\"\\nNew Array with Odd Rows and Even Columns:\"<\/span>)\nprint(newArray)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code13', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code13');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation<\/strong>: <\/p>\n\n\n\n<p>The syntax <code><strong>array[start:stop:step]<\/strong><\/code> lets you specify which elements to select.<\/p>\n\n\n\n<p><strong><code>1::2<\/code> for the rows means<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Start<\/strong>: at index 1 (the second row).<\/li>\n\n\n\n<li><strong>Stop<\/strong>: Omitted, so it goes to the end of the array.<\/li>\n\n\n\n<li><strong>Step<\/strong>: 2, meaning it selects every second row. This gives us rows at indices 1 and 3.<\/li>\n<\/ul>\n\n\n\n<p><strong><code>::2<\/code> for the columns means<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Start<\/strong>: Omitted, so it defaults to 0 (the first column).<\/li>\n\n\n\n<li><strong>Stop<\/strong>: Omitted, so it goes to the end.<\/li>\n\n\n\n<li><strong>Step<\/strong>: 2, meaning it selects every second column. This gives us columns at indices 0 and 2.<\/li>\n<\/ul>\n\n\n\n<p>By combining these two slices, <code><strong>sampleArray[1::2, ::2]<\/strong><\/code>, NumPy efficiently creates a new array containing the elements at the intersection of the specified rows and columns.<\/p>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-14-stack-arrays-horizontally\">Exercise 14: Stack arrays horizontally<\/h2>\n\n\n\n<p><strong>Given<\/strong>:<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\na = np.array(&#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>])\nb = np.array(&#91;<span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[1 2 3 4 5 6]<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Use <code>np.hstack()<\/code>.<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code14\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\na = np.array(&#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>])\nb = np.array(&#91;<span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>])\nstacked_arr = np.hstack((a, b))\nprint(stacked_arr)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code14', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code14');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation<\/strong>: The <code>np.hstack()<\/code> function stands for &#8220;horizontal stack.&#8221; It stacks arrays in sequence column-wise, effectively concatenating them along the second axis. For 1D arrays, this simply joins them end-to-end.<\/p>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-15-slice-the-first-two-rows-and-first-two-columns-from-a-4-4-array\">Exercise 15: Slice the first two rows and first two columns from a 4\u00d74 array<\/h2>\n\n\n\n<p>Write a code to Create a 4\u00d74 NumPy array and extract the first two rows and first two columns.<\/p>\n\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Array:<br> [[ 1  2  3  4]<br> [ 5  6  7  8]<br> [ 9 10 11 12]<br> [13 14 15 16]]<br>First 2 rows and columns:<br> [[1 2]<br> [5 6]]<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Use slicing <code>[0:2, 0:2]<\/code>.<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code15\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-21\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\narr = np.arange(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">17<\/span>).reshape(<span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">4<\/span>)\nprint(<span class=\"hljs-string\">\"Array:\\n\"<\/span>, arr)\n\nsub_arr = arr&#91;<span class=\"hljs-number\">0<\/span>:<span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">0<\/span>:<span class=\"hljs-number\">2<\/span>]\nprint(<span class=\"hljs-string\">\"First 2 rows and columns:\\n\"<\/span>, sub_arr)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-21\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code15', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code15');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation<\/strong>: <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>arr[0:2, 0:2]<\/code> \u2192 selects rows 0\u20131 and columns 0\u20131.<\/li>\n\n\n\n<li>The result is a <strong>2\u00d72 sub-matrix<\/strong>.<\/li>\n<\/ul>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-16-replace-all-odd-numbers-in-a-numpy-array-with-1\">Exercise 16: Replace all odd numbers in a NumPy array with -1<\/h2>\n\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Original Array: [ 1  2  3  4  5  6  7  8  9 10]<br>Modified Array: [-1  2 -1  4 -1  6 -1  8 -1 10]<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Use boolean indexing with modulo <code>%<\/code> to find odd numbers.<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code16\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-22\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\narr = np.arange(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">11<\/span>)\nprint(<span class=\"hljs-string\">\"Original Array:\"<\/span>, arr)\n\narr&#91;arr % <span class=\"hljs-number\">2<\/span> == <span class=\"hljs-number\">1<\/span>] = <span class=\"hljs-number\">-1<\/span>\nprint(<span class=\"hljs-string\">\"Modified Array:\"<\/span>, arr)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-22\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code16', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code16');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>arr % 2 == 1<\/code> \u2192 creates a boolean mask for odd numbers.<\/li>\n\n\n\n<li>Assigning <code>-1<\/code> replaces all odd values.<\/li>\n<\/ul>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-17-get-the-indices-of-non-zero-elements-in-an-array\">Exercise 17: Get the indices of non-zero elements in an array<\/h2>\n\n\n\n<p><strong>Given<\/strong>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-23\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\narr = np.array(&#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">4<\/span>])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-23\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Array: [1 0 2 0 3 0 4]<br>Indices: (array([0, 2, 4, 6]),)<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Use <code>np.nonzero()<\/code>.<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code17\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-24\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\narr = np.array(&#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">4<\/span>])\nprint(<span class=\"hljs-string\">\"Array:\"<\/span>, arr)\n\nindices = np.nonzero(arr)\nprint(<span class=\"hljs-string\">\"Indices of non-zero elements:\"<\/span>, indices)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-24\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code17', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code17');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>np.nonzero(arr)<\/code> returns a tuple of indices where elements are non-zero.<\/li>\n\n\n\n<li>In this example \u2192 <code>(array([0, 2, 4, 6]),)<\/code> meaning non-zero elements are at positions 0, 2, 4, 6.<\/li>\n<\/ul>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-18-find-the-common-items-between-two-arrays\">Exercise 18: Find the common items between two arrays<\/h2>\n\n\n\n<p><strong>Given<\/strong>: <\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-25\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\na = np.array(&#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">8<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">4<\/span>])\nb = np.array(&#91;<span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">8<\/span>])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-25\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[2 4 8]<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Use the <code>np.intersect1d()<\/code> function<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code18\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-26\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\na = np.array(&#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">8<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">4<\/span>])\nb = np.array(&#91;<span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">8<\/span>])\ncommon_items = np.intersect1d(a, b)\nprint(common_items)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-26\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code18', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code18');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation<\/strong>: The <code>np.intersect1d()<\/code> function finds the <strong>unique<\/strong> common elements between two arrays and returns them in a sorted 1D array. This is a highly efficient way to perform set-like operations on NumPy arrays.<\/p>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-19-perform-arithmetic-operations-on-two-numpy-arrays-element-wise\">Exercise 19: Perform arithmetic operations on two NumPy arrays element-wise<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Add two NumPy arrays element by element.<\/li>\n\n\n\n<li>Multiply two NumPy arrays element by element.<\/li>\n<\/ul>\n\n\n\n<p><strong>Given<\/strong>:<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code labguage-python\" aria-describedby=\"shcb-language-27\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\na = np.array(&#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>])\nb = np.array(&#91;<span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-27\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Element-wise Sum: [5 7 9]<br>Element-wise multiplication: [ 4 10 18]<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<ul class=\"wp-block-list\">\n<li>Use the <code>+<\/code> operator or <code>np.add()<\/code>.<\/li>\n\n\n\n<li>Use the <code>*<\/code> operator or <code>np.multiply()<\/code>.<\/li>\n<\/ul>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code19\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-28\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\na = np.array(&#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>])\nb = np.array(&#91;<span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>])\n\nadd = a + b   <span class=\"hljs-comment\"># or np.add(a, b)<\/span>\nprint(<span class=\"hljs-string\">\"Element-wise Sum:\"<\/span>, add)\n\nproduct = a * b   <span class=\"hljs-comment\"># or np.multiply(a, b)<\/span>\nprint(<span class=\"hljs-string\">\"Element-wise multiplication:\"<\/span>, product)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-28\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code19', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code19');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div><\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-20-matrix-multiplication\">Exercise 20: Matrix multiplication<\/h2>\n\n\n\n<p>Write a code to compute the dot product of two NumPy arrays<\/p>\n\n\n\n<p><strong>Given<\/strong>:<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code labguage-python\" aria-describedby=\"shcb-language-29\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\na = np.array(&#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>])\nb = np.array(&#91;<span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-29\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Dot Product: 32<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Use <code>np.dot()<\/code> or <code>@<\/code> operator.<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code20\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-30\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\na = np.array(&#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>])\nb = np.array(&#91;<span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>])\n\ndot_product = np.dot(a, b)   <span class=\"hljs-comment\"># or a @ b<\/span>\nprint(<span class=\"hljs-string\">\"Dot Product:\"<\/span>, dot_product)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-30\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code20', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code20');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation<\/strong>: <\/p>\n\n\n\n<p>For matrix multiplication, it&#8217;s crucial to distinguish it from element-wise multiplication (<code>*<\/code>). The <code>@<\/code> operator and the <code>np.dot()<\/code> function are specifically designed for matrix multiplication, following the rules of linear algebra. The result is a new matrix where the value at each position is the dot product of a row from the first matrix and a column from the second.<\/p>\n\n\n\n<p>The dot product is calculated as: <code>1*4 + 2*5 + 3*6 = 32<\/code>.<\/p>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-21-compute-the-mean-median-and-standard-deviation-of-a-numpy-array\">Exercise 21: Compute the mean, median, and standard deviation of a NumPy array<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Mean<\/strong>: Average of numbers.<\/li>\n\n\n\n<li><strong>Median<\/strong>: Middle value when sorted.<\/li>\n\n\n\n<li><strong>Standard Deviation<\/strong>: How spread out values are from the mean.<\/li>\n<\/ul>\n\n\n\n<p><strong>Given<\/strong>:<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-31\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\narr = np.array(&#91;<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>, <span class=\"hljs-number\">30<\/span>, <span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">200<\/span>, <span class=\"hljs-number\">300<\/span>])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-31\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Mean: 110.0<br>Median: 65.0<br>Standard Deviation: 107.08252269472673<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Use <code>np.mean()<\/code>, <code>np.median()<\/code>, <code>np.std()<\/code>.<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code21\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-32\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\narr = np.array(&#91;<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>, <span class=\"hljs-number\">30<\/span>, <span class=\"hljs-number\">40<\/span>, <span class=\"hljs-number\">50<\/span>])\n\nprint(<span class=\"hljs-string\">\"Mean:\"<\/span>, np.mean(arr))\nprint(<span class=\"hljs-string\">\"Median:\"<\/span>, np.median(arr))\nprint(<span class=\"hljs-string\">\"Standard Deviation:\"<\/span>, np.std(arr))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-32\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code21', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code21');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div><\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-22-remove-common-items-from-array\">Exercise 22: Remove common items from array<\/h2>\n\n\n\n<p><strong>Given<\/strong>:<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-33\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\na = np.array(&#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>])\nb = np.array(&#91;<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">8<\/span>, <span class=\"hljs-number\">9<\/span>])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-33\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[1 2 3 4]<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Use a boolean mask with the <code>np.in1d()<\/code> function<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code22\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-34\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\na = np.array(&#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>])\nb = np.array(&#91;<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">8<\/span>, <span class=\"hljs-number\">9<\/span>])\n\n<span class=\"hljs-comment\"># Create a boolean mask where `a`'s elements are NOT in `b`<\/span>\nmask = np.in1d(a, b)\n\n<span class=\"hljs-comment\"># Use the `~` operator to invert the mask (get the opposite)<\/span>\nresult = a&#91;~mask]\nprint(result)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-34\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code22', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code22');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation:<\/strong> <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>np.in1d()<\/code> function checks if each element of the first array is also present in the second array, returning a boolean array. <\/li>\n\n\n\n<li>The <code>~<\/code> operator is the <strong>bitwise NOT<\/strong> operator, which inverts the boolean mask (e.g., <code>True<\/code> becomes <code>False<\/code>). We use this inverted mask to select only the elements from <code>a<\/code> that are not present in <code>b<\/code>.<\/li>\n<\/ul>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-23-normalize-a-numpy-array-values-between-0-and-1\">Exercise 23: Normalize a NumPy array (values between 0 and 1)<\/h2>\n\n\n\n<p><strong>Given<\/strong>:<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-35\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\narr = np.array(&#91;<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>, <span class=\"hljs-number\">30<\/span>, <span class=\"hljs-number\">40<\/span>, <span class=\"hljs-number\">50<\/span>])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-35\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Normalized Array: [0.   0.25 0.5  0.75 1.  ]<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Use formula: <code>arr - arr.min()) \/ (arr.max() - arr.min())<\/code>.<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code23\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-36\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\narr = np.array(&#91;<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>, <span class=\"hljs-number\">30<\/span>, <span class=\"hljs-number\">40<\/span>, <span class=\"hljs-number\">50<\/span>])\nnormalized = (arr - arr.min()) \/ (arr.max() - arr.min())\nprint(<span class=\"hljs-string\">\"Normalized Array:\"<\/span>, normalized)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-36\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code23', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code23');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Subtract the minimum value so the smallest becomes 0.<\/li>\n\n\n\n<li>Divide by the range <code>(max - min)<\/code> so the largest becomes 1.<\/li>\n\n\n\n<li>This scales all values into <code>[0, 1]<\/code>.<\/li>\n<\/ul>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-24-get-the-positions-where-elements-of-array-a-and-b-match\">Exercise 24: Get the positions where elements of array <code>a<\/code> and <code>b<\/code> match<\/h2>\n\n\n\n<p><strong>Given<\/strong>:<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-37\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\na = np.array(&#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>])\nb = np.array(&#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">8<\/span>])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-37\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(array([0, 2]),)<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Use <code>np.where()<\/code> with a condition.<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code24\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-38\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\na = np.array(&#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>])\nb = np.array(&#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">8<\/span>])\n\n<span class=\"hljs-comment\"># Use np.where() to find the index where the values match<\/span>\nindex = np.where(a == b)\nprint(index)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-38\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code24', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code24');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation:<\/strong> The <code>np.where()<\/code> function returns the indices of elements in an array that satisfy a given condition. In this case, the condition <code>a == b<\/code> creates a boolean array, and <code>np.where()<\/code> returns the index of the <code>True<\/code> value(s)<\/p>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-25-extract-numbers-from-an-array\">Exercise 25: Extract numbers from an array<\/h2>\n\n\n\n<p>Write a code to extract all numbers from an array that are between 5 and 10 (inclusive).<\/p>\n\n\n\n<p><strong>Given<\/strong>:<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-39\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\narr = np.arange(<span class=\"hljs-number\">15<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-39\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[ 5  6  7  8  9 10]<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Combine two boolean masks using the bitwise AND operator (<code>&amp;<\/code>).<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code25\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-40\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\narr = np.arange(<span class=\"hljs-number\">15<\/span>)\nresult = arr&#91;(arr &gt;= <span class=\"hljs-number\">5<\/span>) &amp; (arr &lt;= <span class=\"hljs-number\">10<\/span>)]\nprint(result)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-40\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code25', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code25');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation:<\/strong> <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We create two separate boolean masks: <code>arr &gt;= 5<\/code> and <code>arr &lt;= 10<\/code>. <\/li>\n\n\n\n<li>We then use the bitwise AND operator <code>&amp;<\/code> to combine them. This creates a new mask where <code>True<\/code> is only at positions where <strong>both<\/strong> conditions are met. <\/li>\n\n\n\n<li>This combined mask is then used to select the desired elements from the original array.<\/li>\n<\/ul>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-26-create-a-random-3-2-matrix-and-find-its-maximum-and-minimum-values\">Exercise 26: Create a random 3\u00d72 matrix and find its maximum and minimum values<\/h2>\n\n\n\n<p><strong>Expected Output<\/strong> (can vary):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[[0.9 0.05]<br> [0.7 0.2]<br> [0.4 0.6]]<br>Max Value: 0.9<br>Min Value: 0.05<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Use <code>np.random.random()<\/code> with <code>np.max()<\/code> and <code>np.min()<\/code>.<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code26\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-41\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\nrand_arr = np.random.rand(<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">2<\/span>)\nprint(rand_arr)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-41\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code26', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code26');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation:<\/strong> The <code>np.random.rand()<\/code> function creates an array of a specified shape with random numbers from a uniform distribution over <code>[0, 1)<\/code>. The arguments represent the dimensions of the desired array.<\/p>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-27-sorting-a-numpy-array-based-on-a-specific-column\">Exercise 27: Sorting a NumPy array based on a specific column<\/h2>\n\n\n\n<p><strong>Given<\/strong>:<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-42\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\nsampleArray = np.array(&#91;&#91;<span class=\"hljs-number\">34<\/span>, <span class=\"hljs-number\">43<\/span>, <span class=\"hljs-number\">73<\/span>], &#91;<span class=\"hljs-number\">82<\/span>, <span class=\"hljs-number\">22<\/span>, <span class=\"hljs-number\">12<\/span>], &#91;<span class=\"hljs-number\">53<\/span>, <span class=\"hljs-number\">94<\/span>, <span class=\"hljs-number\">66<\/span>]])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-42\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Original array:<br>[[34 43 73]<br> [82 22 12]<br> [53 94 66]]<br><br>Sorted array:<br>[[82 22 12]<br> [34 43 73]<br> [53 94 66]]<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Get the indices that would sort an array&#8217;s elements using <code>np.argsort()<\/code>. Then, you can use these indices to reorder the rows of the original array<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code27\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-43\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\nsampleArray = np.array(&#91;&#91;<span class=\"hljs-number\">34<\/span>, <span class=\"hljs-number\">43<\/span>, <span class=\"hljs-number\">73<\/span>], &#91;<span class=\"hljs-number\">82<\/span>, <span class=\"hljs-number\">22<\/span>, <span class=\"hljs-number\">12<\/span>], &#91;<span class=\"hljs-number\">53<\/span>, <span class=\"hljs-number\">94<\/span>, <span class=\"hljs-number\">66<\/span>]])\n\n<span class=\"hljs-comment\"># Get the indices that would sort the array by its second column<\/span>\nsorted_indices = sampleArray&#91;:, <span class=\"hljs-number\">1<\/span>].argsort()\n\n<span class=\"hljs-comment\"># Use the sorted indices to reorder the rows of the array<\/span>\nsorted_array = sampleArray&#91;sorted_indices]\n\nprint(<span class=\"hljs-string\">\"Original array:\"<\/span>)\nprint(sampleArray)\n\nprint(<span class=\"hljs-string\">\"\\nSorted array:\"<\/span>)\nprint(sorted_array)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-43\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code27', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code27');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<p>The solution uses <strong><code>np.argsort()<\/code><\/strong> on the second column to get the correct row order.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>sampleArray[:, 1]<\/code><\/strong> selects the second column of the array. The <code>:<\/code> means &#8220;select all rows,&#8221; and the <code>1<\/code> specifies the column index (since indexing starts at 0).<\/li>\n\n\n\n<li><strong><code>.argsort()<\/code><\/strong>: This method is called on the selected column. It does not sort the column itself but returns the <strong>indices<\/strong> that would sort it. For the column <code>[43, 22, 94]<\/code>, the sorted order would be <code>[22, 43, 94]<\/code>, and the corresponding original indices are <code>[1, 0, 2]<\/code>. This is the output of <code>.argsort()<\/code>.<\/li>\n\n\n\n<li>By using these indices to reorder the rows of the <code>sampleArray<\/code>, you get a new array that is sorted based on the values in the second column.<\/li>\n<\/ul>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-28-delete-and-insert-a-column-in-a-numpy-array\">Exercise 28: Delete and Insert a Column in a NumPy Array<\/h2>\n\n\n\n<p><strong>Delete the second column<\/strong> from a given array and insert the following new column in its place.<\/p>\n\n\n\n<p><strong>Given<\/strong>:<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-44\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\nsampleArray = np.array(&#91;&#91;<span class=\"hljs-number\">34<\/span>,<span class=\"hljs-number\">43<\/span>,<span class=\"hljs-number\">73<\/span>],&#91;<span class=\"hljs-number\">82<\/span>,<span class=\"hljs-number\">22<\/span>,<span class=\"hljs-number\">12<\/span>],&#91;<span class=\"hljs-number\">53<\/span>,<span class=\"hljs-number\">94<\/span>,<span class=\"hljs-number\">66<\/span>]])\nnewColumnToAdd = np.array(&#91;<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">10<\/span>])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-44\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[[34 10 73]<br> [82 10 12]<br> [53 10 66]]<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Use <code>np.delete()<\/code> to remove the second column and then <code>np.insert()<\/code> to add the new one. Use <code>axis=1<\/code> to make sure the operation is performed on the columns.<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code28\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-45\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\nsampleArray = np.array(&#91;&#91;<span class=\"hljs-number\">34<\/span>,<span class=\"hljs-number\">43<\/span>,<span class=\"hljs-number\">73<\/span>],&#91;<span class=\"hljs-number\">82<\/span>,<span class=\"hljs-number\">22<\/span>,<span class=\"hljs-number\">12<\/span>],&#91;<span class=\"hljs-number\">53<\/span>,<span class=\"hljs-number\">94<\/span>,<span class=\"hljs-number\">66<\/span>]])\nnewColumn = np.array(&#91;<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">10<\/span>]) <span class=\"hljs-comment\"># Use a 1D array for simplicity<\/span>\n\n<span class=\"hljs-comment\"># Delete the second column (index 1)<\/span>\n<span class=\"hljs-comment\"># axis=1 specifies that the operation should be on columns<\/span>\ndeletedArray = np.delete(sampleArray, <span class=\"hljs-number\">1<\/span>, axis=<span class=\"hljs-number\">1<\/span>)\n\n<span class=\"hljs-comment\"># Insert the new column at the second position (index 1)<\/span>\n<span class=\"hljs-comment\"># axis=1 specifies that we're inserting a column<\/span>\nresultArray = np.insert(deletedArray, <span class=\"hljs-number\">1<\/span>, newColumn, axis=<span class=\"hljs-number\">1<\/span>)\n\nprint(resultArray)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-45\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code28', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code28');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<p>To perform this task, we use a two-step process:<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li><strong>Delete the column:<\/strong> The <code>np.delete()<\/code> function removes a specified column. We pass it the original <code>sampleArray<\/code>, the index of the column we want to delete (<code>1<\/code>), and the argument <strong><code>axis=1<\/code><\/strong> to tell NumPy that we&#8217;re performing the operation on a column.<\/li>\n\n\n\n<li><strong>Insert the new column:<\/strong> The <code>np.insert()<\/code> function adds a new column to the array. We pass it the modified array (<code>deletedArray<\/code>), the index where the new column should go (<code>1<\/code>), the data for the <strong><code>newColumn<\/code><\/strong>, and again, the <strong><code>axis=1<\/code><\/strong> to ensure it&#8217;s treated as a column. NumPy automatically handles broadcasting the <code>newColumn<\/code> to the correct shape to fit into the array.<\/li>\n<\/ol>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-29-swap-column-1-and-2-in-a-2d-array\">Exercise 29: Swap column 1 and 2 in a 2D array<\/h2>\n\n\n\n<p><strong>Given<\/strong>:<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-46\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\narr = np.arange(<span class=\"hljs-number\">9<\/span>).reshape(<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">3<\/span>)\n<span class=\"hljs-comment\"># The columns are at index 0, 1, 2. We want to rearrange to 0, 2, 1<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-46\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Before<br>[[0 1 2]<br> [3 4 5]<br> [6 7 8]]<br><br>After<br>[[0 2 1]<br> [3 5 4]<br> [6 8 7]]<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Use advanced indexing with a list of column indices<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code29\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-47\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\narr = np.arange(<span class=\"hljs-number\">9<\/span>).reshape(<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">3<\/span>)\nprint(<span class=\"hljs-string\">\"Before\"<\/span>)\nprint(arr)\n<span class=\"hljs-comment\"># The columns are at index 0, 1, 2. We want to rearrange to 0, 2, 1<\/span>\n\narr&#91;:, &#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>]] = arr&#91;:, &#91;<span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">1<\/span>]]\nprint(<span class=\"hljs-string\">\"After\"<\/span>)\nprint(arr)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-47\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code29', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code29');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>This is a neat trick using NumPy&#8217;s indexing capabilities. <code>arr[:, [1, 2]]<\/code> selects all rows (<code>:<\/code>) and columns 1 and 2. <\/li>\n\n\n\n<li>We then assign the values from <code>arr[:, [2, 1]]<\/code>, which selects all rows but with columns 2 and 1 in that order. This effectively swaps the data between the two columns.<\/li>\n<\/ul>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-30-generate-10-random-integers-between-1-and-100\">Exercise 30: Generate 10 random integers between 1 and 100<\/h2>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Use <code>np.random.randint(low, high, size)<\/code>.<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code30\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-48\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\narr = np.random.randint(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">101<\/span>, size=<span class=\"hljs-number\">10<\/span>)\nprint(<span class=\"hljs-string\">\"Random Integers:\"<\/span>, arr)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-48\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code30', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code30');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>low=1<\/code>, <code>high=101<\/code> (exclusive:-  integers from 1 to 100.<\/li>\n\n\n\n<li><code>size=10<\/code>: generate 10 random integers.<\/li>\n<\/ul>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-31-create-a-3-3-array-of-random-integers-and-sort-it-row-wise\">Exercise 31: Create a 3\u00d73 array of random integers and sort it row-wise<\/h2>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Use <code>np.sort()<\/code> with <code>axis=1<\/code>.<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code31\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-49\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\narr = np.random.randint(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">50<\/span>, size=(<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">3<\/span>))\nprint(<span class=\"hljs-string\">\"Original Array:\\n\"<\/span>, arr)\n\nsorted_arr = np.sort(arr, axis=<span class=\"hljs-number\">1<\/span>)\nprint(<span class=\"hljs-string\">\"Row-wise Sorted Array:\\n\"<\/span>, sorted_arr)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-49\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code31', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code31');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>axis=1<\/code>: sorts elements <strong>row by row<\/strong>.<\/li>\n\n\n\n<li>Each row is arranged in ascending order.<\/li>\n<\/ul>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-32-shuffle-an-array-randomly\">Exercise 32: Shuffle an array randomly<\/h2>\n\n\n\n<p><strong>Given<\/strong>:<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-50\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\narr = np.arange(<span class=\"hljs-number\">10<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-50\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Expected Output<\/strong> (can vary):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Original Array: [0 1 2 3 4 5 6 7 8 9]<br>Shuffled Array: [4 8 6 1 2 3 5 7 9 0]<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Use <code>np.random.shuffle()<\/code>.<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code32\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-51\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\narr = np.arange(<span class=\"hljs-number\">10<\/span>)\nprint(<span class=\"hljs-string\">\"Original Array:\"<\/span>, arr)\n\nnp.random.shuffle(arr)\nprint(<span class=\"hljs-string\">\"Shuffled Array:\"<\/span>, arr)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-51\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code32', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code32');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>np.random.shuffle()<\/code> randomly reorders elements in-place.<\/li>\n\n\n\n<li>Every run gives a different order.<\/li>\n<\/ul>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-33-create-a-5x5-2d-array-with-1s-on-the-border-and-0s-inside\">Exercise 33: Create a 5&#215;5 2D array with 1s on the border and 0s inside.<\/h2>\n\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[[1. 1. 1. 1. 1.]<br> [1. 0. 0. 0. 1.]<br> [1. 0. 0. 0. 1.]<br> [1. 0. 0. 0. 1.]<br> [1. 1. 1. 1. 1.]]<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Create an array of 1s and then use slicing to change the inner values to 0.<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code33\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-52\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\narr = np.ones((<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">5<\/span>))\narr&#91;<span class=\"hljs-number\">1<\/span>:<span class=\"hljs-number\">-1<\/span>, <span class=\"hljs-number\">1<\/span>:<span class=\"hljs-number\">-1<\/span>] = <span class=\"hljs-number\">0<\/span>\nprint(arr)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-52\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code33', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code33');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We start by creating a 5&#215;5 array of ones using <code>np.ones()<\/code>. <\/li>\n\n\n\n<li>Then, we use slicing to access the inner part of the array. The slice <code>1:-1<\/code> means &#8220;start at index 1 and go up to, but not including, the last element.&#8221;<\/li>\n\n\n\n<li> So, <code>arr[1:-1, 1:-1]<\/code> selects rows 1 through 3 and columns 1 through 3, which is the inner 3&#215;3 square. We then assign all of these elements to <code>0<\/code>.<\/li>\n<\/ul>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-34-check-if-an-array-contains-any-nan-values\">Exercise 34: Check if an array contains any NaN values.<\/h2>\n\n\n\n<p><strong>Given<\/strong>:<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-53\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\na = np.array(&#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, np.nan, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-53\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">True<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Use <code>np.isnan()<\/code> and <code>np.any()<\/code>.<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code34\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-54\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\na = np.array(&#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, np.nan, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>])\nhas_nan = np.isnan(a).any()\nprint(has_nan)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-54\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code34', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code34');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, <code>np.isnan(a)<\/code> creates a boolean array where <code>True<\/code> corresponds to NaN values. <\/li>\n\n\n\n<li>Then, we use the <code>.any()<\/code> method on this boolean array. <\/li>\n\n\n\n<li>The <code>.any()<\/code> method returns <code>True<\/code> if any element of the array is <code>True<\/code>, making it a simple way to check for the presence of NaNs.<\/li>\n<\/ul>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-35-sort-the-rows-of-a-2d-array-based-on-the-values-of-the-second-column\">Exercise 35: Sort the rows of a 2D array based on the values of the second column<\/h2>\n\n\n\n<p><strong>Given<\/strong>:<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-55\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\narr = np.array(&#91;&#91;<span class=\"hljs-number\">8<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">1<\/span>],\n                &#91;<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">7<\/span>],\n                &#91;<span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">9<\/span>, <span class=\"hljs-number\">3<\/span>]])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-55\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[[5 2 7]<br> [8 4 1]<br> [6 9 3]]<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Use <code>np.argsort()<\/code> on the second column to get the sorted indices, then use those indices to reorder the rows of the original array.<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code35\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-56\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\narr = np.array(&#91;&#91;<span class=\"hljs-number\">8<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">1<\/span>],\n                &#91;<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">7<\/span>],\n                &#91;<span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">9<\/span>, <span class=\"hljs-number\">3<\/span>]])\n\n<span class=\"hljs-comment\"># Get the indices that would sort the second column<\/span>\nsorted_indices = arr&#91;:, <span class=\"hljs-number\">1<\/span>].argsort()\n\n<span class=\"hljs-comment\"># Use the indices to sort the entire array's rows<\/span>\nsorted_arr = arr&#91;sorted_indices]\nprint(sorted_arr)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-56\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code35', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code35');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>np.argsort()<\/code> function returns the indices that would sort an array. <\/li>\n\n\n\n<li>We apply it to <code>arr[:, 1]<\/code>, which is the second column. This gives us the order in which the rows should be arranged to be sorted by that column. <\/li>\n\n\n\n<li>We then use this array of indices to reorder the rows of the original array <code>arr<\/code>, resulting in a new sorted array.<\/li>\n<\/ul>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-36-flatten-a-multi-dimensional-numpy-array\">Exercise 36: Flatten a multi-dimensional NumPy array<\/h2>\n\n\n\n<p>Write a code to Convert a 2D array into a 1D array.<\/p>\n\n\n\n<p><strong>Given<\/strong>:<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-57\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\narr = np.array(&#91;&#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>], &#91;<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>], &#91;<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>]])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-57\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Original Array:<br> [[1 2]<br> [3 4]<br> [5 6]]<br>Flattened Array: [1 2 3 4 5 6]<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Use <code>flatten()<\/code> or <code>ravel()<\/code>.<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code36\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-58\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\narr = np.array(&#91;&#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>], &#91;<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>], &#91;<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>]])\nflattened = arr.flatten()\n\nprint(<span class=\"hljs-string\">\"Original Array:\\n\"<\/span>, arr)\nprint(<span class=\"hljs-string\">\"Flattened Array:\"<\/span>, flattened)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-58\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code36', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code36');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>flatten()<\/code> returns a <strong>copy<\/strong> of the array in 1D form.<\/li>\n\n\n\n<li><code>ravel()<\/code> can also be used, but it may return a <strong>view<\/strong> instead of a copy.<\/li>\n<\/ul>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-37-stack-two-arrays-vertically-and-horizontally\">Exercise 37: Stack two arrays vertically and horizontally<\/h2>\n\n\n\n<p><strong>Given<\/strong>:<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-59\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\na = np.array(&#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>])\nb = np.array(&#91;<span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-59\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Vertical Stack:<br> [[1 2 3]<br> [4 5 6]]<br>Horizontal Stack:<br> [1 2 3 4 5 6]<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Use <code>np.vstack()<\/code> and <code>np.hstack()<\/code>.<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code37\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-60\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\na = np.array(&#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>])\nb = np.array(&#91;<span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>])\n\nv_stack = np.vstack((a, b))\nh_stack = np.hstack((a, b))\n\nprint(<span class=\"hljs-string\">\"Vertical Stack:\\n\"<\/span>, v_stack)\nprint(<span class=\"hljs-string\">\"Horizontal Stack:\\n\"<\/span>, h_stack)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-60\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code37', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code37');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>vstack()<\/code> \u2192 stacks arrays row-wise (top to bottom).<\/li>\n\n\n\n<li><code>hstack()<\/code> \u2192 stacks arrays column-wise (side by side).<\/li>\n<\/ul>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-38-split-an-array-into-3-equal-parts\">Exercise 38: Split an array into 3 equal parts<\/h2>\n\n\n\n<p>Split a 1D array of 9 elements into 3 equal parts.<\/p>\n\n\n\n<p><strong>Given<\/strong>:<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-61\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\narr = np.arange(<span class=\"hljs-number\">9<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-61\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Original Array: [0 1 2 3 4 5 6 7 8]<br>Split Arrays: [array([0, 1, 2]), array([3, 4, 5]), array([6, 7, 8])]<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Use <code>np.split(array, parts)<\/code>.<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code38\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-62\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\narr = np.arange(<span class=\"hljs-number\">9<\/span>)\nsplit_arr = np.split(arr, <span class=\"hljs-number\">3<\/span>)\n\nprint(<span class=\"hljs-string\">\"Original Array:\"<\/span>, arr)\nprint(<span class=\"hljs-string\">\"Split Arrays:\"<\/span>, split_arr)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-62\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code38', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code38');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation:<\/strong> <code>np.split(arr, 3)<\/code> divides the array into <strong>3 equal parts<\/strong>. Each part is a smaller NumPy array.<\/p>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-39-perform-addition-and-squaring-on-arrays\">Exercise 39: Perform Addition and Squaring on Arrays<\/h2>\n\n\n\n<p><strong>Given<\/strong>:<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-63\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\narrayOne = np.array(&#91;&#91;<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">9<\/span>], &#91;<span class=\"hljs-number\">21<\/span>, <span class=\"hljs-number\">18<\/span>, <span class=\"hljs-number\">27<\/span>]])\narrayTwo = np.array(&#91;&#91;<span class=\"hljs-number\">15<\/span>, <span class=\"hljs-number\">33<\/span>, <span class=\"hljs-number\">24<\/span>], &#91;<span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">1<\/span>]])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-63\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Result of array addition:<br>[[20 39 33]<br> [25 25 28]]<br><br>Result with each element squared:<br>[[ 400 1521 1089]<br> [ 625  625  784]]<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Perform <strong>vectorized operations<\/strong> directly on NumPy arrays. This means you can add two arrays together with the <code>+<\/code> operator and square them using the <code>** 2<\/code> operator<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code39\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-64\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\narrayOne = np.array(&#91;&#91;<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">9<\/span>], &#91;<span class=\"hljs-number\">21<\/span>, <span class=\"hljs-number\">18<\/span>, <span class=\"hljs-number\">27<\/span>]])\narrayTwo = np.array(&#91;&#91;<span class=\"hljs-number\">15<\/span>, <span class=\"hljs-number\">33<\/span>, <span class=\"hljs-number\">24<\/span>], &#91;<span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">1<\/span>]])\n\n<span class=\"hljs-comment\"># Add the two arrays<\/span>\nresultArray = arrayOne + arrayTwo\n\nprint(<span class=\"hljs-string\">\"Result of array addition:\"<\/span>)\nprint(resultArray)\n\n<span class=\"hljs-comment\"># Square each element of the resultArray<\/span>\nsquaredArray = resultArray ** <span class=\"hljs-number\">2<\/span>\n\nprint(<span class=\"hljs-string\">\"\\nResult with each element squared:\"<\/span>)\nprint(squaredArray)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-64\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code39', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code39');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Addition (+): When you use the + operator on two NumPy arrays of the same shape, it performs element-wise addition.<\/li>\n\n\n\n<li>Squaring (** 2): Similarly, the ** operator works element-wise. <\/li>\n<\/ul>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-40-invert-a-matrix\">Exercise 40: Invert a matrix<\/h2>\n\n\n\n<p><strong>Given<\/strong>:<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-65\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\narr = np.array(&#91;&#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>], &#91;<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>]])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-65\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[[-2.   1. ]<br> [ 1.5 -0.5]]<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Use the <code>np.linalg.inv()<\/code> function.<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code40\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-66\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\narr = np.array(&#91;&#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>], &#91;<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>]])\ninv_arr = np.linalg.inv(arr)\nprint(inv_arr)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-66\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code40', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code40');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation:<\/strong> The <code>np.linalg<\/code> module contains a wide range of linear algebra functions. <code>np.linalg.inv()<\/code> calculates the inverse of a square matrix. The inverse matrix, when multiplied by the original matrix, results in the identity matrix.<\/p>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-41-use-boolean-indexing-to-filter-values-less-than-a-given-number\">Exercise 41: Use boolean indexing to filter values less than a given number<\/h2>\n\n\n\n<p>From an array, return values <strong>less than 30<\/strong>.<\/p>\n\n\n\n<p><strong>Given<\/strong>:<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-67\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\narr = np.array(&#91;<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">12<\/span>, <span class=\"hljs-number\">29<\/span>, <span class=\"hljs-number\">30<\/span>, <span class=\"hljs-number\">44<\/span>, <span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">18<\/span>])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-67\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Values &lt; 30: [ 5 12 29  7 18]<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Build a mask <code>arr &lt; 30<\/code> and apply it.<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code41\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-68\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\narr = np.array(&#91;<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">12<\/span>, <span class=\"hljs-number\">29<\/span>, <span class=\"hljs-number\">30<\/span>, <span class=\"hljs-number\">44<\/span>, <span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">18<\/span>])\nfiltered = arr&#91;arr &lt; <span class=\"hljs-number\">30<\/span>]\nprint(<span class=\"hljs-string\">\"Values &lt; 30:\"<\/span>, filtered)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-68\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code41', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code41');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation:<\/strong> Comparisons on arrays return boolean masks that can be used to filter elements meeting a condition.<\/p>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-42-count-the-number-of-occurrences-of-each-unique-element\">Exercise 42: Count the number of occurrences of each unique element<\/h2>\n\n\n\n<p><strong>Given<\/strong>:<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-69\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\narr = np.array(&#91;<span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">5<\/span>])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-69\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Value 2 occurs 3 time(s)<br>Value 3 occurs 3 time(s)<br>Value 5 occurs 2 time(s)<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Use <code>np.unique(..., return_counts=True)<\/code>.<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code42\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-70\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\narr = np.array(&#91;<span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">5<\/span>])\nvals, counts = np.unique(arr, return_counts=<span class=\"hljs-literal\">True<\/span>)\n<span class=\"hljs-keyword\">for<\/span> v, c <span class=\"hljs-keyword\">in<\/span> zip(vals, counts):\n    print(<span class=\"hljs-string\">f\"Value <span class=\"hljs-subst\">{v}<\/span> occurs <span class=\"hljs-subst\">{c}<\/span> time(s)\"<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-70\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code42', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code42');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation:<\/strong> <code>np.unique<\/code> returns sorted unique values and, with <code>return_counts=True<\/code>, the frequency of each value.<\/p>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-43-find-the-intersection-and-union-of-two-arrays\">Exercise 43: Find the intersection and union of two arrays<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Intersection<\/strong>: elements present in both arrays.<\/li>\n\n\n\n<li><strong>Union<\/strong>: all unique elements from both arrays, sorted.<\/li>\n<\/ul>\n\n\n\n<p><strong>Given<\/strong>:<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-71\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\na = np.array(&#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">7<\/span>])\nb = np.array(&#91;<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">7<\/span>])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-71\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Intersection: [3 5 7]<br>Union: [1 2 3 4 5 6 7]<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Use <code>np.intersect1d()<\/code> and <code>np.union1d()<\/code>.<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code43\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-72\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\na = np.array(&#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">7<\/span>])\nb = np.array(&#91;<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">7<\/span>])\n\nintersection = np.intersect1d(a, b)\nunion = np.union1d(a, b)\n\nprint(<span class=\"hljs-string\">\"Intersection:\"<\/span>, intersection)\nprint(<span class=\"hljs-string\">\"Union:\"<\/span>, union)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-72\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code43', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code43');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div><\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-44-transpose-a-matrix\">Exercise 44: Transpose a matrix<\/h2>\n\n\n\n<p>Transposing a matrix means flipping it over its diagonal.<\/p>\n\n\n\n<p><strong>Given<\/strong>:<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-73\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\narr = np.array(&#91;&#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>], &#91;<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>]])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-73\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[[1 3]<br> [2 4]]<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Use the <code>.T<\/code> attribute or <code>np.transpose()<\/code>.<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code44\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-74\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\narr = np.array(&#91;&#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>], &#91;<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>]])\ntransposed_arr = arr.T\nprint(transposed_arr)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-74\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code44', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code44');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation:<\/strong> The <code>.T<\/code> attribute is a convenient and commonly used shortcut to get the transposed view of an array without creating a copy. <code>np.transpose()<\/code> does the same thing but as a function.<\/p>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-45-compute-the-eigenvalues-and-eigenvectors-of-a-matrix\">Exercise 45: Compute the eigenvalues and eigenvectors of a matrix<\/h2>\n\n\n\n<p>For a square matrix, compute its eigenvalues and eigenvectors.<\/p>\n\n\n\n<p><strong>Given<\/strong>:<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-75\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\nA = np.array(&#91;&#91;<span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">2<\/span>],\n              &#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">3<\/span>]])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-75\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Eigenvalues:<br> [5. 2.]<br>Eigenvectors (columns):<br> [[ 0.89442719 -0.70710678]<br> [ 0.4472136   0.70710678]]<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Use <code>np.linalg.eig()<\/code>.<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code45\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-76\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\nA = np.array(&#91;&#91;<span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">2<\/span>],\n              &#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">3<\/span>]])\n\neigvals, eigvecs = np.linalg.eig(A)\nprint(<span class=\"hljs-string\">\"Eigenvalues:\\n\"<\/span>, eigvals)\nprint(<span class=\"hljs-string\">\"Eigenvectors (columns):\\n\"<\/span>, eigvecs)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-76\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code45', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code45');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation:<\/strong> <code>np.linalg.eig(A)<\/code> returns eigenvalues and the corresponding eigenvectors (each <strong>column<\/strong> in <code>eigvecs<\/code> is an eigenvector of <code>A<\/code>).<\/p>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-46-solve-a-linear-equation\">Exercise 46: Solve a linear equation<\/h2>\n\n\n\n<p>Solve the system of linear equations: <code>x + 2y = 8<\/code> and <code>3x + 4y = 18<\/code>.<\/p>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Represent the equations as a matrix problem <code>Ax = b<\/code> and use <code>np.linalg.solve()<\/code>.<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code46\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-77\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\n<span class=\"hljs-comment\"># Define the coefficient matrix A<\/span>\nA = np.array(&#91;&#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>], &#91;<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>]])\n\n<span class=\"hljs-comment\"># Define the right-hand side vector b<\/span>\nb = np.array(&#91;<span class=\"hljs-number\">8<\/span>, <span class=\"hljs-number\">18<\/span>])\n\n<span class=\"hljs-comment\"># Solve for x and y<\/span>\nsolution = np.linalg.solve(A, b)\nprint(solution)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-77\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code46', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code46');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation:<\/strong> NumPy is excellent for solving linear equations. We first represent the system as <code>Ax = b<\/code>, where <code>A<\/code> is the matrix of coefficients, <code>x<\/code> is the vector of variables we want to solve for, and <code>b<\/code> is the vector of constants. <code>np.linalg.solve(A, b)<\/code> is a very efficient and accurate way to find the <code>x<\/code> vector.<\/p>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-47-create-an-8x8-checkerboard-pattern-using-0s-and-1s\">Exercise 47: Create an 8&#215;8 checkerboard pattern using 0s and 1s<\/h2>\n\n\n\n<p><strong>Given<\/strong>:<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-78\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\narr = np.array(&#91;<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>, <span class=\"hljs-number\">30<\/span>, <span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">200<\/span>, <span class=\"hljs-number\">300<\/span>])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-78\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[[0 1 0 1 0 1 0 1]<br> [1 0 1 0 1 0 1 0]<br> [0 1 0 1 0 1 0 1]<br> [1 0 1 0 1 0 1 0]<br> [0 1 0 1 0 1 0 1]<br> [1 0 1 0 1 0 1 0]<br> [0 1 0 1 0 1 0 1]<br> [1 0 1 0 1 0 1 0]]<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Use slicing and the modulo operator (<code>%<\/code>)<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code47\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-79\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\n<span class=\"hljs-comment\"># Create an 8x8 array of zeros<\/span>\ncheckerboard = np.zeros((<span class=\"hljs-number\">8<\/span>, <span class=\"hljs-number\">8<\/span>), dtype=int)\n\n<span class=\"hljs-comment\"># Use slicing and a step of 2 to set alternating elements<\/span>\ncheckerboard&#91;<span class=\"hljs-number\">1<\/span>::<span class=\"hljs-number\">2<\/span>, ::<span class=\"hljs-number\">2<\/span>] = <span class=\"hljs-number\">1<\/span>\ncheckerboard&#91;::<span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">1<\/span>::<span class=\"hljs-number\">2<\/span>] = <span class=\"hljs-number\">1<\/span>\nprint(checkerboard)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-79\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code47', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code47');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We create an 8&#215;8 array of zeros. The trick is in the slicing. <code>1::2<\/code> means &#8220;start at index 1 and take every second element.&#8221; <code>::2<\/code> means &#8220;start at index 0 and take every second element.&#8221; <\/li>\n\n\n\n<li>By applying these steps to the rows and columns, we can efficiently set the alternating pattern of 1s without needing a loop.<\/li>\n<\/ul>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-48-find-nearest-value\">Exercise 48: Find nearest value<\/h2>\n\n\n\n<p>Find the value in an array that is closest to a number 3.<\/p>\n\n\n\n<p><strong>Given<\/strong>:<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-80\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\narr = np.array(&#91;<span class=\"hljs-number\">1.5<\/span>, <span class=\"hljs-number\">2.8<\/span>, <span class=\"hljs-number\">3.2<\/span>, <span class=\"hljs-number\">4.1<\/span>])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-80\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">The value closest to 3 is: 2.8<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Subtract the given number from the array, take the absolute value, and then find the index of the minimum value using <code>np.abs()<\/code> and <code>np.argmin()<\/code>.<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code48\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-81\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\narr = np.array(&#91;<span class=\"hljs-number\">1.5<\/span>, <span class=\"hljs-number\">2.8<\/span>, <span class=\"hljs-number\">3.2<\/span>, <span class=\"hljs-number\">4.1<\/span>])\ntarget_value = <span class=\"hljs-number\">3<\/span>\n\n<span class=\"hljs-comment\"># Find the index of the minimum absolute difference<\/span>\nindex = np.abs(arr - target_value).argmin()\n\n<span class=\"hljs-comment\"># Use the index to get the value<\/span>\nnearest_value = arr&#91;index]\nprint(<span class=\"hljs-string\">f\"The value closest to <span class=\"hljs-subst\">{target_value}<\/span> is: <span class=\"hljs-subst\">{nearest_value}<\/span>\"<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-81\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code48', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code48');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>This is a neat two-step process. First, we perform a vectorized subtraction of the <code>target_value<\/code> from the entire array. Then, we take the <code>np.abs()<\/code> of the result to get the positive distance from the target for each element. <\/li>\n\n\n\n<li>Finally, <code>np.argmin()<\/code> returns the index of the element with the smallest value (i.e., the smallest distance), which we then use to retrieve the corresponding value from the original array.<\/li>\n<\/ul>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-49-convert-to-object-array\">Exercise 49: Convert to object array<\/h2>\n\n\n\n<p>Convert a 1D array to an object array, retaining the original data type of each element.<\/p>\n\n\n\n<p><strong>Given<\/strong>:<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-82\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\narr = np.array(&#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-string\">'a'<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>]) <\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-82\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Zeros: [0. 0. 0. 0. 0.]<br>Ones: [1. 1. 1. 1. 1.]<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Create a new empty object array and fill it with the elements..<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code49\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-83\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\narr = np.array(&#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>])\n\n<span class=\"hljs-comment\"># Create an empty object array with the same size<\/span>\nobj_arr = np.empty(arr.size, dtype=<span class=\"hljs-string\">'object'<\/span>)\n\n<span class=\"hljs-comment\"># Assign the elements from the original array<\/span>\nobj_arr&#91;:] = arr\n\n<span class=\"hljs-comment\"># We can now add a string element to it<\/span>\nobj_arr&#91;<span class=\"hljs-number\">1<\/span>] = <span class=\"hljs-string\">'a'<\/span>\nprint(obj_arr)\nprint(obj_arr.dtype)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-83\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code49', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code49');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A standard NumPy array can only hold elements of the same data type. An <strong>object array<\/strong> is a special type of array that can hold Python objects of different types, similar to a regular Python list. <\/li>\n\n\n\n<li>We create an empty array with <code>dtype='object'<\/code> and then use slicing to copy the elements over. This allows us to store mixed data types like integers and strings in a single NumPy array.<\/li>\n<\/ul>\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exercise-50-compute-the-mean-median-and-standard-deviation-of-a-numpy-array\">Exercise 50: Compute the mean, median, and standard deviation of a NumPy array<\/h2>\n\n\n\n<p><strong>Given<\/strong>:<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-84\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\narr = np.array(&#91;<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>, <span class=\"hljs-number\">30<\/span>, <span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">200<\/span>, <span class=\"hljs-number\">300<\/span>])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-84\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Expected Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Zeros: [0. 0. 0. 0. 0.]<br>Ones: [1. 1. 1. 1. 1.]<\/pre>\n\n\n\n<details class=\"wp-block-details exercise hint is-layout-flow wp-block-details-is-layout-flow\"><summary>+ Hint<\/summary>\n<p>Use <code>np.mean()<\/code>, <code>np.median()<\/code>, <code>np.std()<\/code>.<\/p>\n<\/details>\n\n\n\n<details class=\"wp-block-details exercise solution is-layout-flow wp-block-details-is-layout-flow\" name=\"solution\"><summary>+ Show Solution<\/summary><div class=\"hljstoolbar\"><pre id=\"code50\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-85\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np\n\nzeros_arr = np.zeros(<span class=\"hljs-number\">5<\/span>)\nones_arr = np.ones(<span class=\"hljs-number\">5<\/span>)\n\nprint(<span class=\"hljs-string\">\"Zeros:\"<\/span>, zeros_arr)\nprint(<span class=\"hljs-string\">\"Ones:\"<\/span>, ones_arr)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-85\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code50', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code50');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>np.ones(5)<\/code> \u2192 Creates <code>[1. 1. 1. 1. 1.]<\/code><\/li>\n\n\n\n<li><code>np.zeros(5)<\/code> \u2192 Creates <code>[0. 0. 0. 0. 0.]<\/code><\/li>\n<\/ul>\n<\/details>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>NumPy, short for Numerical Python, is a fundamental library for data science. It&#8217;s used to create and manipulate multidimensional arrays, making it incredibly useful for numerical operations and data analysis. This article gives you 50 NumPy coding practice problems with solution starting from fundamentals to linear algebra each with a hint, solution, and short explanation [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[22,8],"tags":[30],"class_list":{"0":"post-1773","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python","7":"category-python-exercises-with-solutions","8":"tag-python-exercises","9":"entry"},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>50 NumPy Exercises With Solutions (Beginner to Advanced)<\/title>\n<meta name=\"description\" content=\"Practice 50 Python NumPy exercises with solutions, hints, and explanations. Covers arrays, indexing, random, reshaping, filtering, and linear algebra.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/pynative.com\/python-numpy-exercise\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python NumPy Exercise: 50 Questions With Solutions (Beginner to Advanced)\" \/>\n<meta property=\"og:description\" content=\"Practice 50 Python NumPy exercises with solutions, hints, and explanations. Covers arrays, indexing, random, reshaping, filtering, and linear algebra.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/pynative.com\/python-numpy-exercise\/\" \/>\n<meta property=\"og:site_name\" content=\"PYnative\" \/>\n<meta property=\"article:published_time\" content=\"2019-01-17T04:47:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-08-31T07:51:51+00:00\" \/>\n<meta name=\"author\" content=\"Vishal\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@PyNative\" \/>\n<meta name=\"twitter:site\" content=\"@PyNative\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Vishal\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"14 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/pynative.com\\\/python-numpy-exercise\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-numpy-exercise\\\/\"},\"author\":{\"name\":\"Vishal\",\"@id\":\"https:\\\/\\\/pynative.com\\\/#\\\/schema\\\/person\\\/64b55d5bde2265918c5a9931de4de71f\"},\"headline\":\"Python NumPy Exercise: 50 Questions With Solutions (Beginner to Advanced)\",\"datePublished\":\"2019-01-17T04:47:06+00:00\",\"dateModified\":\"2025-08-31T07:51:51+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-numpy-exercise\\\/\"},\"wordCount\":3419,\"commentCount\":32,\"publisher\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/#\\\/schema\\\/person\\\/64b55d5bde2265918c5a9931de4de71f\"},\"keywords\":[\"Python Exercises\"],\"articleSection\":[\"Python\",\"Python Exercises\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/pynative.com\\\/python-numpy-exercise\\\/#respond\"]}],\"accessibilityFeature\":[\"tableOfContents\"]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/pynative.com\\\/python-numpy-exercise\\\/\",\"url\":\"https:\\\/\\\/pynative.com\\\/python-numpy-exercise\\\/\",\"name\":\"50 NumPy Exercises With Solutions (Beginner to Advanced)\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/#website\"},\"datePublished\":\"2019-01-17T04:47:06+00:00\",\"dateModified\":\"2025-08-31T07:51:51+00:00\",\"description\":\"Practice 50 Python NumPy exercises with solutions, hints, and explanations. Covers arrays, indexing, random, reshaping, filtering, and linear algebra.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-numpy-exercise\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/pynative.com\\\/python-numpy-exercise\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/pynative.com\\\/python-numpy-exercise\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/pynative.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Exercises\",\"item\":\"https:\\\/\\\/pynative.com\\\/python-exercises-with-solutions\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Python NumPy Exercise: 50 Questions With Solutions (Beginner to Advanced)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/pynative.com\\\/#website\",\"url\":\"https:\\\/\\\/pynative.com\\\/\",\"name\":\"PYnative\",\"description\":\"Python Programming\",\"publisher\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/#\\\/schema\\\/person\\\/64b55d5bde2265918c5a9931de4de71f\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/pynative.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/pynative.com\\\/#\\\/schema\\\/person\\\/64b55d5bde2265918c5a9931de4de71f\",\"name\":\"Vishal\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/vishalHule.jpg\",\"url\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/vishalHule.jpg\",\"contentUrl\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/vishalHule.jpg\",\"width\":968,\"height\":1065,\"caption\":\"Vishal\"},\"logo\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/vishalHule.jpg\"},\"description\":\"Founder of PYnative.com. I am a Python developer and I love to write articles to help developers. | All the best for your future Python endeavors!\",\"sameAs\":[\"https:\\\/\\\/pynative.com\"]}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"50 NumPy Exercises With Solutions (Beginner to Advanced)","description":"Practice 50 Python NumPy exercises with solutions, hints, and explanations. Covers arrays, indexing, random, reshaping, filtering, and linear algebra.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/pynative.com\/python-numpy-exercise\/","og_locale":"en_US","og_type":"article","og_title":"Python NumPy Exercise: 50 Questions With Solutions (Beginner to Advanced)","og_description":"Practice 50 Python NumPy exercises with solutions, hints, and explanations. Covers arrays, indexing, random, reshaping, filtering, and linear algebra.","og_url":"https:\/\/pynative.com\/python-numpy-exercise\/","og_site_name":"PYnative","article_published_time":"2019-01-17T04:47:06+00:00","article_modified_time":"2025-08-31T07:51:51+00:00","author":"Vishal","twitter_card":"summary_large_image","twitter_creator":"@PyNative","twitter_site":"@PyNative","twitter_misc":{"Written by":"Vishal","Est. reading time":"14 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/pynative.com\/python-numpy-exercise\/#article","isPartOf":{"@id":"https:\/\/pynative.com\/python-numpy-exercise\/"},"author":{"name":"Vishal","@id":"https:\/\/pynative.com\/#\/schema\/person\/64b55d5bde2265918c5a9931de4de71f"},"headline":"Python NumPy Exercise: 50 Questions With Solutions (Beginner to Advanced)","datePublished":"2019-01-17T04:47:06+00:00","dateModified":"2025-08-31T07:51:51+00:00","mainEntityOfPage":{"@id":"https:\/\/pynative.com\/python-numpy-exercise\/"},"wordCount":3419,"commentCount":32,"publisher":{"@id":"https:\/\/pynative.com\/#\/schema\/person\/64b55d5bde2265918c5a9931de4de71f"},"keywords":["Python Exercises"],"articleSection":["Python","Python Exercises"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/pynative.com\/python-numpy-exercise\/#respond"]}],"accessibilityFeature":["tableOfContents"]},{"@type":"WebPage","@id":"https:\/\/pynative.com\/python-numpy-exercise\/","url":"https:\/\/pynative.com\/python-numpy-exercise\/","name":"50 NumPy Exercises With Solutions (Beginner to Advanced)","isPartOf":{"@id":"https:\/\/pynative.com\/#website"},"datePublished":"2019-01-17T04:47:06+00:00","dateModified":"2025-08-31T07:51:51+00:00","description":"Practice 50 Python NumPy exercises with solutions, hints, and explanations. Covers arrays, indexing, random, reshaping, filtering, and linear algebra.","breadcrumb":{"@id":"https:\/\/pynative.com\/python-numpy-exercise\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/pynative.com\/python-numpy-exercise\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/pynative.com\/python-numpy-exercise\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/pynative.com\/"},{"@type":"ListItem","position":2,"name":"Python Exercises","item":"https:\/\/pynative.com\/python-exercises-with-solutions\/"},{"@type":"ListItem","position":3,"name":"Python NumPy Exercise: 50 Questions With Solutions (Beginner to Advanced)"}]},{"@type":"WebSite","@id":"https:\/\/pynative.com\/#website","url":"https:\/\/pynative.com\/","name":"PYnative","description":"Python Programming","publisher":{"@id":"https:\/\/pynative.com\/#\/schema\/person\/64b55d5bde2265918c5a9931de4de71f"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/pynative.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/pynative.com\/#\/schema\/person\/64b55d5bde2265918c5a9931de4de71f","name":"Vishal","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/01\/vishalHule.jpg","url":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/01\/vishalHule.jpg","contentUrl":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/01\/vishalHule.jpg","width":968,"height":1065,"caption":"Vishal"},"logo":{"@id":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/01\/vishalHule.jpg"},"description":"Founder of PYnative.com. I am a Python developer and I love to write articles to help developers. | All the best for your future Python endeavors!","sameAs":["https:\/\/pynative.com"]}]}},"featured_image_src":null,"featured_image_src_square":null,"author_info":{"display_name":"Vishal","author_link":"https:\/\/pynative.com\/author\/vishal\/"},"_links":{"self":[{"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/posts\/1773","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/comments?post=1773"}],"version-history":[{"count":3,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/posts\/1773\/revisions"}],"predecessor-version":[{"id":12210,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/posts\/1773\/revisions\/12210"}],"wp:attachment":[{"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/media?parent=1773"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/categories?post=1773"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/tags?post=1773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}