DEV Community

Cover image for The Numpy
kainat Raisa
kainat Raisa

Posted on

The Numpy

What we do with Numpy?

The biggest reason behind the popularity of Python programming language is the huge number of built-in libraries it has. Now the question comes to your mind, what is a library? In computer programming a library refers to a collection of some pre-written functionalities which can be reused in any Python code. Python has around 137000 built-in libraries. Numpy(can be written also as NumPy or numpy)is one of the most popular library of Python. Python programmers mainly use this library to work with array,linear algebra, fourier transform and many more mathematical operations.

How to use Numpy

Before using, the Numpy library needs to be installed in the virtual environment we are working in. You can install numpy just by writing "pip install numpy" in the terminal/console.
Then you have to import numpy by writing "import numpy" on the top of your Python code.

Image description
Or you can import this library as another name(programmers are too lazy to type this five letter word so they import numpy as np).

Image description

How to work with Arrays using Numpy

An array is a Data structure where large number of data can be stored inside a single named space. Arrays can be both one and multidimensional. We can create and panipulate arrays using Numpy library. "numpy.array()" or "np.array()" can be used to create an array.

Image description
This is a one dimensional array. A one dimensional array can be considered as a Python list. You can do indexing,slicing, and assign a new value to the one dimensional array just the way you do it with a list.

Image description
You can check the shape(how many rows and columns are there), the size(how many elements does the array have) or reshape a multinational array with the shape,size attribute and reshape() method.

Image description

random.choice and random.randint

The random.randint(lower limit,upper limit) is used(random and randint are methods) to generate random integers between the given range.

Image description

On the otherhand random.choice(sequence)
generates a random value from a given sequence(list,tuple,string etc.).

Image description

Top comments (0)