Skip to content

Python¤

Regular Python¤

Python code is executed in the current process, with isolated global variables.

To capture the output of your code, Markdown Exec patches the print function so that it writes to a buffer instead of standard output.

```python exec="1"
print("**Hello world!**")
```

Hello world!

See the Gallery for more complex examples.

Python console code¤

Code blocks syntax-highlighted with the pycon identifier are also supported. These code blocks will be pre-processed to keep only the lines starting with >>>, and the chevrons (prompt) will be removed from these lines, so we can execute them.

```pycon exec="1" source="console"
>>> print("I'm the result!")
I'm not the result...
```
>>> print("I'm the result!")
I'm the result!

It also means that multiple blocks of instructions will be concatenated, as well as their output:

```pycon exec="1" source="console"
>>> name = "Baron"
>>> print(name)
Baron
>>> age = "???"
>>> print(age)
???
```
>>> name = "Baron"
>>> print(name)
>>> age = "???"
>>> print(age)
Baron
???