Python NumPy Array Slicing | Part 83

Published: 03 July 2023
on channel: Arvind Programming
18
like

Python NumPy Array Slicing @arvindprogramming
To slice a Numpy array, you can use the following syntax:

```python
new_array = array_name[start_index:end_index:step]
```

where:
`array_name` is the name of the Numpy array you want to slice.
`start_index` is the index at which the slicing will start (inclusive).
`end_index` is the index at which the slicing will end (exclusive).
`step` is the number of steps to take between the start and end indices (optional).

Here are a few examples to demonstrate how to slice a Numpy array:

1. Slicing the entire array:
```python
import Numpy as np

array = np.array([1, 2, 3, 4, 5])
new_array = array[:]
print(new_array) # Output: [1, 2, 3, 4, 5]
```

2. Slicing a specific range:
```python
import Numpy as np

array = np.array([1, 2, 3, 4, 5])
new_array = array[1:4]
print(new_array) # Output: [2, 3, 4]
```

3. Slicing with a step:
```python
import Numpy as np

array = np.array([1, 2, 3, 4, 5])
new_array = array[::2]
print(new_array) # Output: [1, 3, 5]
```

Note that slicing Numpy arrays does not create a new copy of the array; instead, it creates a view that shares the same underlying data. Any modifications made to the sliced array will also affect the original array.
Social Media Links-
Facebook Page --   / arvindweb   Coaching-Programming & Games/
Facebook -   / arvindweb  
Twitter -   / arvindwebspn  
YouTube -    / @arvindprogramming  
Instagram -   / arvindprogramming  


On this page of the site you can watch the video online Python NumPy Array Slicing | Part 83 with a duration of hours minute second in good quality, which was uploaded by the user Arvind Programming 03 July 2023, share the link with friends and acquaintances, this video has already been watched 18 times on youtube and it was liked by like viewers. Enjoy your viewing!