1/26
Looks like no tags are added yet.
What is the first array index in Python?
0, not 1.
For x=[4,8,12], what is x[1]?
8 (the second entry).
What does x[-1] select?
The final element.
What does x[-2] select?
The second-to-last element.
In x[a:b], is b included?
No. Start a is included; stop b is excluded.
If x=[2,4,6,8,10], what is x[1:4]?
[4, 6, 8]
What does x[:3] select?
The first three entries (indices 0,1,2).
How can you reverse an entire array using slicing?
x[::-1]
What does x[2] = 0 do?
Replaces the entry at index 2 (third entry) with zero.
Why does x[10] fail for a 5-element array?
IndexError: valid nonnegative indices are 0 to 4.
For x=[1,3,5,7,9], what is x[1:-1]?
[3, 5, 7]
For x=np.array([10,20,30,40]), what is x[0]?
10
For x=np.array([10,20,30,40]), what is x[3]?
40
For a 6-element array, what is the last valid positive index?
5
For x=[10,20,30,40,50] as an array, what is x[2:5]?
[30 40 50]
For x=[10,20,30,40,50] as an array, what is x[2:2]?
[]; start equals stop, so the slice is empty.
For x=[0,1,2,3,4,5], what is x[1:6:2]?
[1 3 5]
For x=[0,1,2,3,4,5], what is x[4:1:-1]?
[4 3 2]
What are start, stop, and step in x[start:stop:step]?
Start included, stop excluded, step sets direction/stride.
Write a slice for entries at indices 2, 3, and 4
x[2:5]
Write a slice for the first four array elements
x[:4]
Write code to replace the first two entries with 0
x[:2] = 0
For x=np.array([2,4,6]), after x[1]=10, what is x?
[2 10 6]
For x=np.array([1,2,3,4]), what is x[::2]?
[1 3]
Why can x[2.0] produce an error even though 2.0 equals 2?
Array positions require integer indices, not floats.
What happens if you slice x[100:200] when x only has 5 entries?
You get an empty slice, not an IndexError.
What does x[1:4] = 0 do?
Replaces entries at indices 1, 2, and 3 with zero.