{ "cells": [ { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ ".. meta::\n", " :description: Topic: Overview of formatting in Python Like You Mean It, Difficulty: Easy, Category: Instructions\n", " :keywords: overview, formatting, background, code block, console style" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# A Quick Guide to Formatting\n", "This section provides a brief overview of the code formatting style that will be used throughout this text. You are not expected to understand the details of the code, here. This merely provides a guide for what is to come. \n", "\n", "Any code that is included in-line within plain text will be formatted distinctly as so: \"the variable `x` was updated...\". Such items will be distinguished with backticks wherever such formatting is not available. Take for example the following commented line within Python code:\n", "\n", "```python\n", "# the variable `x` will be updated\n", "```\n", "\n", "Python code will be displayed within distinct, colorized code blocks. These will typically begin with a comment, which is meant to serve as a caption that summarizes the purpose of the code block:\n", "\n", "```python\n", "# demonstrating a basic for-loop\n", "cnt = 0\n", "for i in range(10):\n", " cnt += 1\n", "\n", "#`cnt` is now 10\n", "```\n", "\n", "The symbol `>>>` appears within code blocks to indicate \"console-style\" code, which distinguishes between code being entered by a user and the resulting output. The purpose of this is that it allows us to easily display the result of a computation without having to rely on calling the `print` function. For instance, the following code assigns the integer `1` to the variable `x`, and then displays the result of `x + 2`:\n", "\n", "```python\n", "# demonstrating the distinction of\n", "# input and output via >>>\n", "\n", ">>> x = 1\n", ">>> x + 2\n", "3\n", "```\n", "\n", "The code blocks throughout a given section of the text should be understood to be persistent even if there is a mix of \"pure\" code blocks and \"console-style\" code blocks. For example, a function may be defined at the beginning of a section, and then referenced throughout the rest of that section:\n", "```python\n", "# defining an example function\n", "def my_func(x):\n", " return x**2\n", "```\n", "\n", "We can spend some time talking about `my_func` and then see it in action:\n", "```python\n", "# demonstrating `my_func`\n", ">>> my_func(10.)\n", "100.\n", "```\n", "\n", "Lastly, the input and output of an iPython console and a Jupyter notebook alike is displayed as follows:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 + 3" ] } ], "metadata": { "jupytext": { "text_representation": { "extension": ".md", "format_name": "markdown", "format_version": "1.2", "jupytext_version": "1.3.0rc1" } }, "kernelspec": { "display_name": "Python 3", "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.7.6" } }, "nbformat": 4, "nbformat_minor": 4 }