DEV Community

Cover image for Numpy and Python
petercour
petercour

Posted on

Numpy and Python

If you want to work with arrays, matrices and more, the module numpy is great. Without numpy, you would be using loops to create lists of numbers appending the numbers and so. That isn't very practical.

Then there's numpy. You can easily create lists of numbers with numpy. This can be single list numbers like ones, zeroes or lists that contain incremental values. It even supports multi-dimensional lists. The program below creates different lists.

#!/usr/bin/python3
import numpy

x = numpy.zeros(6)
print(x)
x = numpy.zeros((2,3)) 
print(x) 
x = numpy.ones((2,3)) 
print(x) 
x = numpy.empty((3,3))
print(x)

print(numpy.arange(6))
print(numpy.arange(0,6,2) )
Enter fullscreen mode Exit fullscreen mode

Output of the above program

[0. 0. 0. 0. 0. 0.]
[[0. 0. 0.]
 [0. 0. 0.]]
[[1. 1. 1.]
 [1. 1. 1.]]
[[0.00000000e+000 1.53582796e-316 6.93295664e-310]
 [6.93294681e-310 6.93295664e-310 6.93294655e-310]
 [6.93294681e-310 6.93294681e-310 3.95252517e-322]]
 [0 1 2 3 4 5]
 [0 2 4]
Enter fullscreen mode Exit fullscreen mode

This created different lists of numbers. You can store the output (x =) or directly output it to the screen with the print function

Related links:

Top comments (0)