|
| 1 | +# Python Arrays |
| 2 | +# *Note: Python does not have built-in support for Arrays, but Python Lists can be used instead. |
| 3 | + |
| 4 | +# Arrays |
| 5 | +# *Note: This page shows you how to use LISTS as ARRAYS, however, to work with arrays in Python you will have to import a library, like the NumPy library. |
| 6 | + |
| 7 | +# Arrays are used to store multiple values in one single variable: |
| 8 | + |
| 9 | +# Example |
| 10 | +# Create an array containing car names: |
| 11 | + |
| 12 | +cars = ["Ford", "Volvo", "BMW"] |
| 13 | + |
| 14 | +# What is an Array? |
| 15 | +# An array is a special variable, which can hold more than one value at a time. |
| 16 | + |
| 17 | +# If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this: |
| 18 | + |
| 19 | +car1 = "Ford" |
| 20 | +car2 = "Volvo" |
| 21 | +car3 = "BMW" |
| 22 | + |
| 23 | +# However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300? |
| 24 | + |
| 25 | +# The solution is an array! |
| 26 | + |
| 27 | +# An array can hold many values under a single name, and you can access the values by referring to an index number. |
| 28 | + |
| 29 | +# Access the Elements of an Array |
| 30 | +# You refer to an array element by referring to the index number. |
| 31 | + |
| 32 | +# Example |
| 33 | +# Get the value of the first array item: |
| 34 | + |
| 35 | +x = cars[0] |
| 36 | +# Example |
| 37 | +# Modify the value of the first array item: |
| 38 | + |
| 39 | +cars[0] = "Toyota" |
| 40 | + |
| 41 | +# The Length of an Array |
| 42 | +# Use the len() method to return the length of an array (the number of elements in an array). |
| 43 | + |
| 44 | +# Example |
| 45 | +# Return the number of elements in the cars array: |
| 46 | + |
| 47 | +x = len(cars) |
| 48 | + |
| 49 | +# *Note: The length of an array is always one more than the highest array index. |
| 50 | + |
| 51 | +# Looping Array Elements |
| 52 | +# You can use the for in loop to loop through all the elements of an array. |
| 53 | + |
| 54 | +# Example |
| 55 | +# Print each item in the cars array: |
| 56 | + |
| 57 | +for x in cars: |
| 58 | + print(x) |
| 59 | + |
| 60 | +# Adding Array Elements |
| 61 | +# You can use the append() method to add an element to an array. |
| 62 | + |
| 63 | +# Example |
| 64 | +# Add one more element to the cars array: |
| 65 | + |
| 66 | +cars.append("Honda") |
| 67 | + |
| 68 | +# Removing Array Elements |
| 69 | +# You can use the pop() method to remove an element from the array. |
| 70 | + |
| 71 | +# Example |
| 72 | +# Delete the second element of the cars array: |
| 73 | + |
| 74 | +cars.pop(1) |
| 75 | + |
| 76 | +# You can also use the remove() method to remove an element from the array. |
| 77 | + |
| 78 | +# Example |
| 79 | +# Delete the element that has the value "Volvo": |
| 80 | + |
| 81 | +cars.remove("Volvo") |
| 82 | + |
| 83 | +# *Note: The list's remove() method only removes the first occurrence of the specified value. |
| 84 | + |
| 85 | +# Array Methods |
| 86 | +# Python has a set of built-in methods that you can use on lists/arrays. |
| 87 | + |
| 88 | +# Method Description |
| 89 | + |
| 90 | +# append() Adds an element at the end of the list |
| 91 | +# clear() Removes all the elements from the list |
| 92 | +# copy() Returns a copy of the list |
| 93 | +# count() Returns the number of elements with the specified value |
| 94 | +# extend() Add the elements of a list (or any iterable), to the end of the current list |
| 95 | +# index() Returns the index of the first element with the specified value |
| 96 | +# insert() Adds an element at the specified position |
| 97 | +# pop() Removes the element at the specified position |
| 98 | +# remove() Removes the first item with the specified value |
| 99 | +# reverse() Reverses the order of the list |
| 100 | +# sort() Sorts the list |
| 101 | + |
| 102 | +# *Note: Methods of Arrays & Lists Are Exactly Same |
| 103 | +# *Note: Python does not have built-in support for Arrays, but Python Lists can be used instead. |
| 104 | + |
0 commit comments