Arrays Recap

In this chapter, we learned how to work with arrays (lists) in Python. We found out that:

  • We can create arrays using square brackets [] and elements separated by commas. For example: list = [1, 2, 3, 3].
  • We access array elements using their indices, which start at 0. For example: print(list[0]) prints the first element of the list array.
  • We can easily change array elements using their indices:
  • For example: list[3] = 4 sets the fourth element of the list array to the value 4.
  • We can add new elements to the end of the array using the append() method. For example: list.append(5) adds the value 5 to the end of the array.
  • We can remove the last element from the array using the pop() method. For example: lastElement = list.pop() removes the last element from the list array and stores the value of this element in the lastElement variable.
  • We can find out the number of elements in the array using the len() function. For example: elementCount = len(list) stores the number of elements in the list array in the elementCount variable.
  • Arrays can contain other arrays as their elements, allowing us to create nested arrays. For example: nestedArray = [[1, 2], [3, 4], [5, 6]].
  • Text strings can also be considered as arrays of characters, which we can access using their indices. For example: text = "Python" and firstChar = text[0] store the first character "P" of text in the firstChar variable.

Congratulations on completing the lesson on arrays! In the next lesson, we will look at loops and their use in Python. Get ready for the next step in your programming adventure!

Instructions

When you're ready, move on to the next lesson!

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

10/10

Arrays Recap | Start Coder