Python does not have a built in array type, but you can use lists for all of the same tasks. An array is a collection of values of the same type saved under the same name.
Each value in the array is called an “element” and indexing that represents its position. You can access specific elements by calling the array name with the desired element’s index. You can also get the length of an array using the len() method.
Unlike programming languages like Java that have static arrays after declaration, Python’s arrays automatically scale up or down when elements are added/subtracted.
For example, we could use the append() method to add an additional element on the end of an existing array instead of declaring a new array.
1.cars = ["Toyota", "Tesla", "Hyundai"] 2.print(len(cars)) 3.cars.append("Honda") 4.cars.pop(1) 5.for x in cars: 6. print(x)
3 Toyota Hyundai Honda
Top comments (0)