Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a brief guide on arrays #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,35 @@ with open("myfile.txt") as f:
lines = [line.strip() for line in f]
```

Arrays
----
**Just like lists, arrays are mutable sequences, with a couple of differencies. They are restricted to contain elements of the same data type, which makes this type more memory-efficient. Functionality to work with arrays is provided by array and numpy modules.**

```python
# Using array module
import array

my_array = array.array('i', [1, 2, 3, 4, 5]) # --> Creates an array
my_array[2] = 10 # None --> Modifies an element at index
my_array.append(6) # None --> Adds an element
my_array.remove(4) # None --> Removes an element if present, otherwise raises a ValueError
print(my_array) # --> Output: array('i', [1, 2, 10, 5, 6])
my_list = my_array.tolist() # Creates a new list based on a given array
print(my_list) # --> Output: [1, 2, 10, 5, 6]
```

```python
# Using numpy module
import numpy as np

arr = np.array([1, 2, 3, 4, 5]) # --> Creates a numpy array
arr[2] = 10 # --> Modifies the element at given index
arr = np.append(arr, 99) # --> Appends an element
arr = np.append(arr, [6, 7]) # --> Appends elements from another array
arr = np.insert(arr, 4, [8, 9]) # --> Inserts 8 and 9, starting at index 4
arr = np.delete(arr, 3) # --> Removes element at a given index
```

Dictionaries
----------
**Also known as mappings or hash tables. They are key value pairs that are guaranteed to retain order of insertion starting from Python 3.7**
Expand Down