# New Features

## f-string Debugging

Suppose you are doing a RSA challenge. Your objective is to factor $$N = p \* q$$ and you want the values of $$p$$ and $$q$$ printed out if the factorization algorithm succeeded. In this case, the code snippet will look like this:

```python
p = 13
q = 37
print(f"{p = }")
print(f"{q = }")
```

Output:

![Example 1](https://3988450783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MWVjG_njKgBtvmnKaJh%2Fuploads%2FyPdBkXDcHI19znb6ZSC3%2Fimage.png?alt=media\&token=393c3814-36cd-4be3-aff9-272ab2fd93af)

Here `print(f"{p = }")` expands to `print(f"p = {p}")` .

Suppose you are doing a ret2libc challenge and you have just leaked an address from libc. Now you want to examine this address in hex and see if it looks legit. In this case, the code snippet will look like this:

```python
leaked_address = 13371337
print(f"{hex(leaked_address) = }")
```

Output:

![Example 2](https://3988450783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MWVjG_njKgBtvmnKaJh%2Fuploads%2FCHZFrcCW8Gpv538q5qkS%2Fimage.png?alt=media\&token=4fada5af-ca72-427d-b239-093c03818b21)

Here `print(f"{hex(leaked_address) = }")` expands to `print(f"hex(leaked_address) = {hex(leaked_address)}")` .

## Walrus Operator

The `:=` operator is officially known as the **assignment expression operator**. During early discussions, it was dubbed the **walrus operator** because the `:=` syntax resembles the eyes and tusks of a sideways walrus:

![Walrus](https://3988450783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MWVjG_njKgBtvmnKaJh%2Fuploads%2FeqOpR3RWC1BgkdCCxPNh%2Fimage.png?alt=media\&token=15e9758d-cb73-4d01-9aff-7f8b211824a3)

To get a first impression of what assignment expressions are all about, start your REPL and play around with the following code:

```python
>>> walrus = False
>>> walrus
False

>>> (walrus := True)
True
>>> walrus
True
```

There's a subtle—but important—difference between the two types of assignments seen earlier with the `walrus` variable. **An assignment expression returns the value, while a traditional assignment doesn't.** You can see this in action when the REPL doesn't print any value after `walrus = False` on line 1, while it prints out `True` after the assignment expression on line 5.

Why is walrus operator useful? Consider the following example:

```python
# Syntax error
while line=file.readline():
    print(line)
```

In order to achieve the above semantics with correct syntax, using the walrus operator:

```python
# Walrus operator
while (line:=file.readline()):
    print(line)
```

{% hint style="danger" %}
Using walrus operator as a normal assignment operator results in a syntax error unless you put parentheses around it.
{% endhint %}

## Reference

{% embed url="<https://realpython.com/python-walrus-operator>" %}
The Walrus Operator - Real Python
{% endembed %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ret2basic.gitbook.io/ctfnote/computer-science/python/new-features.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
