# How to Multiply Array by Scalar in Python using NumPy

## Table of Contents

In Python, you can use the NumPy library to multiply an array by a scalar.

Because we are using a third-party library here, we can be sure that the code has been tested and is safe to use.

In this post, we'll learn how to use numpy to multiply all the elements in an array by a scalar.

## Multiply an array by a scalar

First, let's start off importing the numpy library.

```
PYTHONimport numpy as np
```

Now, let's contine by creating an array in Python:

```
PYTHONimport numpy as np
array1 = np.array([1, 2, 3, 4, 5])
```

Now let's define `n`

, as the number we want to multiply every element in the array by:

```
PYTHONimport numpy as np
array1 = np.array([1, 2, 3, 4, 5])
n = 5
```

To multiple every element, we can use the `*`

operator, and then print it:

```
PYTHONimport numpy as np
array1 = np.array([1, 2, 3, 4, 5])
n = 5
print(array1 * n)
```

```
BASH[5, 10, 15, 20, 25]
```

Alternatively, you can also use the `multiply`

function from numpy to multiply every element in the array by a scalar:

```
PYTHONimport numpy as np
array1 = np.array([1, 2, 3, 4, 5])
n = 5
new_array = np.multiply(array1, n)
print(new_array)
```

```
BASH[5, 10, 15, 20, 25]
```

In either case, you get the same result.

## Conclusion

In this post, we looked at two different ways to multiply every element in an array by a scalar.

You can either use the `*`

operator, or the `multiply`

function from numpy.

Thanks for reading!

## Resources

- Getting Started with Solid
- Managing PHP Dependencies with Composer
- Git Tutorial: Learn how to use Version Control
- How to Serve Static Files with Nginx and Docker
- How to Set Up Cron Jobs in Linux
- How to build a Discord bot using TypeScript
- How to deploy a PHP app using Docker
- Getting Started with Deno
- How to deploy a MySQL Server using Docker
- Getting Started with Handlebars.js
- Building a Real-Time Note-Taking App with Vue and Firebase
- Getting Started with Moon.js