## DEV Community 👩‍💻👨‍💻 is a community of 921,001 amazing developers

We're a place where coders share, stay up-to-date and grow their careers. Bernice Waweru

Posted on • Updated on

# NumPy Basics : Part 1

This post aims to cover the basics of Numpy. Let's do this.

### Introduction

NumPy refers to Numerical Python which is a Python library used array manipulation.

To use the NumPy library import it as:

`import numpy as np`
np is the conventional alias for numpy.

The main object of Numpy is the ndarray (N-dimensional array) object which is a powerful and faster that Python lists. The ndarray is a multidimensional array of homogeneous data; all elements in the array have the same data type.

### Creating a Numpy array

Use the ndarray class to create ndarray objects and access their attributes and methods.

Using the numpy.array() function

``````import numpy as np
a = np.array([1, 2, 3])
``````

You can also create an array with zeros only or ones only

``````#array filled with zeros; creates array with 5 zeros
arrZeros = np.zeros(5)
#array filled with ones; creates array with 4 ones
arrOnes = np.ones(4)
``````

You can also create an empty array which can be filled later.

``````# Create an empty array with 3 elements
arrEmpty = np.empty(3)
``````

You can also create an array using numpy.arange()

``````# output is a range from 0 to the specified number but not #including that number.([0, 1, 2, 3,4])
arrRange = np.arange(5)
array([0, 1, 2, 3,4])
``````

You can also specify the first number, last number, and the step size in the range.

``````np.arange(1, 9, 2)
array([1, 3, 5, 7]
``````

### Attributes

Let's use this example to understand ndarray attributes.

``````array_A = np.array([[2,4,6], [1,3,5]])

``````

1. ndarray.ndim: The number of dimensions (axes) of the array.
The ndim for array_A is 2.

2. ndarray.dtype: The data type of the elements in the array. The dtype in our example is int64.
You can specify the dtype when creating an array using the dtype keyword.

``````# array of ones.
a = np.ones(3, dtype=np.int64)
``````

3. ndarray.shape: The number of elements along with each axis.
The shape is a tuple of N-positive integers that specifies the number of elements of each dimension.
For our example the shape is (2,3) because the array has two rows and three columns.
Ps: The length of the shape tuple is the number of dimensions, ndim.

4. ndarray.size: The total number of elements in the array.
It is equal to the product of the elements of shape.
The size of our example array is 6. i.e 2*3

### Indexing

Indexing in Numpy works similarly to indexing in python lists.

For a one dimensional array, values can be accessed by specifying the desired index in square brackets counting from 0.
```syntax: array_x[start:stop:step] ```

``````import numpy as np
array_x = np.array([5,6,7,8,9])
array_x
``````

Output:
```array([5, 6, 7, 8, 9]) ```
Item at index 0

``````array_x
``````

Output:
```5 ```Items from index 0 to 3 but not including 3.

``````array_x[:3]
``````

Output:

```array([5, 6, 7]) ```
Items from index 3 to the last element.

``````array_x[3:]
``````

Output:

```array([8, 9]) ```
Items in the array taking a step size of 2

``````array_x[0:-1:2]
``````

Output:
```array([5, 7]) ```

In a multi-dimensional array, values can be accessed using a comma-separated tuple of indices. the first value specifies the row while the second specifies the column.

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

Output:

``````array_A[0,0]
``````

```2 ```

``````array_A[1, 1]
``````

Output:
`3`
You can also use indexing to change the value at a given index.

``````array_A[1, 1]=7
array_A
``````

Output:

```````array([[2, 4, 6],
[1, 7, 5]])`
``````

Advantages of using Numpy Arrays.

1. Numpy data structures take up less memory.
2. Numpy arrays are faster than lists.
3. NumPy arrays have homogeneous data types and allow for mathematical manipulation.

## Top comments (0)

### 🌚 Browsing with dark mode makes you a better developer.

It's a scientific fact.
{"ft_lang_label":"__label__en","ft_lang_prob":0.67517483,"math_prob":0.9746024,"size":3445,"snap":"2022-40-2023-06","text_gpt3_token_len":944,"char_repetition_ratio":0.14007556,"word_repetition_ratio":0.01208981,"special_character_ratio":0.278955,"punctuation_ratio":0.17227979,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9975995,"pos_list":[0,1,2],"im_url_duplicate_count":[null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-10-05T22:21:06Z\",\"WARC-Record-ID\":\"<urn:uuid:69b3798a-cdb6-4921-a75f-a25992864774>\",\"Content-Length\":\"116884\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:da44b9ec-8f5a-4f78-adea-c42c2750b492>\",\"WARC-Concurrent-To\":\"<urn:uuid:fb0435a6-ece2-48d5-bff7-1766bccea78f>\",\"WARC-IP-Address\":\"146.75.33.194\",\"WARC-Target-URI\":\"https://practicaldev-herokuapp-com.global.ssl.fastly.net/wanguiwaweru/numpy-basics-part-1-47e1\",\"WARC-Payload-Digest\":\"sha1:AK5DEBTOIMECJ7VDD5MWTUI7GBIG32RW\",\"WARC-Block-Digest\":\"sha1:CPDHGQEEOJA2SGYE2PQAXQHDQWAGGTZR\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-40/CC-MAIN-2022-40_segments_1664030337668.62_warc_CC-MAIN-20221005203530-20221005233530-00724.warc.gz\"}"}