{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "np.random.seed(12345)\n", "np.set_printoptions(precision=4, suppress=True)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "data = [np.random.standard_normal() for i in range(7)]\n", "data" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "a = [1, 2, 3]" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "b = a\n", "b" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "a.append(4)\n", "b" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "def append_element(some_list, element):\n", " some_list.append(element)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "data = [1, 2, 3]\n", "append_element(data, 4)\n", "data" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "a = 5\n", "type(a)\n", "a = \"foo\"\n", "type(a)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "\"5\" + 5" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "a = 4.5\n", "b = 2\n", "# String formatting, to be visited later\n", "print(f\"a is {type(a)}, b is {type(b)}\")\n", "a / b" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "a = 5\n", "isinstance(a, int)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "a = 5; b = 4.5\n", "isinstance(a, (int, float))\n", "isinstance(b, (int, float))" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "a = \"foo\"" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "getattr(a, \"split\")" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "def isiterable(obj):\n", " try:\n", " iter(obj)\n", " return True\n", " except TypeError: # not iterable\n", " return False" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "isiterable(\"a string\")\n", "isiterable([1, 2, 3])\n", "isiterable(5)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "5 - 7\n", "12 + 21.5\n", "5 <= 2" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "a = [1, 2, 3]\n", "b = a\n", "c = list(a)\n", "a is b\n", "a is not c" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "a == c" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "a = None\n", "a is None" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "a_list = [\"foo\", 2, [4, 5]]\n", "a_list[2] = (3, 4)\n", "a_list" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "a_tuple = (3, 5, (4, 5))\n", "a_tuple[1] = \"four\"" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "ival = 17239871\n", "ival ** 6" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "fval = 7.243\n", "fval2 = 6.78e-5" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "3 / 2" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "3 // 2" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "c = \"\"\"\n", "This is a longer string that\n", "spans multiple lines\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "c.count(\"\\n\")" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "a = \"this is a string\"\n", "a[10] = \"f\"" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "b = a.replace(\"string\", \"longer string\")\n", "b" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "a = 5.6\n", "s = str(a)\n", "print(s)" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [], "source": [ "s = \"python\"\n", "list(s)\n", "s[:3]" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [], "source": [ "s = \"12\\\\34\"\n", "print(s)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [], "source": [ "s = r\"this\\has\\no\\special\\characters\"\n", "s" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [], "source": [ "a = \"this is the first half \"\n", "b = \"and this is the second half\"\n", "a + b" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [], "source": [ "template = \"{0:.2f} {1:s} are worth US${2:d}\"" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [], "source": [ "template.format(88.46, \"Argentine Pesos\", 1)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [], "source": [ "amount = 10\n", "rate = 88.46\n", "currency = \"Pesos\"\n", "result = f\"{amount} {currency} is worth US${amount / rate}\"" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [], "source": [ "f\"{amount} {currency} is worth US${amount / rate:.2f}\"" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "val = \"espa\u00f1ol\"\n", "val" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [], "source": [ "val_utf8 = val.encode(\"utf-8\")\n", "val_utf8\n", "type(val_utf8)" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [], "source": [ "val_utf8.decode(\"utf-8\")" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [], "source": [ "val.encode(\"latin1\")\n", "val.encode(\"utf-16\")\n", "val.encode(\"utf-16le\")" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [], "source": [ "True and True\n", "False or True" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [], "source": [ "int(False)\n", "int(True)" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [], "source": [ "a = True\n", "b = False\n", "not a\n", "not b" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [], "source": [ "s = \"3.14159\"\n", "fval = float(s)\n", "type(fval)\n", "int(fval)\n", "bool(fval)\n", "bool(0)" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [], "source": [ "a = None\n", "a is None\n", "b = 5\n", "b is not None" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [], "source": [ "from datetime import datetime, date, time\n", "dt = datetime(2011, 10, 29, 20, 30, 21)\n", "dt.day\n", "dt.minute" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [], "source": [ "dt.date()\n", "dt.time()" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [], "source": [ "dt.strftime(\"%Y-%m-%d %H:%M\")" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [], "source": [ "datetime.strptime(\"20091031\", \"%Y%m%d\")" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [], "source": [ "dt_hour = dt.replace(minute=0, second=0)\n", "dt_hour" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [], "source": [ "dt" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [], "source": [ "dt2 = datetime(2011, 11, 15, 22, 30)\n", "delta = dt2 - dt\n", "delta\n", "type(delta)" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [], "source": [ "dt\n", "dt + delta" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [], "source": [ "a = 5; b = 7\n", "c = 8; d = 4\n", "if a < b or c > d:\n", " print(\"Made it\")" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [], "source": [ "4 > 3 > 2 > 1" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [], "source": [ "for i in range(4):\n", " for j in range(4):\n", " if j > i:\n", " break\n", " print((i, j))\n" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [], "source": [ "range(10)\n", "list(range(10))" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [], "source": [ "list(range(0, 20, 2))\n", "list(range(5, 0, -1))" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [], "source": [ "seq = [1, 2, 3, 4]\n", "for i in range(len(seq)):\n", " print(f\"element {i}: {seq[i]}\")" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [], "source": [ "total = 0\n", "for i in range(100_000):\n", " # % is the modulo operator\n", " if i % 3 == 0 or i % 5 == 0:\n", " total += i\n", "print(total)" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [], "source": [] } ], "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.7.6" } }, "nbformat": 4, "nbformat_minor": 4 }