Numpy Exercise : Bascis

  1. Create an array ( x1 ) with sequence of number starting from 9 to 17 ( inclusive )
  2. Create one more array x2 in which each element I = 2 (element of x1) + 8
  3. Create an array with 4 numbers starting from 2 to 20
  4. Create an array starting from 2 to 20 and each element is increased by 3
  5. Create another array of same shape of x1
  6. Create another array of same shape of x1 but with different random values.

Create an array ( x1 ) with sequence of number starting from 9 to 17 ( inclusive )

how to create an array with start, stop and step by using arange()
import numpy as np
x1=np.arange(9,18)
print(x1) # [ 9 10 11 12 13 14 15 16 17] 

Create one more array x2 in which each element I = 2 (element of x1) + 8

x2=(2*x1) +8
print(x2) # [26 28 30 32 34 36 38 40 42]

Create an array with 4 numbers starting from 2 to 20

How to create an array with start stop and number of steps by using linspace()
x3=np.linspace(2,20,4)
print(x3) # [ 2.  8. 14. 20.]

Create an array starting from 2 to 20 and each element is increased by 3

x4=np.arange(2,21,3)
print(x4) # [ 2  5  8 11 14 17 20]

Create another array of same shape of x1

import numpy as np
x1=np.arange(9,18)
print(x1)
s1=x1.shape
y1=np.ones(s1)
print(y1)
Output
[ 9 10 11 12 13 14 15 16 17]
[1. 1. 1. 1. 1. 1. 1. 1. 1.]

Create another array of same shape of x1 but with different random values.

Read more on shape.
import numpy as np
x1=np.arange(9,18)
print(x1)
s1=x1.shape
y1=np.ones(s1)
y1=np.random.randint(4,high=10,size=s1) 
print(y1)
Output
[ 9 10 11 12 13 14 15 16 17]
[7 4 5 7 7 6 4 4 4]
Numpy ones() bincount() arange()
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer