Podcast
Questions and Answers
What is the index of the first element in a Python array?
What is the index of the first element in a Python array?
What is the purpose of negative indexing in Python arrays?
What is the purpose of negative indexing in Python arrays?
What is the default step value in Python array slicing?
What is the default step value in Python array slicing?
How do you access the element at the second row and third column of a 2D array arr
?
How do you access the element at the second row and third column of a 2D array arr
?
Signup and view all the answers
What happens when you try to access an element in a Python array with an index that is out of bounds?
What happens when you try to access an element in a Python array with an index that is out of bounds?
Signup and view all the answers
What is the purpose of slicing in Python arrays?
What is the purpose of slicing in Python arrays?
Signup and view all the answers
What is the syntax for extracting every other element from the second to the sixth element of an array arr
?
What is the syntax for extracting every other element from the second to the sixth element of an array arr
?
Signup and view all the answers
What is the index of the last element in a Python array of length n
?
What is the index of the last element in a Python array of length n
?
Signup and view all the answers
What does arr[1:5:2] return?
What does arr[1:5:2] return?
Signup and view all the answers
What does arr[:3] return?
What does arr[:3] return?
Signup and view all the answers
What does arr[:, 1] return?
What does arr[:, 1] return?
Signup and view all the answers
What does arr[arr > 3] return?
What does arr[arr > 3] return?
Signup and view all the answers
What does arr[::] return?
What does arr[::] return?
Signup and view all the answers
What does arr[0, :] return?
What does arr[0, :] return?
Signup and view all the answers
What does arr[[True, False, True, False]] return?
What does arr[[True, False, True, False]] return?
Signup and view all the answers
What does arr[1::2] return?
What does arr[1::2] return?
Signup and view all the answers
What does arr[[-1, -2, -3]] return?
What does arr[[-1, -2, -3]] return?
Signup and view all the answers
What does arr[0, 1:] return?
What does arr[0, 1:] return?
Signup and view all the answers
Study Notes
Array Indexing
Basic Indexing
- In Python, arrays are 0-indexed, meaning the first element has an index of 0.
- Indexing starts from the left, with the first element having an index of 0, and the last element having an index of
n-1
, wheren
is the length of the array.
Positive Indexing
- Positive indexing is used to access elements from the start of the array.
- Example:
arr[0]
accesses the first element of the arrayarr
.
Negative Indexing
- Negative indexing is used to access elements from the end of the array.
- Example:
arr[-1]
accesses the last element of the arrayarr
. - Example:
arr[-2]
accesses the second last element of the arrayarr
.
Slicing
- Slicing is used to extract a subset of elements from the array.
- Syntax:
arr[start:stop:step]
-
start
: The index of the first element to include in the slice (inclusive). -
stop
: The index of the last element to include in the slice (exclusive). -
step
: The increment between elements (default is 1). - Example:
arr[1:3]
extracts the second and third elements of the arrayarr
. - Example:
arr[1:6:2]
extracts every other element from the second to the sixth element of the arrayarr
.
Multi-Dimensional Arrays
- In Python, multi-dimensional arrays are arrays of arrays.
- Indexing for multi-dimensional arrays is done using comma-separated indices.
- Example:
arr[0, 1]
accesses the element at the first row and second column of the 2D arrayarr
.
Out-of-Bounds Indexing
- If an index is out of bounds (i.e., greater than or equal to the length of the array), a
IndexError
is raised. - Example:
arr[10]
raises anIndexError
if the length ofarr
is less than 11.
Array Indexing
Basic Indexing
- Arrays in Python are 0-indexed, meaning the first element has an index of 0.
- Indexing starts from the left, with the first element having an index of 0, and the last element having an index of
n-1
, wheren
is the length of the array.
Positive Indexing
- Positive indexing is used to access elements from the start of the array.
- The first element of an array can be accessed using
arr[0]
.
Negative Indexing
- Negative indexing is used to access elements from the end of the array.
- The last element of an array can be accessed using
arr[-1]
. - The second last element of an array can be accessed using
arr[-2]
.
Slicing
- Slicing is used to extract a subset of elements from the array.
- The syntax for slicing is
arr[start:stop:step]
. - The
start
parameter defines the index of the first element to include in the slice (inclusive). - The
stop
parameter defines the index of the last element to include in the slice (exclusive). - The
step
parameter defines the increment between elements (default is 1). - Example:
arr[1:3]
extracts the second and third elements of the arrayarr
. - Example:
arr[1:6:2]
extracts every other element from the second to the sixth element of the arrayarr
.
Multi-Dimensional Arrays
- In Python, multi-dimensional arrays are arrays of arrays.
- Indexing for multi-dimensional arrays is done using comma-separated indices.
- Example:
arr[0, 1]
accesses the element at the first row and second column of the 2D arrayarr
.
Out-of-Bounds Indexing
- If an index is out of bounds (i.e., greater than or equal to the length of the array), a
IndexError
is raised. - Example:
arr[10]
raises anIndexError
if the length ofarr
is less than 11.
Array Indexing
- Array indexing in Python uses square brackets
[]
and the index of the element. - There are two types of indexing: positive and negative.
Positive and Negative Indexing
- Positive indexing starts from 0 and increments by 1.
- Negative indexing starts from -1 and decrements by 1.
Slicing
- Slicing extracts a subset of elements from an array using a colon
:
. -
arr[start:stop]
returns elements fromstart
tostop-1
. -
arr[start:]
returns elements fromstart
to the end of the array. -
arr[:stop]
returns elements from the beginning tostop-1
. -
arr[:]
returns a copy of the entire array.
Indexing with Step
- Indexing with step extracts every
n
-th element from an array usingarr[start:stop:step]
. -
arr[::2]
returns every 2nd element from the beginning to the end. -
arr[1::2]
returns every 2nd element starting from the 2nd element.
Multi-indexing
Multi-dimensional Arrays
- Multi-dimensional arrays can be indexed using commas
,
to separate indices. -
arr[0, 1]
returns the element at the 1st row and 2nd column. -
arr[0, :]
returns the entire 1st row. -
arr[:, 1]
returns the entire 2nd column.
Boolean Array Indexing
- Boolean array indexing uses a boolean array to select elements from another array.
-
arr[arr > 5]
returns elements fromarr
where the corresponding element inarr
is greater than 5.
Fancy Indexing
- Fancy indexing uses an array of indices to select elements from another array.
-
arr[[0, 2, 4]]
returns elements at indices 0, 2, and 4 fromarr
. -
arr[[True, False, True]]
returns elements fromarr
where the corresponding element in the boolean array isTrue
.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
Learn about the basics of array indexing in Python, including 0-indexing, positive and negative indexing, and how to access elements in an array.