Skip to content
Closed
Show file tree
Hide file tree
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
81 changes: 81 additions & 0 deletions content/numpy/concepts/ndarray/terms/view/view.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
---
Title: '.view()'
Description: "Returns a new view of the array's data without copying the underlying memory."
Subjects:
- 'Computer Science'
- 'Data Science'
Tags:
- 'Array'
- 'Data'
- 'NumPy'
CatalogContent:
- 'learn-python-3'
- 'paths/data-science'
---

The **`.view()`** method in NumPy returns a new array object that views the same data as the original array. Both arrays share the same underlying memory block, meaning that any modification to the data in one will directly affect the other. Only the array’s metadata (such as data type or shape) may differ.

Creating a view is much faster and more memory-efficient than creating a copy, especially when working with large arrays.

## Syntax

```pseudo
ndarray.view([dtype][, type])
```

**Parameters:**

- `dtype` (data-type): The desired data type for the new array view. Changing the `dtype` changes how the data bytes are interpreted, not the data itself.
- `type` (type): The desired type for the resulting object (e.g., `np.matrix`).

**Return value:**

Returns a new `ndarray` object that shares the data of the original array.

## Example

In this example, modifying the data in the view (`view_array`) also changes the original array (`original_array`) because both share the same memory:

```py
import numpy as np

# Create the original array
original_array = np.array([1, 2, 3, 4, 5])

# Create a view of the original array
view_array = original_array.view()

print(f"Original Array before modification: {original_array}")
print(f"View Array before modification: {view_array}")

# Modify a single element in the view
view_array[0] = 99

print(f"\nOriginal Array after modifying the view: {original_array}")
```

The output of this code is:

```shell
Original Array before modification: [1 2 3 4 5]
View Array before modification: [1 2 3 4 5]

Original Array after modifying the view: [99 2 3 4 5]
```

## Codebyte Example

In this example, changing the original array also updates its view, since both reference the same data in memory:

```codebyte/python
import numpy as np

original = np.array([10, 20, 30])
data_view = original.view()

# Modify the original array's second element
original[1] = 50

print(f"Original array: {original}")
print(f"View array: {data_view}")
```
80 changes: 80 additions & 0 deletions content/python/concepts/binascii-module/crc-hqx/crc-hqx.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
---
Title: '.crc_hqx()'
Description: 'Computes the CRC-CCITT hash value for binary data, specifically using the CRC-HQX standard.'
Subjects:
- 'Computer Science'
- 'Data Science'
Tags:
- 'Python'
- 'binascii'
- 'hashing'
- 'checksum'
CatalogContent:
- 'learn-python-3'
- 'paths/computer-science'
---

The **`.crc_hqx()`** method is a function within Python's built-in `binascii` module used to compute a 16-bit Cyclic Redundancy Check (CRC) value. This specific implementation uses the parameters of the **CRC-HQX** standard (known for its use in the original Mac OS).

Checksums are primarily used to detect errors in data transmission or storage, ensuring that the data received is identical to the data sent.

## Syntax

The method takes the binary data to be checked and an optional initial CRC value.

```pseudo
binascii.crc_hqx(data, crc)
```

## Parameters

* `data` (bytes-like object): The binary data for which the CRC value should be calculated.
* `crc` (integer): An optional 16-bit starting value for the checksum calculation. If omitted, the initial value is `0`.

## Return Value

Returns an integer representing the 16-bit CRC-HQX checksum of the input data.

## Example

This example demonstrates computing the checksum for a short byte string, both with and without an initial CRC value.

```python
import binascii

data = b"Codecademy Docs"
initial_crc = 0xAAAA # A common non-zero starting value

# 1. Compute CRC with a starting value (0xAAAA)
crc_with_initial = binascii.crc_hqx(data, initial_crc)

# 2. Compute CRC starting from 0
crc_without_initial = binascii.crc_hqx(data, 0)

print(f"Data: {data!r}")
print(f"CRC (with initial value 0xAAAA): 0x{crc_with_initial:04x}")
print(f"CRC (starting from 0): 0x{crc_without_initial:04x}")
```

**Output:**

```shell
Data: b'Codecademy Docs'
CRC (with initial value 0xAAAA): 0x05f0
CRC (starting from 0): 0xa663
```

## Codebyte

Use the Codebyte below to calculate the CRC-HQX checksum for your own byte string.

```python
import binascii

data_to_check = b"Python Contributor"

# Calculate the CRC-HQX checksum starting from 0
checksum = binascii.crc_hqx(data_to_check, 0)

print(f"Checksum: 0x{checksum:04x}")
```
Loading