﻿ Python NumPy: Create a 2d array with 1 on the border and 0 inside - w3resource

# Python NumPy: Create a 2d array with 1 on the border and 0 inside

## Python NumPy: Array Object Exercise-8 with Solution

Write a Python program to create a 2d array with 1 on the border and 0 inside.

Sample Solution:-

Python Code:

``````import numpy as np
import numpy as np
x = np.ones((5,5))
print("Original array:")
print(x)
print("1 on the border and 0 inside in the array")
x[1:-1,1:-1] = 0
print(x)
```
```

Sample Output:

```Original array:
[[ 1.  1.  1.  1.  1.]
[ 1.  1.  1.  1.  1.]
[ 1.  1.  1.  1.  1.]
[ 1.  1.  1.  1.  1.]
[ 1.  1.  1.  1.  1.]]
1 on the border and 0 inside in the array
[[ 1.  1.  1.  1.  1.]
[ 1.  0.  0.  0.  1.]
[ 1.  0.  0.  0.  1.]
[ 1.  0.  0.  0.  1.]
[ 1.  1.  1.  1.  1.]]
```

Python Code Editor:

```import numpy as np
x = np.ones((5,5))
print("Original array:")
print(x)
print("1 on the border and 0 inside in the array")
x[1:-1,1:-1] = 0
print(x)```

Improve this sample solution and post your code through Disqus