{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Displaying Python Code in Markdown Text Cells"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Sometimes it is useful to show Python code in a notebook without it being executable, in a Code cell; for example if it is only a fragment, or it is not working yet and you want to discuss it.\n",
    "\n",
    "There are several ways to do this, which I will illustrate first with small pieces of code and then for an entire cell."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "One method is surrounding Python code with single backward quotes;  \n",
    "for example `if a==0:` is displayed diffeerntl than if a==0:\n",
    "\n",
    "This becomes more obvious when there are multiple lines of code:\n",
    "\n",
    "`\n",
    "if a==0:\n",
    "    print(\"Warning: a=0 will lead to a division by zero error!\")\n",
    "`\n",
    "\n",
    "That respects line breaks and indentation, whereas\n",
    "\n",
    "if a==0:\n",
    "    print(\"Warning: a=0 will lead to a division by zero error!\")\n",
    "\n",
    "does not.\n",
    "\n",
    "Note also that the code chunk is separated by blank lines before and after."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A second method is indenting:\n",
    "\n",
    "    if a==0:\n",
    "        print(\"Warning: a=0 will lead to a division by zero error!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "These methods can also work for entire code cells, but then there is also another option: using \"Raw\" cells.\n",
    "I illustrate each with the code cell:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if a==0:\n",
    "    print(\"Warning: a=0 will lead to a division by zero error!\")\n",
    "else:\n",
    "    print(\"This is a genuine quadratic; let's compute its real roots, if any ...\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Method 1: backquotes in a Markdown cell:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`if a==0:\n",
    "    print(\"Warning: a=0 will lead to a division by zero error!\")\n",
    "else:\n",
    "    print(\"This is a genuine quadratic; let's compute its real roots, if any ...\")`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Method 2: indentation in a Markdown cell:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    if a==0:\n",
    "        print(\"Warning: a=0 will lead to a division by zero error!\")\n",
    "    else:\n",
    "        print(\"This is a genuine quadratic; let's compute its real roots, if any ...\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Method 3: convert cell to type \"Raw\""
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "if a==0:\n",
    "    print(\"Warning: a=0 will lead to a division by zero error!\")\n",
    "else:\n",
    "    print(\"This is a genuine quadratic; let's compute its real roots, if any ...\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
