Lists are mutable and can store items of various data types, while arrays can only store items of the same single data type
Arrays
Fundamental data structure, important part of most programming languages, containers in Python that can store more than one item at the same time, ordered collection of elements with every value being of the same data type
How to define arrays in Python
Import the array module, define a Python array by specifying variable name, typecode, and elements in square brackets
All elements in an array should be of the same data type
Lists
Common data structures in Python, mutable and not fixed in size, can grow and shrink, store items of various data types
How to use arrays in Python
Import the array module, create an array using array.array(), use import array as arr for alias name, use from array import * for importing all functionalities
How to find the length of an array in Python
Use the built-in len() method to determine the total number of elements in the array
Negative indexing can also be used to access individual elements in an array
How to loop through an array in Python
1. Access and print each individual element in the array
2. Print the entire array using the print() method
The index value of the last element in an array is always one less than the length of the array
If values not of the specified typecode are included in the array, an error will occur
Array indexing and how to access individual items in an array in Python
1. Access individual items by referencing their index number starting from 0
2. To access an element, write the array name followed by square brackets containing the item's index number
How to define arrays in Python
1. Include the array module with import array as arr
2. Create a numbers array using arr.array()
3. Specify the type of values the array can include (e.g., signed integer)
4. Include the values to be stored in the array in square brackets
If there are multiple elements with the same value, the index of the first instance will be returned
The length of an array is equal to the total number of elements it contains
How to search through an array in Python
Use the index() method to find an element's index number by passing the value being searched as an argument
Adding a new value to an array
1. Add one single value at the end of an array using the append() method
2. Add more than one value to the end of an array using the extend() method with items of the same data type
3. Insert an item at a specific position in an array using the insert() method
Slicing an array in Python
1. Access a specific range of values inside the array using the slicing operator
2. When using the slicing operator with one value, counting starts from 0 and goes up to but not including the specified index
3. When using two values, specify a range where counting starts at the position of the first number and goes up to but not including the second one
Changing the value of an item in an array
Change the value of a specific element by specifying its position and assigning a new value
Removing a value from an array
1. Remove an element from an array using the remove() method by specifying the value
2. Only the first instance of the specified value will be removed
3. Use the pop() method and specify the position of the element to be removed
Looping through an array in Python
1. Access each individual element in an array and print it out on its own
2. Print the array using the print() method
3. Loop through the array and print out each value one by one using a for loop
4. Use the range() function with the len() method to achieve the same result