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 thelistarray. - We can easily change array elements using their indices:
- For example:
list[3] = 4sets the fourth element of thelistarray 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 thelistarray and stores the value of this element in thelastElementvariable. - 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 thelistarray in theelementCountvariable. - 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"andfirstChar = text[0]store the first character"P"oftextin thefirstCharvariable.
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
10/10