0% found this document useful (0 votes)
12 views5 pages

Minor Module5

This document discusses NumPy arrays including how to create, access, and manipulate them. NumPy arrays can be 1D, 2D or 3D. Methods like reshape, slicing, and sorting arrays are covered. The document also discusses random number generation and reading/writing files in Python.

Uploaded by

Indo Asia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

Minor Module5

This document discusses NumPy arrays including how to create, access, and manipulate them. NumPy arrays can be 1D, 2D or 3D. Methods like reshape, slicing, and sorting arrays are covered. The document also discusses random number generation and reading/writing files in Python.

Uploaded by

Indo Asia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Module5 Notes

Creating arrays, arithmetic, indexing, slicing

Numerical Python- library

1. How do you create a Numpy array?


Import numpy as np
Arr1=np.array([1,2,3,4])
Print(arr1)

Ans: [1,2,3,4,5]

You can create array of integers, float, complex numbers

 Type(arr1) gives you

To create an ndarray, we can pass a list, tuple or any array-like object


into the array() method, and it will be converted into an ndarray:

The above array [1,2,3,4,5] is considered as 1-D array.

2. How to create 2D array?


An array that has 1-D arrays as its elements is called a 2-D array.

import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr)

Ans: [[1 2 3]
[4 5 6]]

3. 3 D array?
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])

Here, the elements are 2 D arrays


4. How do you check dimension of an array?

Ans: 0, 1, 2, 3

5. Difference between python lists and Numpy array?


-List can contain any data type, Array contains only one data type values.
- Arrays are faster than lists
- Array consumes less memory, convenient to use

6. What are attributes of array?

Dtype ( specifies data type), ndim ( dimension of array), shape

The shape of an array is a tuple of non-negative integers that specify the sizes of each
dimension.
6. How to access array elements?
- By indexing

arr = np.array([1, 2, 3, 4])

print(arr[0]) Ans: 1

7. How to access element in 2 D array?


arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])

print('2nd element on 1st row: ', arr[0, 1])


8. How to access element in second row fifth column?
arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])

print('5th element on 2nd row: ', arr[1, 4])


Ans:10
9, How to do array slicing?

Same as list slicing

9. How do you convert a list into Numpy array?


10. How to create special arrays?
1. Array of zeros
A1= np.zeros((2,3))

2. Array of ones
A1=np.ones((2,3))
3. How to create identity matrix?
4. Constant array of size 4x4 with full elements 5
Np.full((4,4),5)
11. What is use of arange()?

1.

Ans:

12. How to create arrays of random numbers?


a. from numpy import random
x = random.randint(100)
print(x)

Ans: 37 ( Any random integer <100)


b. If you want a float number, use random.rand(100)
c. Generate a 1-D array containing 5 random integers from 0 to 100:
x=random.randint(100, size=(5))
Ans: [31 90 41 86 63]
d. Generate a 2-D array with 3 rows, each row containing 5 random
integers from 0 to 100:

x = random.randint(100, size=(3, 5))

Ans: [[90 99 11 30 34]


[66 40 63 36 37]
[63 35 89 51 58]]

e. Create 1D array with 5 random values from a standard normal distribution


X=np.random.randn(5)
( Means values range b/n -1 and +1)

13. What is reshape()?

14. How to sort elements in an array?

15. What are the methods of os and sys module?


16. Write a program to read the contents of a file and display it.
17.file = open('myfile.txt', 'r')
18. content = file.read()
19. print(content)
20. file.close()

17. Program to open an existing file, write one more line into it, display.
file = open('myfile.txt', 'a')
file.write('
New line of text')
file.close()
f1=open (‘myfile.txt’, r)

con= f1.read()

print(con)

21. Write a program to create a text file. Read the contents, convert into uppercase letters, write
into another file. Display its contents too..
22. What are different modes for opening a file?
R  opening a file for reading
w-> opening a file for writing only ( existing contents will be lost)
a opening a file for writing ( at the end of existing content) – append

You might also like