Indexing and Slicing - Exam 1

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/26

encourage image

There's no tags or description

Looks like no tags are added yet.

27 Terms

1
New cards

What is the first array index in Python?

0, not 1.

2
New cards

For x=[4,8,12], what is x[1]?

8 (the second entry).

3
New cards

What does x[-1] select?

The final element.

4
New cards

What does x[-2] select?

The second-to-last element.

5
New cards

In x[a:b], is b included?

No. Start a is included; stop b is excluded.

6
New cards

If x=[2,4,6,8,10], what is x[1:4]?

[4, 6, 8]

7
New cards

What does x[:3] select?

The first three entries (indices 0,1,2).

8
New cards

How can you reverse an entire array using slicing?

x[::-1]

9
New cards

What does x[2] = 0 do?

Replaces the entry at index 2 (third entry) with zero.

10
New cards

Why does x[10] fail for a 5-element array?

IndexError: valid nonnegative indices are 0 to 4.

11
New cards

For x=[1,3,5,7,9], what is x[1:-1]?

[3, 5, 7]

12
New cards

For x=np.array([10,20,30,40]), what is x[0]?

10

13
New cards

For x=np.array([10,20,30,40]), what is x[3]?

40

14
New cards

For a 6-element array, what is the last valid positive index?

5

15
New cards

For x=[10,20,30,40,50] as an array, what is x[2:5]?

[30 40 50]

16
New cards

For x=[10,20,30,40,50] as an array, what is x[2:2]?

[]; start equals stop, so the slice is empty.

17
New cards

For x=[0,1,2,3,4,5], what is x[1:6:2]?

[1 3 5]

18
New cards

For x=[0,1,2,3,4,5], what is x[4:1:-1]?

[4 3 2]

19
New cards

What are start, stop, and step in x[start:stop:step]?

Start included, stop excluded, step sets direction/stride.

20
New cards

Write a slice for entries at indices 2, 3, and 4

x[2:5]

21
New cards

Write a slice for the first four array elements

x[:4]

22
New cards

Write code to replace the first two entries with 0

x[:2] = 0

23
New cards

For x=np.array([2,4,6]), after x[1]=10, what is x?

[2 10 6]

24
New cards

For x=np.array([1,2,3,4]), what is x[::2]?

[1 3]

25
New cards

Why can x[2.0] produce an error even though 2.0 equals 2?

Array positions require integer indices, not floats.

26
New cards

What happens if you slice x[100:200] when x only has 5 entries?

You get an empty slice, not an IndexError.

27
New cards

What does x[1:4] = 0 do?

Replaces entries at indices 1, 2, and 3 with zero.