1a[-1] # last item in the array
2a[-2:] # last two items in the array
3a[:-2] # everything except the last two items
4
1# array[start:stop:step]
2
3# start = include everything STARTING AT this idx (inclusive)
4# stop = include everything BEFORE this idx (exclusive)
5# step = (can be ommitted) difference between each idx in the sequence
6
7arr = ['a', 'b', 'c', 'd', 'e']
8
9arr[2:] => ['c', 'd', 'e']
10
11arr[:4] => ['a', 'b', 'c', 'd']
12
13arr[2:4] => ['c', 'd']
14
15arr[0:5:2] => ['a', 'c', 'e']
16
17arr[:] => makes copy of arr
1a[start:stop] # items start through stop-1
2a[start:] # items start through the rest of the array
3a[:stop] # items from the beginning through stop-1
4a[:] # a copy of the whole array
5
1# array[start:stop:step]
2
3# start = include everything STARTING AT this idx (inclusive)
4# stop = include everything BEFORE this idx (exclusive)
5# step = (can be committed) difference between each idx in the sequence
6
7arr = ['a', 'b', 'c', 'd', 'e']
8
9arr[2:] => ['c', 'd', 'e']
10
11arr[:4] => ['a', 'b', 'c', 'd']
12
13arr[2:4] => ['c', 'd']
14
15arr[0:5:2] => ['a', 'c', 'e']
16
17arr[:] => makes copy of arr
1#slicing arrays:
2#generic sampling is done by
3#arr[start:end] -> where start is the starting index and end is ending idx
4>>> import numpy as np
5>>> arr = np.array([1,2,3,4,5])
6>>> print(arr[1:5]) #starting idx 1 to ending index 4
7[2 3 4 5]#it will print from starting idx to ending idx-1
8
9#if you leave the ending index blank it will print all
10#from the starting index till end
11>>> arr = np.array([2,6,1,7,5])
12>>> print(arr[3:])
13[7 5]
14>>> print(arr[:3]) #if you leave the starting index blank it will print from 0 index to the ending idx-1\
15[2 6 1]
16>>> print(arr[:])
17[2 6 1 7 5]
18#leaving both the index open will print the entire array.
19
20##########STEP slicing########
21#if you want to traverse by taking steps more than 1
22#we use step slicing in that case
23#syntax for step slicing is : arr[start:end:step]
24>>> arr = np.array([2,6,1,7,5,10,43,21,100,29])
25>>> print(arr[1:8:2])#we have taken steps of two
26[ 6 7 10 21]
27
28
29
30
31
1string = 'string_text'
2
3beginning = 0 # at what index the slice should start.
4end = len(string) # at what index the slice should end.
5step = 1 # how many characters the slice should go forward after each letter
6
7new_string = string[beginning:end:step]
8
9# some examples
10a = 0
11b = 3
12c = 1
13
14new_string = string[a:b:c] # will give you: str
15# ____________________________________________
16a = 2
17b = len(string) - 2
18c = 1
19
20new_string = string[a:b:c] # will give you: ring_te