{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\"FMP\"\n", "\"AudioLabs\"\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "
\"B\"
\n", "

Python Style Guide

\n", "
\n", "\n", "

\n", "This notebook makes some general recommendations for coding conventions as often used in Python.\n", "

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Our recommendation closely follows the [PEP 8 Style Guide for Python Code](https://www.python.org/dev/peps/pep-0008/). Note that such coding conventions are only rough guidelines, and specific projects may follow other conventions. Use the guidelines only if there are no reasons to do something else. In the following, we list a couple of general conventions:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use `'` instead of `\"` for strings" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2024-02-15T09:02:04.864591Z", "iopub.status.busy": "2024-02-15T09:02:04.864258Z", "iopub.status.idle": "2024-02-15T09:02:04.867843Z", "shell.execute_reply": "2024-02-15T09:02:04.867172Z" } }, "outputs": [], "source": [ "x = 'string' # good\n", "x = \"string\" # bad" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use spaces after commas" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2024-02-15T09:02:04.871217Z", "iopub.status.busy": "2024-02-15T09:02:04.870970Z", "iopub.status.idle": "2024-02-15T09:02:04.874392Z", "shell.execute_reply": "2024-02-15T09:02:04.873551Z" } }, "outputs": [], "source": [ "x = [1, 2, 3] # good\n", "x = [1,2,3] # bad" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use no space before or after brackets" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2024-02-15T09:02:04.877581Z", "iopub.status.busy": "2024-02-15T09:02:04.877336Z", "iopub.status.idle": "2024-02-15T09:02:04.880534Z", "shell.execute_reply": "2024-02-15T09:02:04.879798Z" } }, "outputs": [], "source": [ "x = range(1, 10, 2) # good\n", "x = range( 1, 10, 2 ) # bad" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use `under_score` instead of `CamelCase` or `mixedCase` for variable names and function names." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2024-02-15T09:02:04.883833Z", "iopub.status.busy": "2024-02-15T09:02:04.883530Z", "iopub.status.idle": "2024-02-15T09:02:04.886468Z", "shell.execute_reply": "2024-02-15T09:02:04.885821Z" } }, "outputs": [], "source": [ "my_variable = 1 # good\n", "myVariable = 1 # bad" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use spaces around `=` in assignments of variables." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2024-02-15T09:02:04.889394Z", "iopub.status.busy": "2024-02-15T09:02:04.889174Z", "iopub.status.idle": "2024-02-15T09:02:04.891840Z", "shell.execute_reply": "2024-02-15T09:02:04.891214Z" } }, "outputs": [], "source": [ "x = 1 # good\n", "x=1 # bad" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Don't use spaces around `=` in assignments of parameters." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2024-02-15T09:02:04.894629Z", "iopub.status.busy": "2024-02-15T09:02:04.894426Z", "iopub.status.idle": "2024-02-15T09:02:04.897255Z", "shell.execute_reply": "2024-02-15T09:02:04.896664Z" } }, "outputs": [], "source": [ "x = dict(a=1) # good\n", "x = dict(a = 1) # bad" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use spaces around operators like `+`, `-`, `*`, `/`, etc." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2024-02-15T09:02:04.899974Z", "iopub.status.busy": "2024-02-15T09:02:04.899772Z", "iopub.status.idle": "2024-02-15T09:02:04.902268Z", "shell.execute_reply": "2024-02-15T09:02:04.901708Z" } }, "outputs": [], "source": [ "x = 1 + 2 # good\n", "x = 1+2 # bad" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "Acknowledgment: This notebook was created by Frank Zalkow and Meinard Müller.\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", "
\"C0\"\"C1\"\"C2\"\"C3\"\"C4\"\"C5\"\"C6\"\"C7\"\"C8\"
" ] } ], "metadata": { "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.8.16" } }, "nbformat": 4, "nbformat_minor": 1 }