Array Indexing Basics
18 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is the index of the first element in a Python array?

  • n-1
  • 0 (correct)
  • -1
  • 1
  • What is the purpose of negative indexing in Python arrays?

  • To access every other element in the array
  • To access elements from the end of the array (correct)
  • To access the middle element of the array
  • To access elements from the start of the array
  • What is the default step value in Python array slicing?

  • 1 (correct)
  • 0
  • n-1
  • 2
  • How do you access the element at the second row and third column of a 2D array arr?

    <p>arr[1, 2]</p> 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?

    <p>It raises an <code>IndexError</code></p> Signup and view all the answers

    What is the purpose of slicing in Python arrays?

    <p>To extract a subset of elements from the array</p> 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?

    <p>arr[1:6:2]</p> Signup and view all the answers

    What is the index of the last element in a Python array of length n?

    <p>n-1</p> Signup and view all the answers

    What does arr[1:5:2] return?

    <p>Elements from index 1 to 4 with a step of 2</p> Signup and view all the answers

    What does arr[:3] return?

    <p>Elements from index 0 to 2</p> Signup and view all the answers

    What does arr[:, 1] return?

    <p>The entire 2nd column</p> Signup and view all the answers

    What does arr[arr > 3] return?

    <p>Elements from arr where the corresponding element is greater than 3</p> Signup and view all the answers

    What does arr[::] return?

    <p>A copy of the entire array</p> Signup and view all the answers

    What does arr[0, :] return?

    <p>The entire 1st row</p> Signup and view all the answers

    What does arr[[True, False, True, False]] return?

    <p>Elements at indices where the corresponding boolean is True</p> Signup and view all the answers

    What does arr[1::2] return?

    <p>Every other element from the array starting from the 2nd element</p> Signup and view all the answers

    What does arr[[-1, -2, -3]] return?

    <p>The elements at indices -1, -2, and -3 from the array</p> Signup and view all the answers

    What does arr[0, 1:] return?

    <p>The entire 1st row of the array from the 2nd column</p> 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, where n 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 array arr.

    Negative Indexing

    • Negative indexing is used to access elements from the end of the array.
    • Example: arr[-1] accesses the last element of the array arr.
    • Example: arr[-2] accesses the second last element of the array arr.

    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 array arr.
    • Example: arr[1:6:2] extracts every other element from the second to the sixth element of the array arr.

    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 array arr.

    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 an IndexError if the length of arr 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, where n 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 array arr.
    • Example: arr[1:6:2] extracts every other element from the second to the sixth element of the array arr.

    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 array arr.

    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 an IndexError if the length of arr 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 from start to stop-1.
    • arr[start:] returns elements from start to the end of the array.
    • arr[:stop] returns elements from the beginning to stop-1.
    • arr[:] returns a copy of the entire array.

    Indexing with Step

    • Indexing with step extracts every n-th element from an array using arr[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 &gt; 5] returns elements from arr where the corresponding element in arr 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 from arr.
    • arr[[True, False, True]] returns elements from arr where the corresponding element in the boolean array is True.

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    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.

    More Like This

    Array Indexing
    10 questions

    Array Indexing

    GreatestFreesia avatar
    GreatestFreesia
    NumPy Array Indexing and Slicing
    6 questions
    Programming Week 10 Flashcards
    5 questions
    Use Quizgecko on...
    Browser
    Browser