*** Wartungsfenster jeden ersten Mittwoch vormittag im Monat ***

Skip to content
Snippets Groups Projects
03_PythonMiscellaneous.ipynb 5.54 KiB
Newer Older
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "24061ec0",
   "metadata": {},
   "source": [
    "# Miscellaneous Python Tips"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a97572f",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true,
    "tags": []
   },
   "source": [
    "## f-Strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "464ec78b-f11c-4515-9c82-df69102bbad3",
   "metadata": {},
   "outputs": [],
   "source": [
    "s1, s2, s3 = 'Python', 'String', 'Concatenation'\n",
    "i4 = 1337"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "40e48437-5945-469e-ac80-7375fee5069b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "258 ns ± 3.35 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit (f'{s1} {s2} {s3} {i4}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "298fdf52-56a9-4f5c-a3bb-9b75081252fd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "410 ns ± 4.28 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit ('{} {} {} {}'.format(s1, s2, s3, i4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "2f07c4a0-1eb1-454f-b90e-dda302f06f77",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "306 ns ± 6.15 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit ('%s %s %s %d' % (s1, s2, s3, i4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "9648aeb3-bdfc-4da0-a9f2-22b684f49126",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "394 ns ± 9.91 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit (','.join([s1, s2, s3, str(i4)]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "d54cf428-469f-4e12-8285-6c0b121756a1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "352 ns ± 5.15 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit (s1 + s2 + s3 + str(i4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "bd48c6f2-8057-4581-8374-5075513b8428",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Python String Concatenation 1337\n"
     ]
    }
   ],
   "source": [
    "s = f'{s1} {s2} {s3} {i4}'\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93757aed",
   "metadata": {},
   "source": [
    "## Context Managers"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7d3fafca-395a-47d3-874d-41a408852c3b",
   "metadata": {},
   "source": [
    "# Python Lists & Arrays to NumPy Arrays"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "06a5e755-bb73-43cc-bab2-35023ef8fbed",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import array as arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "f288c9d2-321b-4301-abc6-86b54363e90c",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = range(10_000_000) # list\n",
    "b = arr.array('i', range(10_000_000)) # array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9fc053b1-93e7-42e3-bad1-1944a70c9bd2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "111 ms ± 9.58 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit x = np.array(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "449312d4-9f73-4266-8f98-323d24a86a32",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.14 s ± 183 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
     ]
    }
   ],
   "source": [
    "%timeit y = np.asarray(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "34566d24-5bc1-479c-882a-c31975d9b0f9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "19.8 ms ± 3.55 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit v = np.array(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "1974685b-b0fb-4281-bfe8-8b83ab6af76f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "932 ns ± 313 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit w = np.asarray(b)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.13 ('venv': venv)",
   "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.13"
  },
  "vscode": {
   "interpreter": {
    "hash": "9d2533a8417e93bb270061fddcf202607a399e1158d66c4e8746479fc11cda46"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}