Skip to content
Open
103 changes: 103 additions & 0 deletions content/python/concepts/deque/terms/append/append.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
---
Title: 'append()'
Description: 'Adds an element to the right end of a deque.'
Subjects:
- 'Computer Science'
- 'Data Structures'
Tags:
- 'Collections'
- 'Deque'
- 'Python'
CatalogContent:
- 'learn-python-3'
- 'paths/computer-science'
---

The **`append()`** method of a `collections.deque` object adds an element to the right end (back) of the deque efficiently. This operation runs in O(1) time and is ideal for queue or stack-like use cases.

## Syntax

```pseudo
deque_object.append(value)
```

**Parameters:**

- `value`: The element to be added to the right end of the deque.

**Return value:**

Does not return a `value` (returns None). The deque is modified in place.

## Example 1

In this example, an element is added to an initially empty deque, then another element is appended and the contents are displayed:

```py
from collections import deque

dq = deque()
dq.append('a')
print(dq)

dq.append('b')
print(dq)
```

The output of this code is:

```shell
deque(['a'])
deque(['a', 'b'])
```

## Example 2

In this example, an element is appended to a deque with a fixed maximum length, showing how older elements are automatically dropped from the opposite end when full:

```py
from collections import deque

dq = deque([1, 2, 3], maxlen=3)
print(dq)

dq.append(4)
print(dq)
```

The output of this code is:

```shell
deque([1, 2, 3], maxlen=3)
deque([2, 3, 4], maxlen=3)
```

## Codebyte Example

In this example, multiple items are appended to a deque, then elements are popped from the left while printing the current state and size:

```codebyte/python
from collections import deque

dq = deque()
for ch in ['x', 'y', 'z']:
dq.append(ch)
print(f"After append('{ch}'): {dq}")

while dq:
print(f"Popping left: {dq.popleft()} | Current deque: {dq}")
```

## Frequently Asked Questions

### 1. How do you append to a deque in Python?

Use the `append(value)` method on the deque object to add the element `value` to its right end.

### 2. What is the `append()` function used for in Python?

In the context of a `deque`, `append()` adds an element at the back end of the container, supporting efficient queue- or stack-like operations.

### 3. What is the deque function in Python?

The `deque` class in the `collections` module is a double-ended queue that supports fast appends and pops from both ends, making it useful for queues, stacks, and history buffers.
79 changes: 79 additions & 0 deletions docs/content/pytorch/concepts/tensor-operations/terms/log2/log2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---
Title: '.log2()'
Description: 'Computes the base-2 logarithm of each element in the input tensor and returns a new tensor with the results.'
Subjects:
- 'Code Foundations'
- 'Computer Science'
- 'Data Science'
Tags:
- 'Elements'
- 'Methods'
- 'PyTorch'
- 'Tensors'
CatalogContent:
- 'learn-python-3'
- 'paths/data-science'
---

The **`.log2()`** method in PyTorch returns a new [tensor](https://www.codecademy.com/resources/docs/pytorch/tensors) by computing the logarithm base 2 of each element in the input tensor. This operation is useful in numerous data-science and machine-learning workflows where values are interpreted on a log scale (e.g., information theory, binary magnitude comparisons).

## Syntax

```pseudo
torch.log2(input, *, out=None) → Tensor
```

**Parameters:**

- `input` (Tensor): The tensor whose elements are to be transformed by base-2 logarithm.
- `out` (Tensor, optional): A tensor to store the output; must have the same shape as input if provided.

**Return value:**

Returns a new tensor of the same shape as `input` where each element is `log₂(input[i])`.

## Example 1: Basic Usage of `torch.log2()`

In this example, the base-2 logarithm is computed for a tensor of powers of two:

```py
import torch

# Define a tensor
input_tensor = torch.tensor([2.0, 4.0, 8.0, 16.0, 32.0])

# Compute base-2 logarithm
output_tensor = torch.log2(input_tensor)

print(output_tensor)
```

The output of this code is:

```shell
tensor([1., 2., 3., 4., 5.])
```

## Example 2: Applying `torch.log2()` on Random Values

In this example, a tensor with random positive values is transformed using base-2 logarithm to analyze data on a log scale:

```py
import torch

# Generate a tensor of random positive values
data = torch.rand(5) * 10 + 1

# Apply log2 transformation
log_tensor = torch.log2(data)

print(data)
print(log_tensor)
```

The output of this code is:

```shell
tensor([10.5500, 9.2777, 10.9371, 1.3551, 5.2609])
tensor([3.3992, 3.2138, 3.4512, 0.4384, 2.3953])
```