From 4b560896c458a1ee2c910e41d87560cbc39ecec1 Mon Sep 17 00:00:00 2001 From: Sindhuja Golagani Date: Mon, 9 Feb 2026 15:36:09 +0530 Subject: [PATCH 1/2] Add NumPy tutorial with examples and explanations This file demonstrates various NumPy functionalities including array creation, reshaping, and properties. --- Test/num01 | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 Test/num01 diff --git a/Test/num01 b/Test/num01 new file mode 100644 index 0000000..95dbd2f --- /dev/null +++ b/Test/num01 @@ -0,0 +1,83 @@ +# Author: OMKAR PATHAK + +# NumPy (Numeric Python) is a Python package used for building multi dimensional arrays and performing +# various operations + +# In this program we will walk through various concepts and see available functions in the NumPy package. + +# For installing: pip3 install numpy + +import numpy as np + +# we have a function arange() which makes an array of the specified dimension. Example: +myArray = np.arange(20) +print(myArray) # [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19] + +# an array from 10 to 20 +myArray = np.arange(10, 20) # [10 11 12 13 14 15 16 17 18 19] +print(myArray) + +# an array from 10 to 20 with 2 steps +myArray = np.arange(10, 20, 2) +print(myArray) # [10 12 14 16 18] + +# reshape() helps to reshape our NumPy array +myArray = np.arange(20) +# syntax: reshape(number_of_rows, number_of_columns) +myArray = myArray.reshape(4, 5) +print(myArray) + +# [[ 0  1  2  3  4] +#  [ 5  6  7  8  9] +#  [10 11 12 13 14] +#  [15 16 17 18 19]] + +myArray = myArray.reshape(10, 2) +print(myArray) + +# [[ 0  1] +#  [ 2  3] +#  [ 4  5] +#  [ 6  7] +#  [ 8  9] +#  [10 11] +#  [12 13] +#  [14 15] +#  [16 17] +#  [18 19]] + +# shape returns the shape of the array. The length of shape tuple is called as rank (or dimension) +print(myArray.shape) # (10, 2) + +# ndim returns the dimension (rank) of the array +print(myArray.ndim) # 2 + +# size returns the total number of elements in the array +print(myArray.size) # 20 + +# to check the data we have dtype. +print(myArray.dtype) # int64 + +# zeros creates an array will all zeros +myArray = np.zeros((3, 4)) +print(myArray) + +# [[ 0.  0.  0.  0.] +#  [ 0.  0.  0.  0.] +#  [ 0.  0.  0.  0.]] + +# ones creates an array with all ones +myArray = np.ones((3, 4)) +print(myArray) + +# [[ 1.  1.  1.  1.] +#  [ 1.  1.  1.  1.] +#  [ 1.  1.  1.  1.]] + +# numpy random module helps to initialize array with random values +myArray = np.random.rand(3, 4) +print(myArray) + +# [[ 0.54808903  0.08750717  0.23886267  0.93589283] +#  [ 0.90750146  0.31197039  0.54013725  0.91092763] +#  [ 0.38827674  0.04647878  0.15997665  0.94909537]] From 21e616e2de747bb05f094082a93195d354707d63 Mon Sep 17 00:00:00 2001 From: Sindhu17020103 Date: Fri, 13 Feb 2026 19:34:37 +0530 Subject: [PATCH 2/2] Added numpy programs --- Test/P01_Introduction.py | 83 ++++++++++++++++++++++++++ Test/P02_NumpyDataTypes.py | 34 +++++++++++ Test/P03_NumpyAttributes.py | 24 ++++++++ Test/P04_ArrayFromNumericalRanges.py | 43 +++++++++++++ Test/P05_NumpyArrayManipulation.py | 83 ++++++++++++++++++++++++++ Test/P06_NumpyStringFunctions.py | 43 +++++++++++++ Test/P07_NumpyMathematicalFunctions.py | 30 ++++++++++ Test/P08_NumpyArithmeticOperations.py | 25 ++++++++ 8 files changed, 365 insertions(+) create mode 100644 Test/P01_Introduction.py create mode 100644 Test/P02_NumpyDataTypes.py create mode 100644 Test/P03_NumpyAttributes.py create mode 100644 Test/P04_ArrayFromNumericalRanges.py create mode 100644 Test/P05_NumpyArrayManipulation.py create mode 100644 Test/P06_NumpyStringFunctions.py create mode 100644 Test/P07_NumpyMathematicalFunctions.py create mode 100644 Test/P08_NumpyArithmeticOperations.py diff --git a/Test/P01_Introduction.py b/Test/P01_Introduction.py new file mode 100644 index 0000000..95dbd2f --- /dev/null +++ b/Test/P01_Introduction.py @@ -0,0 +1,83 @@ +# Author: OMKAR PATHAK + +# NumPy (Numeric Python) is a Python package used for building multi dimensional arrays and performing +# various operations + +# In this program we will walk through various concepts and see available functions in the NumPy package. + +# For installing: pip3 install numpy + +import numpy as np + +# we have a function arange() which makes an array of the specified dimension. Example: +myArray = np.arange(20) +print(myArray) # [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19] + +# an array from 10 to 20 +myArray = np.arange(10, 20) # [10 11 12 13 14 15 16 17 18 19] +print(myArray) + +# an array from 10 to 20 with 2 steps +myArray = np.arange(10, 20, 2) +print(myArray) # [10 12 14 16 18] + +# reshape() helps to reshape our NumPy array +myArray = np.arange(20) +# syntax: reshape(number_of_rows, number_of_columns) +myArray = myArray.reshape(4, 5) +print(myArray) + +# [[ 0  1  2  3  4] +#  [ 5  6  7  8  9] +#  [10 11 12 13 14] +#  [15 16 17 18 19]] + +myArray = myArray.reshape(10, 2) +print(myArray) + +# [[ 0  1] +#  [ 2  3] +#  [ 4  5] +#  [ 6  7] +#  [ 8  9] +#  [10 11] +#  [12 13] +#  [14 15] +#  [16 17] +#  [18 19]] + +# shape returns the shape of the array. The length of shape tuple is called as rank (or dimension) +print(myArray.shape) # (10, 2) + +# ndim returns the dimension (rank) of the array +print(myArray.ndim) # 2 + +# size returns the total number of elements in the array +print(myArray.size) # 20 + +# to check the data we have dtype. +print(myArray.dtype) # int64 + +# zeros creates an array will all zeros +myArray = np.zeros((3, 4)) +print(myArray) + +# [[ 0.  0.  0.  0.] +#  [ 0.  0.  0.  0.] +#  [ 0.  0.  0.  0.]] + +# ones creates an array with all ones +myArray = np.ones((3, 4)) +print(myArray) + +# [[ 1.  1.  1.  1.] +#  [ 1.  1.  1.  1.] +#  [ 1.  1.  1.  1.]] + +# numpy random module helps to initialize array with random values +myArray = np.random.rand(3, 4) +print(myArray) + +# [[ 0.54808903  0.08750717  0.23886267  0.93589283] +#  [ 0.90750146  0.31197039  0.54013725  0.91092763] +#  [ 0.38827674  0.04647878  0.15997665  0.94909537]] diff --git a/Test/P02_NumpyDataTypes.py b/Test/P02_NumpyDataTypes.py new file mode 100644 index 0000000..fd33774 --- /dev/null +++ b/Test/P02_NumpyDataTypes.py @@ -0,0 +1,34 @@ +# Author: OMKAR PATHAK + +# Data type Description +# bool_ Boolean (True or False) stored as a byte +# int_ Default integer type (same as C long; normally either int64 or int32) +# intc Identical to C int (normally int32 or int64) +# intp Integer used for indexing (same as C ssize_t; normally either int32 or int64) +# int8 Byte (-128 to 127) +# int16 Integer (-32768 to 32767) +# int32 Integer (-2147483648 to 2147483647) +# int64 Integer (-9223372036854775808 to 9223372036854775807) +# uint8 Unsigned integer (0 to 255) +# uint16 Unsigned integer (0 to 65535) +# uint32 Unsigned integer (0 to 4294967295) +# uint64 Unsigned integer (0 to 18446744073709551615) +# float_ Shorthand for float64. +# float16 Half precision float: sign bit, 5 bits exponent, 10 bits mantissa +# float32 Single precision float: sign bit, 8 bits exponent, 23 bits mantissa +# float64 Double precision float: sign bit, 11 bits exponent, 52 bits mantissa +# complex_ Shorthand for complex128. +# complex64 Complex number, represented by two 32-bit floats (real and imaginary components) +# complex128 Complex number, represented by two 64-bit floats (real and imaginary components) + +import numpy as np + +# while creating a numpy array, any data type from above can be explicitly specified. +myArray = np.arange(10) +print(myArray) # [0 1 2 3 4 5 6 7 8 9] + +myArray = np.array(myArray, dtype = np.float32) +print(myArray) # [ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9.] + +myArray = np.array(myArray, dtype = np.complex64) +print(myArray) # [ 0.+0.j  1.+0.j  2.+0.j  3.+0.j  4.+0.j  5.+0.j  6.+0.j  7.+0.j  8.+0.j 9.+0.j] diff --git a/Test/P03_NumpyAttributes.py b/Test/P03_NumpyAttributes.py new file mode 100644 index 0000000..538689a --- /dev/null +++ b/Test/P03_NumpyAttributes.py @@ -0,0 +1,24 @@ +# Author: OMKAR PATHAK + +# These are the various attributes provided by NumPy. + +import numpy as np + +myArray = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) +print(myArray) + +# [[1 2 3] +#  [4 5 6] +#  [7 8 9]] + +# ndarray.size returns the number of items in the array +print(myArray.size) # 9 + +# ndarray.shape returns a tuple consisting of array dimensions +print(myArray.shape) # (3, 3) + +# ndarray.ndim returns the number of array dimensions +print(myArray.ndim) # 2 + +# ndarray.itemsize returns the memory size of each element in the array +print(myArray.itemsize) # 8 diff --git a/Test/P04_ArrayFromNumericalRanges.py b/Test/P04_ArrayFromNumericalRanges.py new file mode 100644 index 0000000..e8dbbf5 --- /dev/null +++ b/Test/P04_ArrayFromNumericalRanges.py @@ -0,0 +1,43 @@ +# Author: OMKAR PATHAK + +# This program illustrates how to create an adarray from numerical ranges + +import numpy as np + +# ndarray.arange(start, stop, step, dtype) +# Creates a numpy array from 1 to 20 +myArray = np.arange(1, 21) +print(myArray) # [ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20] + +# Specifying data type of each element +myArray = np.arange(10, dtype = 'float') +print(myArray) # [ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9.] + +# Specifying steps to jump in between two elements +myArray = np.arange(1, 21, 2) +print(myArray) # [ 1  3  5  7  9 11 13 15 17 19] + +# ndarray.linspace(start, stop, num, endpoint, retstep, dtype) +# Shows 5 equal intervals between 10 to 20 +myArray = np.linspace(10, 20, 5) +print(myArray) # [ 10.   12.5  15.   17.5  20. ] + +# if endpoint is set to false the last number inn STOP parameter is not executed +myArray = np.linspace(10, 20, 5, endpoint = False) +print(myArray) # [ 10.  12.  14.  16.  18.] + +# ndarray.lopspace returns an ndarray object that contains the numbers that are evenly spaced +# on a log scale. +# ndarray.logscale(start, stop, num, endpoint, base, dtype) +# default base is 10 +myArray = np.logspace(1.0, 3.0, num = 10) +print(myArray) + +# [   10.            16.68100537    27.82559402    46.41588834    77.42636827 +#    129.1549665    215.443469     359.38136638   599.48425032  1000.        ] + +myArray = np.logspace(1.0, 3.0, num = 10, base = 2) +print(myArray) + +# [ 2.          2.33305808  2.72158     3.1748021   3.70349885  4.32023896 +#   5.0396842   5.87893797  6.85795186  8.        ] diff --git a/Test/P05_NumpyArrayManipulation.py b/Test/P05_NumpyArrayManipulation.py new file mode 100644 index 0000000..9a070c5 --- /dev/null +++ b/Test/P05_NumpyArrayManipulation.py @@ -0,0 +1,83 @@ +# Author: OMKAR PATHAK + +# This example shows various array manipulation operations +import numpy as np + +# numpy.reshape(array_to_reshape, tuple_of_new_shape) gives new shape (dimension) to our array +myArray = np.arange(0, 30, 2) +print(myArray) # [ 0  2  4  6  8 10 12 14 16 18 20 22 24 26 28] + +myArrayReshaped = myArray.reshape(5, 3) +print(myArrayReshaped) + +# [[ 0  2  4] +#  [ 6  8 10] +#  [12 14 16] +#  [18 20 22] +#  [24 26 28]] + +# numpy.ndarray.flat() returns an 1-D iterator +print(myArray.flat[5]) # 10 + +# numpy.ndarray.flatten() restores the reshaped array into a 1-D array +print(myArrayReshaped.flatten()) + +# numpy.tranpose() this helps to find the tranpose of the given array +print(myArrayReshaped.transpose()) + +# [[ 0  6 12 18 24] +#  [ 2  8 14 20 26] +#  [ 4 10 16 22 28]] + +# numpy.swapaxes(array, axis1, axis2) interchanges the two axes of an array +originalArray = np.arange(8).reshape(2,2,2) +print(originalArray) + +# [[[0 1] +#   [2 3]] +#   +#  [[4 5] +#   [6 7]]] + +print(np.swapaxes(originalArray, 2, 0)) + +# [[[0 4] +#   [2 6]] +#   +#  [[1 5] +#   [3 7]]] + +# numpy.rollaxis(arr, axis, start) rolls the specified axis backwards, until it lies in a specified position +print(np.rollaxis(originalArray, 2)) + +# [[[0 2] +#   [4 6]] +#   +#  [[1 3] +#   [5 7]]] + +# numpy.resize(arr, shape) returns a new array with the specified size. If the new size is greater than +# the original, the repeated copies of entries in the original are contained + +myArray = np.array([[1,2,3],[4,5,6]]) +print(myArray) + +# [[1 2 3] +#  [4 5 6]] + +print(np.resize(myArray, (3, 2))) + +# [[1 2] +#  [3 4] +#  [5 6]] + +# numpy.append(array, values, axis) +myArray = np.array([[1,2,3],[4,5,6]]) +print(myArray) + +# [[1 2 3] +#  [4 5 6]] + +print(np.append(myArray, [7, 8, 9])) + +# [1 2 3 4 5 6 7 8 9] diff --git a/Test/P06_NumpyStringFunctions.py b/Test/P06_NumpyStringFunctions.py new file mode 100644 index 0000000..2337a25 --- /dev/null +++ b/Test/P06_NumpyStringFunctions.py @@ -0,0 +1,43 @@ +# Author: OMKAR PATHAK + +import numpy as np + +abc = ['abc'] +xyz = ['xyz'] + +# string concatenation +print(np.char.add(abc, xyz)) # ['abcxyz'] + +print(np.char.add(abc, 'pqr')) # ['abcpqr'] + +# string multiplication +print(np.char.multiply(abc, 3)) # ['abcabcabc'] + +# numpy.char.center: This function returns an array of the required width so that the input string is +# centered and padded on the left and right with fillchar. + +print(np.char.center(abc, 20, fillchar = '*')) # ['********abc*********'] + +# numpy.char.capitalize(): This function returns the copy of the string with the first letter capitalized. +print(np.char.capitalize('hello world')) # Hello world + +# numpy.char.title(): This function returns a title cased version of the input string with the first letter +# of each word capitalized. +print(np.char.title('hello how are you?')) # Hello How Are You? + +# numpy.char.lower(): This function returns an array with elements converted to lowercase. It calls +# str.lower for each element. +print(np.char.lower(['HELLO','WORLD'])) # ['hello' 'world'] + +# numpy.char.upper(): This function calls str.upper function on each element in an array to return +# the uppercase array elements. +print(np.char.upper('hello')) # HELLO + +# numpy.char.split(): This function returns a list of words in the input string. By default, a whitespace +# is used as a separator +print(np.char.split('Omkar Pathak')) # ['Omkar', 'Pathak'] +print(np.char.split('2017-02-11', sep='-')) # ['2017', '02', '11'] + +# numpy.char.join(): This method returns a string in which the individual characters are joined by +# separator character specified. +print(np.char.join(':','dmy')) # d:m:y diff --git a/Test/P07_NumpyMathematicalFunctions.py b/Test/P07_NumpyMathematicalFunctions.py new file mode 100644 index 0000000..3929826 --- /dev/null +++ b/Test/P07_NumpyMathematicalFunctions.py @@ -0,0 +1,30 @@ +# Author: OMKAR PATHAK + +import numpy as np + +angles = np.array([0, 30, 45, 60, 90, 180, 360]) + +# Convert to radians by multiplying with pi/180 +# for getting sine of angles +print(np.sin(angles * np.pi/180)) + +# for getting cosine of angles +print(np.cos(angles * np.pi/180)) + +# for getting tangent of angles +print(np.tan(angles * np.pi/180)) + +# for computing inverse of trigonometric functions +sine = np.sin(angles * np.pi/180) +sineinv = np.arcsin(sine) +# computing angle from inverse +print(np.degrees(sineinv)) + +# for rounding the values +print(np.around(sine, 4)) # [ 0.      0.5     0.7071  0.866   1.      0.     -0.    ] + +# for rounding to previous integer +print(np.floor(sine)) # [ 0.  0.  0.  0.  1.  0. -1.] + +# for rounding to next integer +print(np.ceil(sine)) # [ 0.  1.  1.  1.  1.  1. -0.] diff --git a/Test/P08_NumpyArithmeticOperations.py b/Test/P08_NumpyArithmeticOperations.py new file mode 100644 index 0000000..4c74718 --- /dev/null +++ b/Test/P08_NumpyArithmeticOperations.py @@ -0,0 +1,25 @@ +# Author: OMKAR PATHAK + +import numpy as np + +firstArray = np.arange(12).reshape(3, 4) +print(firstArray) + +secondArray = np.arange(4) +print(secondArray) + +# adding above two arrays (NOTE: array shapes should be same) +print(np.add(firstArray, secondArray)) + +# subtracting above two arrays +print(np.subtract(firstArray, secondArray)) + +# multiplying above two arrays +print(np.multiply(firstArray, secondArray)) + +# dividing the above two arrays +print(np.divide(firstArray, secondArray)) + +# numpy.power(): returns array element raised to the specified value result +array = np.array([1, 2, 3]) +print(np.power(array, 2)) # [1 4 9]