{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python: As a very good scripting language\n", "Gonzalo Rios - grios@dim.uchile.cl\n", "\n", "https://docs.python.org/3/tutorial/index.html" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:26:24.466298Z", "start_time": "2018-09-25T19:26:24.463128Z" } }, "outputs": [], "source": [ "'hola'" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:26:26.351829Z", "start_time": "2018-09-25T19:26:26.348784Z" } }, "outputs": [], "source": [ "\"hola\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:26:31.337868Z", "start_time": "2018-09-25T19:26:31.334780Z" } }, "outputs": [], "source": [ "1 + 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:26:38.977722Z", "start_time": "2018-09-25T19:26:38.974296Z" } }, "outputs": [], "source": [ "1e9 * 1.0125 // 33.93 ** 2.2" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:26:52.661801Z", "start_time": "2018-09-25T19:26:52.659107Z" } }, "outputs": [], "source": [ "#Comment\n", "x = 'chao'\n", "print(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:27:14.171060Z", "start_time": "2018-09-25T19:27:14.167572Z" } }, "outputs": [], "source": [ "x" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:27:31.864405Z", "start_time": "2018-09-25T19:27:31.859415Z" } }, "outputs": [], "source": [ "help(help)\n", "print('\\n' + (('*' * 90) + '\\n') * 5)\n", "help(print)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:27:55.782208Z", "start_time": "2018-09-25T19:27:55.778751Z" } }, "outputs": [], "source": [ "p = print\n", "p" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Jupyter Notebook\n", "Markdown and $Latex$ support. Nbextensions. Templates." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Esta es una funcion cosntante: $f(x) = \\pi$" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:29:31.086111Z", "start_time": "2018-09-25T19:29:31.083053Z" } }, "outputs": [], "source": [ "p?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:29:50.130861Z", "start_time": "2018-09-25T19:29:50.020660Z" } }, "outputs": [], "source": [ "!ls" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:30:11.507857Z", "start_time": "2018-09-25T19:30:11.491486Z" } }, "outputs": [], "source": [ "%lsmagic" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:30:30.346340Z", "start_time": "2018-09-25T19:30:30.336493Z" } }, "outputs": [], "source": [ "%%bash\n", "pwd\n", "ls\n", "cd ..\n", "echo\n", "pwd\n", "ls" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:30:47.506350Z", "start_time": "2018-09-25T19:30:47.500380Z" } }, "outputs": [], "source": [ "%%latex\n", "\\begin{align}\n", "a = \\frac{1}{2} && b = \\frac{1}{3} && c = \\frac{1}{4} \\\\\n", "a && b && c \\\\\n", "1 && 2 && 3\n", "\\end{align}" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:31:02.349349Z", "start_time": "2018-09-25T19:31:02.343519Z" } }, "outputs": [], "source": [ "%%HTML\n", "

Jupyter Notebooks can render HTML

\n", "" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:31:17.608744Z", "start_time": "2018-09-25T19:31:17.603904Z" } }, "outputs": [], "source": [ "%%time\n", "p('hola')\n", "p('chao')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:31:31.817231Z", "start_time": "2018-09-25T19:31:31.809837Z" } }, "outputs": [], "source": [ "%time p('hola')\n", "p('chao')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:31:56.282850Z", "start_time": "2018-09-25T19:31:46.660964Z" } }, "outputs": [], "source": [ "%%timeit\n", "p('hola')\n", "p('chao')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:32:21.133920Z", "start_time": "2018-09-25T19:32:16.544027Z" } }, "outputs": [], "source": [ "%timeit p('hola')\n", "p('chao')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:32:33.379188Z", "start_time": "2018-09-25T19:32:33.373443Z" } }, "outputs": [], "source": [ "%%prun\n", "p('hola')\n", "p('chao')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:32:57.599973Z", "start_time": "2018-09-25T19:32:57.594682Z" }, "scrolled": true }, "outputs": [], "source": [ "%hist" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Strings" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:34:54.875569Z", "start_time": "2018-09-25T19:34:54.872264Z" } }, "outputs": [], "source": [ "'Hello world!'" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:34:56.813077Z", "start_time": "2018-09-25T19:34:56.809775Z" } }, "outputs": [], "source": [ "\"Spam and eggs...\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:35:00.579565Z", "start_time": "2018-09-25T19:35:00.576096Z" } }, "outputs": [], "source": [ "\"First string\" + \", \" + \"second string\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:35:11.856460Z", "start_time": "2018-09-25T19:35:11.853142Z" } }, "outputs": [], "source": [ "\"spam \" * 3" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:35:24.383450Z", "start_time": "2018-09-25T19:35:24.375588Z" } }, "outputs": [], "source": [ "p(42, '42', str(42))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:35:43.986718Z", "start_time": "2018-09-25T19:35:43.983263Z" } }, "outputs": [], "source": [ "42, '42', str(42)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:36:05.344117Z", "start_time": "2018-09-25T19:36:05.339120Z" } }, "outputs": [], "source": [ "42 == '42'" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:36:09.291487Z", "start_time": "2018-09-25T19:36:09.288165Z" } }, "outputs": [], "source": [ "str(42) == '42'" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:36:13.230127Z", "start_time": "2018-09-25T19:36:13.226769Z" } }, "outputs": [], "source": [ "\"Spam \" * 3 + str(42) * 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Strings Functions" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:37:01.597208Z", "start_time": "2018-09-25T19:37:01.593722Z" } }, "outputs": [], "source": [ "\"Numbers: {0} {1} {2}\".format(34, 75, 847)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:37:16.727560Z", "start_time": "2018-09-25T19:37:16.723809Z" } }, "outputs": [], "source": [ "\"Numbers: {x},{y}\".format(y=1, x=2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:37:28.167137Z", "start_time": "2018-09-25T19:37:28.163613Z" } }, "outputs": [], "source": [ "\", \".join([\"spam\", \"eggs\", \"ham\"])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:37:40.674257Z", "start_time": "2018-09-25T19:37:40.670387Z" } }, "outputs": [], "source": [ "\"Hello ME\".replace(\"ME\", \"world\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:37:45.372245Z", "start_time": "2018-09-25T19:37:45.368487Z" } }, "outputs": [], "source": [ "\"This is a sentence.\".startswith(\"This\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:37:47.173082Z", "start_time": "2018-09-25T19:37:47.169873Z" } }, "outputs": [], "source": [ "\"This is a sentence.\".endswith(\"sentence.\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:37:49.066741Z", "start_time": "2018-09-25T19:37:49.063641Z" } }, "outputs": [], "source": [ "\"This is a sentence.\".upper()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:37:51.726530Z", "start_time": "2018-09-25T19:37:51.723156Z" } }, "outputs": [], "source": [ "\"AN ALL CAPS SENTENCE\".lower()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:37:55.508551Z", "start_time": "2018-09-25T19:37:55.504337Z" } }, "outputs": [], "source": [ "\"spam, eggs, ham\".split(\", \")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:38:02.994790Z", "start_time": "2018-09-25T19:38:02.990437Z" } }, "outputs": [], "source": [ "len('abcdefghi')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:38:16.883180Z", "start_time": "2018-09-25T19:38:16.877548Z" } }, "outputs": [], "source": [ "chr(254), ord('รพ')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Numbers" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:39:12.388387Z", "start_time": "2018-09-25T19:39:12.384855Z" } }, "outputs": [], "source": [ "(1 + 2 * 3 % 2 ** 2) ** 2 // 2 - 5 / 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:39:20.612600Z", "start_time": "2018-09-25T19:39:20.609230Z" } }, "outputs": [], "source": [ "p(2 + 3)\n", "\n", "p(-2 * 9)\n", "\n", "p(2**8 + 1 / 8)\n", "\n", "p(20 // 6 + 1.25 % 0.5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Numbers Functions" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:39:32.053487Z", "start_time": "2018-09-25T19:39:32.047884Z" } }, "outputs": [], "source": [ "pow(10, 0.5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:39:38.720132Z", "start_time": "2018-09-25T19:39:38.716636Z" } }, "outputs": [], "source": [ "round(101.26, -1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:39:54.668773Z", "start_time": "2018-09-25T19:39:54.663100Z" } }, "outputs": [], "source": [ "round(101.26, 1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:40:00.493442Z", "start_time": "2018-09-25T19:40:00.487973Z" } }, "outputs": [], "source": [ "round(101.26, 0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:40:19.606351Z", "start_time": "2018-09-25T19:40:19.600789Z" } }, "outputs": [], "source": [ "int(10.2), int('100', base=2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:40:37.997190Z", "start_time": "2018-09-25T19:40:37.993673Z" } }, "outputs": [], "source": [ "int('100',base=13)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:40:44.169452Z", "start_time": "2018-09-25T19:40:44.165802Z" } }, "outputs": [], "source": [ "(1024).bit_length()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:40:48.508445Z", "start_time": "2018-09-25T19:40:48.505132Z" } }, "outputs": [], "source": [ "float(10.1413614624242245242), float('10.e2')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:41:13.337191Z", "start_time": "2018-09-25T19:41:13.332037Z" } }, "outputs": [], "source": [ "(-2.0).is_integer()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:41:31.875202Z", "start_time": "2018-09-25T19:41:31.869435Z" } }, "outputs": [], "source": [ "complex(1, 5.52352), complex('4+3e-1j').conjugate()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:41:34.104330Z", "start_time": "2018-09-25T19:41:34.101116Z" } }, "outputs": [], "source": [ "(1 + 2j).real, (1 + 2j).imag" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:41:38.029471Z", "start_time": "2018-09-25T19:41:38.026195Z" } }, "outputs": [], "source": [ "abs(-10.1), abs(1 + 1j)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:41:57.005096Z", "start_time": "2018-09-25T19:41:56.999437Z" } }, "outputs": [], "source": [ "divmod(10, 3), divmod(10.1, 2.5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:42:03.434505Z", "start_time": "2018-09-25T19:42:03.428515Z" } }, "outputs": [], "source": [ "hex(1234567), oct(1234567), bin(1234567)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Variables" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:42:52.298649Z", "start_time": "2018-09-25T19:42:52.294624Z" } }, "outputs": [], "source": [ "x = \"X\" * 3\n", "y = 5\n", "x * y" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:43:22.940234Z", "start_time": "2018-09-25T19:43:22.934664Z" } }, "outputs": [], "source": [ "# Operators\n", "x = 42.245\n", "p(x)\n", "\n", "x += 3\n", "p(x)\n", "\n", "x -= 5\n", "p(x)\n", "\n", "x *= 2\n", "p(x)\n", "\n", "x /= 4\n", "p(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:43:41.406325Z", "start_time": "2018-09-25T19:43:41.401854Z" } }, "outputs": [], "source": [ "x = \"spam\"\n", "p(x)\n", "\n", "x += \"eggs\"\n", "p(x)\n", "\n", "x *= 3\n", "p(x)\n", "\n", "x, x.find('egg')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:45:13.946505Z", "start_time": "2018-09-25T19:45:13.943921Z" } }, "outputs": [], "source": [ "# x.\n", "# space name = 7\n", "# 123abc = 7\n", "# f\"He said his name is {x}.\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Control Flow" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:45:36.658492Z", "start_time": "2018-09-25T19:45:36.654642Z" } }, "outputs": [], "source": [ "# Boolean\n", "False, True, bool(0), bool(1), bool(False), bool(True), bool(None), bool(bool)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:47:14.679733Z", "start_time": "2018-09-25T19:47:14.676001Z" } }, "outputs": [], "source": [ "a = 7\n", "b = 1 if a <= 5 else 42\n", "b" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:47:51.204175Z", "start_time": "2018-09-25T19:47:51.199581Z" } }, "outputs": [], "source": [ "# if, else, elif\n", "my_boolean = False # True\n", "if(2 != 10 and my_boolean):\n", " p(2)\n", "elif(not (7 <= 5 or 42 == \"the answer to the life the universe and everything\")):\n", " p(x * 42)\n", "else:\n", " p(x * 43)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:48:11.869023Z", "start_time": "2018-09-25T19:48:09.144860Z" } }, "outputs": [], "source": [ "%%timeit\n", "b = 1 if a > 5 else 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:48:14.609623Z", "start_time": "2018-09-25T19:48:11.870602Z" } }, "outputs": [], "source": [ "%%timeit\n", "if a > 5:\n", " b = 1\n", "else:\n", " b = 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:50:21.258654Z", "start_time": "2018-09-25T19:50:21.255847Z" } }, "outputs": [], "source": [ "# while - break\n", "i = 1\n", "while 1 == 1:\n", " p(i)\n", " i += 1\n", " if i >= 5:\n", " p(\"Breaking\\n\")\n", " break" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:51:19.113334Z", "start_time": "2018-09-25T19:51:19.107132Z" } }, "outputs": [], "source": [ "# while - continue\n", "i = 0\n", "j = 3\n", "while True:\n", " i += 1\n", " if i == j:\n", " p(\"Skipping \" + str(j))\n", " continue\n", " elif i == 5:\n", " p(\"Breaking\")\n", " break\n", " else:\n", " p(\"Passing \" + str(i)) \n", " pass\n", " p(i)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:52:05.397265Z", "start_time": "2018-09-25T19:52:05.394149Z" } }, "outputs": [], "source": [ "# For loop, else\n", "words = [\"hello\", \"world\", \"spam\", \"eggs\"]\n", "for word in words:\n", " p(word + \"!\")\n", "else:\n", " p(\"Ham \")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exceptions" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:52:28.533033Z", "start_time": "2018-09-25T19:52:28.514069Z" } }, "outputs": [], "source": [ "11/0" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:53:22.827022Z", "start_time": "2018-09-25T19:53:22.814775Z" } }, "outputs": [], "source": [ "try:\n", " 11/0\n", " p('try')\n", "except ZeroDivisionError as m:\n", " p('zero')\n", "except Exception as m:\n", " p(m)\n", "else:\n", " p('else')\n", "finally:\n", " p('finally')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:53:43.657098Z", "start_time": "2018-09-25T19:53:43.653487Z" } }, "outputs": [], "source": [ "try:\n", " 1 + '2' + 3 + '4'\n", " p('try')\n", "except ZeroDivisionError as m:\n", " p('zero')\n", "except Exception as m:\n", " p(m)\n", "else:\n", " p('else')\n", "finally:\n", " p('finally')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:54:01.655279Z", "start_time": "2018-09-25T19:54:01.650368Z" } }, "outputs": [], "source": [ "try:\n", " pass\n", " p('try')\n", "except ZeroDivisionError as m:\n", " p('zero')\n", "except Exception as m:\n", " p(m)\n", "else:\n", " p('else')\n", "finally:\n", " p('finally')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:54:57.198852Z", "start_time": "2018-09-25T19:54:57.192411Z" } }, "outputs": [], "source": [ "x = 11\n", "y = 0\n", "if y is 0:\n", " raise Exception('ERROR! '*10)\n", "else:\n", " p(x/y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Structures\n", "https://docs.python.org/3/tutorial/datastructures.html" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:55:58.419876Z", "start_time": "2018-09-25T19:55:58.414516Z" } }, "outputs": [], "source": [ "# Data Structures\n", "tuples = (1, 2, 3, 4)\n", "sets = {1, 2, 3, 4}\n", "lists = [1, 2, 3, 4]\n", "dictionaries = {1: 1, 2: 2, 3: 3, 4: 4}\n", "\n", "p(tuples)\n", "p(sets)\n", "p(dictionaries)\n", "p(lists)\n", "len(lists)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Tuples" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:56:19.250689Z", "start_time": "2018-09-25T19:56:19.245032Z" } }, "outputs": [], "source": [ "words = (\"spam\", \"eggs\", \"sausages\")\n", "words" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:56:23.745981Z", "start_time": "2018-09-25T19:56:23.742867Z" } }, "outputs": [], "source": [ "words[2]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:56:30.232727Z", "start_time": "2018-09-25T19:56:30.225549Z" } }, "outputs": [], "source": [ "# words[3]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:57:22.353301Z", "start_time": "2018-09-25T19:57:22.347824Z" } }, "outputs": [], "source": [ "# words[2] = \"fries\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:58:49.969509Z", "start_time": "2018-09-25T19:58:49.963805Z" } }, "outputs": [], "source": [ "nums = \"one\", \"two\", \"three\", \"four\"\n", "nums[0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:58:50.424984Z", "start_time": "2018-09-25T19:58:50.154868Z" } }, "outputs": [], "source": [ "a, *b, c = nums\n", "b" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:58:58.648626Z", "start_time": "2018-09-25T19:58:58.644933Z" } }, "outputs": [], "source": [ "t = tuple((1, 2))\n", "t_ = (1, 2)\n", "t == t_" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:59:18.943153Z", "start_time": "2018-09-25T19:59:18.938970Z" } }, "outputs": [], "source": [ "t + t_ * 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Sets" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:59:47.130524Z", "start_time": "2018-09-25T19:59:47.126932Z" } }, "outputs": [], "source": [ "num_set = {1, 2, 3, 4, 5}\n", "word_set = set((\"spam\", \"eggs\", \"sausage\"))\n", "p(num_set)\n", "p(word_set)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T19:59:56.672501Z", "start_time": "2018-09-25T19:59:56.668761Z" } }, "outputs": [], "source": [ "3 in num_set" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:00:01.743162Z", "start_time": "2018-09-25T20:00:01.740213Z" } }, "outputs": [], "source": [ "\"spam\" not in word_set" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:00:11.983455Z", "start_time": "2018-09-25T20:00:11.977417Z" } }, "outputs": [], "source": [ "num_set.add(-7)\n", "num_set.remove(3)\n", "num_set" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:00:32.528276Z", "start_time": "2018-09-25T20:00:32.524697Z" } }, "outputs": [], "source": [ "num_set2 = {4, 5, 6, 7}\n", "p(num_set | num_set2)\n", "p(num_set & num_set2)\n", "p(num_set - num_set2)\n", "p(num_set2 - num_set)\n", "p(num_set ^ num_set2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Dictionaries" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:01:14.065129Z", "start_time": "2018-09-25T20:01:14.062365Z" } }, "outputs": [], "source": [ "empty = dict()\n", "ages = {\"A\": 24, \"B\": 42, 3: 58}\n", "p(empty)\n", "p(ages)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:01:42.385128Z", "start_time": "2018-09-25T20:01:42.373463Z" } }, "outputs": [], "source": [ "ages[\"A\"] + ages[3]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:02:06.053579Z", "start_time": "2018-09-25T20:02:06.050604Z" } }, "outputs": [], "source": [ "primary = {\n", " \"red\": [255, 0, 0],\n", " \"blue\": [0, 255, 0],\n", " \"green\": [0, 0, 255],\n", "}\n", "p(primary)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:02:25.308855Z", "start_time": "2018-09-25T20:02:25.304583Z" } }, "outputs": [], "source": [ "primary[\"black\"] = [0, 0, 0]\n", "primary" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:02:30.467335Z", "start_time": "2018-09-25T20:02:30.463275Z" } }, "outputs": [], "source": [ "\"black\" in primary" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:03:34.908255Z", "start_time": "2018-09-25T20:03:34.904305Z" } }, "outputs": [], "source": [ "\"white\" not in primary" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:03:36.426714Z", "start_time": "2018-09-25T20:03:36.423271Z" } }, "outputs": [], "source": [ "primary.get(\"black\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:03:55.446354Z", "start_time": "2018-09-25T20:03:55.440628Z" } }, "outputs": [], "source": [ "primary.get(\"white\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:03:57.038896Z", "start_time": "2018-09-25T20:03:57.035333Z" } }, "outputs": [], "source": [ "primary.get(\"white\", [255, 255, 255])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:03:58.998875Z", "start_time": "2018-09-25T20:03:58.992439Z" } }, "outputs": [], "source": [ "p(primary)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:04:11.554827Z", "start_time": "2018-09-25T20:04:11.551105Z" } }, "outputs": [], "source": [ "empty.update(ages)\n", "empty.update(primary)\n", "empty" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:06:04.615279Z", "start_time": "2018-09-25T20:06:04.612599Z" } }, "outputs": [], "source": [ "tuple_dict = {(1, 2, 3): \"one two three\", }\n", "# list_dict = {[1, 2, 3]: \"one two three\",}" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:06:04.955102Z", "start_time": "2018-09-25T20:06:04.882528Z" } }, "outputs": [], "source": [ "tuple_dict" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Lists" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:09:49.127700Z", "start_time": "2018-09-25T20:09:49.124955Z" } }, "outputs": [], "source": [ "l = list((1, 2, 3))\n", "l_ = [1, 2, 3]\n", "p(l)\n", "p(l_)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:09:53.482285Z", "start_time": "2018-09-25T20:09:53.479060Z" } }, "outputs": [], "source": [ "p(3 in l)\n", "p(0 not in l_)\n", "p(l == l_)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:09:58.201735Z", "start_time": "2018-09-25T20:09:58.198517Z" } }, "outputs": [], "source": [ "l_.append(0)\n", "p(l_)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:10:03.160579Z", "start_time": "2018-09-25T20:10:03.157165Z" } }, "outputs": [], "source": [ "l == l_" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:11:22.344749Z", "start_time": "2018-09-25T20:11:22.340984Z" } }, "outputs": [], "source": [ "l.sort()\n", "l" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:11:24.042848Z", "start_time": "2018-09-25T20:11:24.038477Z" } }, "outputs": [], "source": [ "l.reverse()\n", "l" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:11:27.024206Z", "start_time": "2018-09-25T20:11:27.020687Z" } }, "outputs": [], "source": [ "l.extend((10, 11, 12, 13))\n", "l" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:11:34.204114Z", "start_time": "2018-09-25T20:11:34.200803Z" } }, "outputs": [], "source": [ "l.append(11)\n", "l" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:11:39.092204Z", "start_time": "2018-09-25T20:11:39.088928Z" } }, "outputs": [], "source": [ "l.index(11)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:11:48.149318Z", "start_time": "2018-09-25T20:11:48.144166Z" } }, "outputs": [], "source": [ "l.count(11)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:11:57.356124Z", "start_time": "2018-09-25T20:11:57.352385Z" } }, "outputs": [], "source": [ "l.clear()\n", "l" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:12:34.215758Z", "start_time": "2018-09-25T20:12:34.209740Z" }, "scrolled": true }, "outputs": [], "source": [ "l.insert(0, 0)\n", "l.insert(0, 1)\n", "l.insert(0, 3)\n", "l" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:12:52.450691Z", "start_time": "2018-09-25T20:12:52.446622Z" }, "scrolled": true }, "outputs": [], "source": [ "l.pop()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:12:54.777751Z", "start_time": "2018-09-25T20:12:54.773771Z" } }, "outputs": [], "source": [ "l" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:16:49.182149Z", "start_time": "2018-09-25T20:16:49.177581Z" } }, "outputs": [], "source": [ "x = [30]\n", "l.append(x)\n", "l" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:17:04.291662Z", "start_time": "2018-09-25T20:17:04.286640Z" }, "scrolled": true }, "outputs": [], "source": [ "x.append(40)\n", "l" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:13:07.640012Z", "start_time": "2018-09-25T20:13:07.634824Z" } }, "outputs": [], "source": [ "l.remove(3)\n", "l" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:13:26.731577Z", "start_time": "2018-09-25T20:13:26.727913Z" } }, "outputs": [], "source": [ "l = l * 10 + l_ * 3\n", "l" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:13:33.149266Z", "start_time": "2018-09-25T20:13:33.144728Z" } }, "outputs": [], "source": [ "l.sort()\n", "l" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:18:16.983884Z", "start_time": "2018-09-25T20:18:16.974286Z" } }, "outputs": [], "source": [ "# Strings as tuples\n", "words = [\"Hello\", \"world\", \"!\"]\n", "p(words)\n", "p(words[0])\n", "p(words[1][0])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:19:19.804251Z", "start_time": "2018-09-25T20:19:19.798995Z" } }, "outputs": [], "source": [ "# Mixed lists\n", "things = [\"string\", 0, [1, 2, \"number\"], 4.56]\n", "p(things)\n", "p(things[1])\n", "p(things[2])\n", "p(things[2][2][2])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:19:51.296821Z", "start_time": "2018-09-25T20:19:51.292120Z" } }, "outputs": [], "source": [ "# List Operations\n", "nums = [7, 7, 7, 7, 7]\n", "nums[2] = 5\n", "nums[0] = nums[2] - nums[3]\n", "nums[4] /= 2\n", "p(nums)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:19:54.357069Z", "start_time": "2018-09-25T20:19:54.353495Z" } }, "outputs": [], "source": [ "nums + [4, 5, 6]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:20:08.967210Z", "start_time": "2018-09-25T20:20:08.963785Z" } }, "outputs": [], "source": [ "nums * 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Others Structures" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:20:31.653680Z", "start_time": "2018-09-25T20:20:31.648679Z" } }, "outputs": [], "source": [ "b'abcefg'" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:20:33.013662Z", "start_time": "2018-09-25T20:20:33.010111Z" } }, "outputs": [], "source": [ "f = bytes((123, 57, 75, 245))\n", "f" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:20:50.216510Z", "start_time": "2018-09-25T20:20:50.206641Z" } }, "outputs": [], "source": [ "try:\n", " f.append(0)\n", "except Exception as e:\n", " p(e)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:22:10.827655Z", "start_time": "2018-09-25T20:22:10.824091Z" } }, "outputs": [], "source": [ "f = bytearray((123, 57, 75, 245))\n", "f.append(0)\n", "f" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:22:14.063505Z", "start_time": "2018-09-25T20:22:14.060131Z" } }, "outputs": [], "source": [ "f.reverse()\n", "f" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:22:16.970768Z", "start_time": "2018-09-25T20:22:16.966421Z" } }, "outputs": [], "source": [ "f = memoryview(f)\n", "f" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:22:41.998778Z", "start_time": "2018-09-25T20:22:41.989652Z" } }, "outputs": [], "source": [ "f.tolist()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:23:02.309448Z", "start_time": "2018-09-25T20:23:02.305236Z" } }, "outputs": [], "source": [ "f = frozenset(f.tolist())\n", "f" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:23:12.605734Z", "start_time": "2018-09-25T20:23:12.602325Z" } }, "outputs": [], "source": [ "try:\n", " f.append(0)\n", "except Exception as e:\n", " p(e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Slicing" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:23:54.843624Z", "start_time": "2018-09-25T20:23:54.837443Z" } }, "outputs": [], "source": [ "t = (1, 2, 3, 4, 5)\n", "l = [1, 2, 3, 4, 5]\n", "s = \"12345\"\n", "\n", "p(l[1:3])\n", "p(t[1:3])\n", "p(s[1:3])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:25:24.112198Z", "start_time": "2018-09-25T20:25:24.107858Z" } }, "outputs": [], "source": [ "p(l[0:2])\n", "p(l[2:4])\n", "p(l[4:6])\n", "p(l[3:])\n", "p(l[:3])\n", "p(l[0:5:2])\n", "p(l[1:-1])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:28:55.588715Z", "start_time": "2018-09-25T20:28:55.585154Z" } }, "outputs": [], "source": [ "l[0] = 4\n", "l" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:29:04.490143Z", "start_time": "2018-09-25T20:29:04.481074Z" } }, "outputs": [], "source": [ "l[0:5]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:29:07.256155Z", "start_time": "2018-09-25T20:29:07.244591Z" } }, "outputs": [], "source": [ "l[:]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:29:22.586856Z", "start_time": "2018-09-25T20:29:22.581404Z" } }, "outputs": [], "source": [ "l[1:3] = [5, 7]\n", "l" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:29:30.060445Z", "start_time": "2018-09-25T20:29:30.055314Z" } }, "outputs": [], "source": [ "slice(1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:29:32.064386Z", "start_time": "2018-09-25T20:29:32.060886Z" } }, "outputs": [], "source": [ "slice(1, 2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:29:33.043924Z", "start_time": "2018-09-25T20:29:33.040530Z" } }, "outputs": [], "source": [ "slice(1, 2, 3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:29:49.239201Z", "start_time": "2018-09-25T20:29:49.236906Z" } }, "outputs": [], "source": [ "xx = slice(1, 2, 3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:30:46.805206Z", "start_time": "2018-09-25T20:30:46.800098Z" } }, "outputs": [], "source": [ "l[xx]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:30:51.528426Z", "start_time": "2018-09-25T20:30:51.522717Z" } }, "outputs": [], "source": [ "l[slice(1, -1, 2)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:30:52.839651Z", "start_time": "2018-09-25T20:30:52.836344Z" } }, "outputs": [], "source": [ "l[1:-1:2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# I/O" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:31:05.170336Z", "start_time": "2018-09-25T20:31:05.168106Z" } }, "outputs": [], "source": [ "p = print" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:31:25.128085Z", "start_time": "2018-09-25T20:31:16.912533Z" } }, "outputs": [], "source": [ "inText = input(\"Enter something please: \")\n", "p(inText)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:31:49.668653Z", "start_time": "2018-09-25T20:31:49.663135Z" } }, "outputs": [], "source": [ "print(3, 4, ['hola', 'chao'], '?', print)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:32:05.633757Z", "start_time": "2018-09-25T20:32:05.630799Z" } }, "outputs": [], "source": [ "print(3, 4, sep=',', end=' EOF\\n', file=None, flush=False)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:32:43.957655Z", "start_time": "2018-09-25T20:32:43.950380Z" } }, "outputs": [], "source": [ "# write mode\n", "file = open(\"filename.txt\", \"w\")\n", "file.write(str(123))\n", "file.close()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:33:12.914950Z", "start_time": "2018-09-25T20:33:12.911392Z" } }, "outputs": [], "source": [ "# read mode\n", "file = open(\"filename.txt\", \"r\")\n", "p(file.read())\n", "file.close()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:33:39.012908Z", "start_time": "2018-09-25T20:33:39.010015Z" } }, "outputs": [], "source": [ "# append mode\n", "file_test = open(\"filename.txt\", \"a\")\n", "print(4,5,6, sep=',', end=' EOF\\n', file=file_test, flush=True)\n", "file_test.close()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:35:24.854842Z", "start_time": "2018-09-25T20:35:24.851358Z" } }, "outputs": [], "source": [ "# read binary mode\n", "file = open(\"filename.txt\", \"rb\")\n", "p(file.read())\n", "file.close()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:36:30.134990Z", "start_time": "2018-09-25T20:36:30.126351Z" } }, "outputs": [], "source": [ "# good practice\n", "try:\n", " f = open(\"filename.txt\")\n", " print(f.read())\n", "finally:\n", " f.close()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:37:04.817533Z", "start_time": "2018-09-25T20:37:04.814413Z" } }, "outputs": [], "source": [ "# with statment\n", "with open(\"filename.txt\") as f:\n", " p(f.read())" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# Compile" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:37:57.832097Z", "start_time": "2018-09-25T20:37:57.828469Z" } }, "outputs": [], "source": [ "python_code = \"7*64*'str'\"\n", "c = compile(python_code, 'test.x', 'exec')\n", "c, exec(c), eval(c)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:38:34.287946Z", "start_time": "2018-09-25T20:38:34.274887Z" } }, "outputs": [], "source": [ "c = compile(python_code, 'test.x', 'eval')\n", "c, exec(c), eval(c)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# Site" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:39:21.697860Z", "start_time": "2018-09-25T20:39:21.694659Z" } }, "outputs": [], "source": [ "credits" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:39:24.188306Z", "start_time": "2018-09-25T20:39:24.184840Z" } }, "outputs": [], "source": [ "copyright" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:39:35.156929Z", "start_time": "2018-09-25T20:39:25.888116Z" } }, "outputs": [], "source": [ "license()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:40:08.057082Z", "start_time": "2018-09-25T20:40:08.053286Z" } }, "outputs": [], "source": [ "import this" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-25T20:40:26.782907Z", "start_time": "2018-09-25T20:40:26.778017Z" } }, "outputs": [], "source": [ "quit()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "start_time": "2018-09-25T20:40:27.113Z" } }, "outputs": [], "source": [ "exit()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [default]", "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.6.5" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }