Zed-King Study app · Free
O Level students across India practise here, free
Google login · no Play Store needed
About this PDF
M3 Chapter 9 notes — NumPy basics for O Level: creating arrays, array shape and indexing, slicing, and the arithmetic and aggregate operations used on arrays.
This is part of the free O Level M3-R5.1 study material in the Zed-King Study Zone. You can read every page above in your browser, or download the file and study offline. Zed-King Group of Institute is a NIELIT Study Centre in Kaithal, Haryana, and all of this material follows the current NIELIT syllabus.
- Course
- O Level — NIELIT Foundation Diploma
- Module
- M3 — M3-R5.1: Programming and Problem Solving through Python
- Section
- Chapterwise Notes
- Format
- PDF — read online or download
- Price
- Free, no sign-up required
Text from this PDF
The opening pages as plain text — open the reader above for tables, diagrams and the full file.
CHAPTER-9 Numpy Basics NumPy:- Numpy is a library of python through which operations can be performed multidimensional arrays in python.
The full name of NumPy is Numerical Python.
Check PIP (preferred installer package (Program) )package:-
1. Go to cmd
2. Type pip list command
3. Press enter
4. If screen show
5. It means pip is installed already
6. If pip is not installed
7. Then screen show
8. Pio is not recognized as an internal or external command Download & install numPy:-
1. Go to cmd
2. Type pip install numpy
3. Press enter install nunpy Collecting nunpy Obtaining dependency information for nunpy fron https://fites.pythonhosted.org/packages/28/75/3b679b4171 87be72c971c9e718b1c17b8f8749249ddca8/nunpy-1.26.2-cp312-cp312-win_and6u. "hl. metadata Downloading .netadata (61 kB)
eta Downloading nunpy-1.26.2-cp312-cp312-win_and6u.wh1 (15.5 "B)
eta Installing collected packages: numpy Successfully installed numpy-I.26.2 ] A new release of pip is available:
To update, run: python .exe -n pip install --upgrade pip Check numPy Installation:-
1. Go to cmd
2. Type pip list command
3. Press enter C:\Users\Akhilesh>pip list version Package asgiref Django nunpy pip sqlparse 0 tzdata A new release of pip is available:
To update, run: python .exe -n pip install --upgrade pip Creation Numpy Array (One Dimensional)
Creation Numpy Array (One Dimensional)
myarr=np.array(a)
pong(array)
Through User Input:
a ill myarr=np.array([1,2,3,4,51)
for i in range(n):
val=int(input("Enter Number"))
Creation Numpy Array (Two Dimensional)
impert nymPY as np myarr=np.array(a)
Array Attribute:- impert as np Numpy array is a set of attributes that you can access like array size, shape, number of dimensions etc.
ndim:- it display the dimensions of array.
print("Total no of dimension= ",arr.ndim)
Shape:- it return a tuple of integers indicating the size of the array.
print("Shape of Array is= ",arr.shape)
Shape of Array is= Size:- it return the total number of elements in the NumPy array.
print(llsize of Array is: ll,arr.size)
size of Array is: 12 Dtype:- it return the type of elements in the array. i.e. int 64.
arr=np.array([[1.5,2.6,3.4]])
data type is= float64 itemsize:- it return the length of each element of array in bytes.
arr=np.array([[1,2,3],[5,6,7]l)
each item contain—
• — 4 bytes Reshape:- the reshape() function associated with the ndarray object is used to reshape the array. It accept the two parameters indicating the row and columns of the new shape of the array.
a=a.reshape(3,4)
print("reshaped array is ")
Numpy functions Zeros:- this function creates an array (either one dimensional or multi dimensional) and fill all the values with Zero (O).
arr= np.zeros(5)
arr= np.zeros((3,4))
Ones:- this function creates an array (either one dimensional or multi dimensional) and fill all the values with one (I).
arr= Eye:- this function creates an array with all the diagonal elements as 1 and rest as O.
arr=np.eye(3,4)
arr=np.eye(3)
Diag:- this function creates a two dimensional array with all the diagonal elements as the given value and rest as O.
arr=np.diag([I,2,3,4])
Randint:- This function is used to generate a random number between a given range.
Syntax:- arr=np.random.randint(I,IO,3)
rand:- This function is used to generate a random values between O to 1.
Syntax:- rand(number of value)
arr=np.random.rand(2,3)
arr=np.random.rand(5)
randn:- This function is used generate a random values close to O (zero). This may return positive or negative numbers as well.
randn(number of value)
Syntax:- arr= np.random.randn(5)
seed:- we know that randint() function generates random numbers. Every time we run the program, new set of random number is generated. But what if we want to fix the generation of this random number. In this case we use seed, this generates fix set of random numbers.
np.random.seed(10)
arr=np.random.randint(1,50,10)
Array Front Nun•erical Ranges: — Np-arange:— arrange(O returns an ndarray object containing evenly spaced values vvithin a given range.
Syntax:— nurnpy-arange(start.stop.step.dtype)
i nn port nurnpy as n p print (arr)
Conditional Selection inn port nurnpy as n p a r r — np_ara nge( 1.20.1 )
b = a rr > 10 print (a )
Multiply Nun•ber Printing i rnport nurnpy as n p print(arrW2)
Even Nurnber Printing arr= np-arange(l .20, 1 )
O u t put Exponent Nurnber Printing print(arr/O)
[inf inf inf infl Note:— it nurnpy array divide by zero gives infinity and not any error Exponent Nurnber Printing arr= Np.linspace:- linspace() function returns values between a given range and with a same gap between consecutive elements.
Syntax:— a=np.linspace(5,15,5)
More chapterwise notes for M3-R5.1
Run these Python programs yourself — free
Reading code is not the same as writing it. Type any program from these notes into our free online Python compiler (real Python 3 with NumPy, input() works), or study one of the explained programs with its output.
Prepare for O Level with expert guidance
Classroom training, practical labs and exam registration at Zed-King Institute, Kaithal.
