Practical6 Python Programming
Practical6 Python Programming
PRACTICAL 6
Part A (To be referred by students)
2. Create a 5 * 6 array between 10 to 1000 such that the difference between the elements
are 12 and they are only even numbers. Print the even rows and odd columns from the
array.
3. Take user input for dimension of matrix and create the following pattern. Below is for
8 * 8 pattern.
Theory:
● NumPy is 'Numerical Python package consisting of multidimensional array (ndarray)
objects and a collection of routines for processing of array.
1 | Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
Course: Python Programming
PROGRAMME: B.Tech/MBATech.
First Year AY 2022-2023 Semester: II
NumPy’s array class is called ndarray. The important attributes of an ndarray object are:
ndarray.ndim - the number of axes (dimensions) of the array.
ndarray.shape - the dimensions of the array. This is a tuple of integers indicating the
size of the array in each dimension. For a matrix with n rows and m columns, shape will
be (n,m). The length of the shape tuple is therefore the number of axes, ndim.
ndarray.size -the total number of elements of the array. This is equal to the product of
the elements of shape.
ndarray.dtype -an object describing the type of the elements in the array. One can create
or specify dtype’s using standard Python types. Additionally, NumPy provides types of
its own. numpy.int32, numpy.int16, and numpy.float64 are some examples.
ndarray.itemsize - the size in bytes of each element of the array. For example, an array
of elements of type float64 has itemsize 8 (=64/8), while one of type complex32 has
itemsize 4 (=32/8). It is equivalent to ndarray.dtype. itemsize.
ndarray.data - the buffer containing the actual elements of the array. Normally, we
won’t need to use this attribute because we will access the elements in an array using
indexing facilities.
Example –
import numpy as np print( a.itemsize)
a = np.arange(15).reshape(3, 5) 8
print(a) print( a.size)
array([[ 0, 1, 2, 3, 4], 15
[ 5, 6, 7, 8, 9], print( type(a))
[10, 11, 12, 13, 14]]) <class 'numpy.ndarray'>
print( a.shape) print( b = np.array([6, 7, 8])
(3, 5) print( b)
print(a.ndim) array([6, 7, 8])
2 print( type(b))
print( a.dtype.name) <class 'numpy.ndarray'>
'int64'
Array Creation There are several ways to create arrays. For example, you can create an
array from a regular Python list or tuple using the array function
2 | Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
Course: Python Programming
PROGRAMME: B.Tech/MBATech.
First Year AY 2022-2023 Semester: II
np.array([1.2, 3.5, arguments but 4 were print( c = np.array([[1, 2], [3, 4]],
5.1]) given dtype=complex)
print( b.dtype) print( a = np.array([1, 2, print( c)
dtype('float64') 3, 4]) # RIGHT array([[1.+0.j, 2.+0.j],
[3.+0.j, 4.+0.j]])
To create sequences of numbers, NumPy provides the arange function which is analogous
to the Python built-in range, but returns an array.
print( np.arange(10, 30, 5))
array([10, 15, 20, 25])
print(np.arange(0, 2, 0.3)) # it accepts float arguments
array([0. , 0.3, 0.6, 0.9, 1.2, 1.5, 1.8])
Universal Functions
NumPy provides familiar mathematical functions such as sin, cos, and exp
The shape of an array can be changed with various commands. Note that the following
three commands all return a modified array, but do not change the original array:
3 | Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
Course: Python Programming
PROGRAMME: B.Tech/MBATech.
First Year AY 2022-2023 Semester: II
4 | Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
Course: Python Programming
PROGRAMME: B.Tech/MBATech.
First Year AY 2022-2023 Semester: II
PRACTICAL 6
Part B (to be completed by students)
1. Program Code along with Sample Output: (Paste your programs [1,2,3], input and
output screen shot for programs [1,2,3])
1. a) Program-
import numpy as np
arr = np.ones((3,3))
print("Numpy array with all ones:")
print(arr)
Output-
b) Program-
import numpy as np
arr = np.random.randint(5, 11, size=(3, 5))
print(arr)
Output-
5 | Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
Course: Python Programming
PROGRAMME: B.Tech/MBATech.
First Year AY 2022-2023 Semester: II
c) Program-
import numpy as np
matrix = np.identity(4)
print(matrix)
mean_value = np.mean(matrix)
print("Mean value of the matrix is: ", mean_value)
Output-
d) Program-
Output-
6 | Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
Course: Python Programming
PROGRAMME: B.Tech/MBATech.
First Year AY 2022-2023 Semester: II
2. Program-
import numpy as np
arr = np.zeros((5, 6), dtype=int)
start = 10
for i in range(5):
for j in range(6):
arr[i][j] = start
start += 12
while start % 2 != 0:
start += 12
print(arr)
even_rows_odd_cols = arr[::2, 1::2]
print("\nEven rows and odd columns:")
print(even_rows_odd_cols)
Output-
3. Program-
import numpy as np
x = np.ones((3,3))
print("Checkerboard pattern:")
x = np.zeros((8,8),dtype=int)
x[1::2,::2] = 1
x[::2,1::2] = 1
print(x)
7 | Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
Course: Python Programming
PROGRAMME: B.Tech/MBATech.
First Year AY 2022-2023 Semester: II
Output-
2. Conclusion (Learning Outcomes): Reflect on the questions answered by you jot down
your learnings about the Topic.
8 | Page