
| pip install numpy |
| import numpy print(numpy.__version__) |
| Creating An Array with List and Tuple |
| import numpy as np # Creating an array from a list list_array = np.array([10, 20, 30, 40]) print("Array from list:", list_array) # Creating an array from a tuple tuple_array = np.array((1, 2, 3, 4)) print("Array from tuple:", tuple_array) |
| Output- Array from list: [10 20 30 40] Array from tuple: [1 2 3 4] |
| Create An Array of Fixed Size In NumPy |
| import numpy as np # Creating a 4X4 array with all zeros c = np.zeros((4, 4)) print ("Array with all zeros:\n", c) # Create a constant value array of complex type d = np.full((3, 3), 8, dtype = 'complex') print ("Array with all 8s.":\n", d) # Create an array with random values e = np.random.random((3, 3)) print ("A random array:\n", e) |
| Output- Array with all zeros: [[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]] Array with all 8s. [[8.+0.j 8.+0.j 8.+0.j] [8.+0.j 8.+0.j 8.+0.j] [8.+0.j 8.+0.j 8.+0.j]] A random array: [[0.51232649 0.12543429 0.93746276] [0.57389056 0.83174901 0.14257313] [0.46584118 0.29798456 0.78659301]] |
| Create Array Using `arange()` Function |
| import numpy as np # Array from 1 to 8 with step 2 arange_step_array = np.arange(1, 9, 2) print("Array with np.arange(1, 9, 2):", arange_step_array) |
| Output- Array with np.arange(1, 9, 2): [1 3 5 7] |
| Reshaping Array using Reshape Method |
| import numpy as np
# Original array original_array = np.array([1, 2, 3, 4, 5, 6]) print("Original array:", original_array)# Reshaped to 2x3 array reshaped_array = original_array.reshape(2, 3) print("Reshaped array to 2x3:", reshaped_array) |
| Output- Original array: [1 2 3 4 5 6] Reshaped array to 2x3: [[1 2 3] [4 5 6]] |
| Program To Demonstrate Indexing |
| import numpy as np arr = np.array([[1, -2, 5, -4], [-4, 0.5, -6, 10], [-2.6, 0, -7, 8], [3, 7, -4, -2]]) # Slicing an array temp = arr[:2, ::2] print("Array with first 2 rows and alternate columns (0 and 2):\n", temp) # Integer array indexing temp = arr[[0, 1, 2, 3], [3, 2, 1, 0]] print("\nElements at indices (0, 3), (1, 2), (2, 1), (3, 0):\n", temp) # Boolean array indexing cond = arr > 0 # cond is a boolean array temp = arr[cond] print("\nElements greater than 0:\n", temp) |
| Output- Array with first 2 rows and alternate columns (0 and 2): [[ 1 5] [-4 -6]] Elements at indices (0, 3), (1, 2), (2, 1), (3, 0): [-4 -6 0 3] Elements greater than 0: [ 1. 5. 0.5 10. 8. 3. 7.] |
| Basic Arithmetic Operations |
| # Python program to demonstrate basic operations on single array import numpy as np a = np.array([1, 2, 5, 3]) # add 1 to every element print ("Adding 1 to every element:", a+1) # subtract 3 from each element print ("Subtracting 3 from each element:", a-3) # multiply each element by 10 print ("Multiplying each element by 10:", a*10) # square each element print ("Squaring each element:", a**2) # modify existing array a *= 2 print ("Doubled each element of original array:", a) # transpose of array a = np.array([[1, 2, 3], [3, 4, 5], [9, 6, 0]]) print ("\nOriginal array:\n", a) print ("Transpose of array:\n", a.T) |
| Output-
Adding 1 to every element: [2 3 6 4]
Subtracting 3 from each element: [-2 -1 2 0]
Multiplying each element by 10: [10 20 50 30]
Squaring each element: [ 1 4 25 9]
Doubled each element of original array: [ 2 4 10 6]
Original array: [[1 2 3] [3 4 5] [9 6 0]]Transpose of array: [[1 3 9] [2 4 6] [3 5 0]] |