From adc16ceddb938d31f733b81fd4974da796809b08 Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Sun, 25 Jan 2015 06:52:00 -0500 Subject: [PATCH] Added IPython Notebook for data structures, starting with snippets for tuples. --- core/structs.ipynb | 267 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 267 insertions(+) create mode 100644 core/structs.ipynb diff --git a/core/structs.ipynb b/core/structs.ipynb new file mode 100644 index 0000000..ec3d283 --- /dev/null +++ b/core/structs.ipynb @@ -0,0 +1,267 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:3a81774c42cd506eb4c0d4a364178d879dabd4791d93008c2f967309d872bd37" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Data Structures" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Tuples" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# One dimensional, fixed-length, immutable sequence\n", + "tup = (1, 2, 3)\n", + "tup" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 46, + "text": [ + "(1, 2, 3)" + ] + } + ], + "prompt_number": 46 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "list_x = [1, 2, 3]" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 47 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Convert to a tuple\n", + "type(tuple(list_x))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 48, + "text": [ + "tuple" + ] + } + ], + "prompt_number": 48 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Nested tuples\n", + "nested_tup = ([1, 2, 3], (4, 5))\n", + "nested_tup" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 56, + "text": [ + "([1, 2, 3], (4, 5))" + ] + } + ], + "prompt_number": 56 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Access by index\n", + "nested_tup[0]" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 57, + "text": [ + "[1, 2, 3]" + ] + } + ], + "prompt_number": 57 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Although tuples are immutable, their contents can contain mutable objects\n", + "nested_tup[0].append(4)\n", + "nested_tup[0]" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 58, + "text": [ + "[1, 2, 3, 4]" + ] + } + ], + "prompt_number": 58 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Concatenate tuples with +\n", + "(1, 3, 2) + (4, 5, 6)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 59, + "text": [ + "(1, 3, 2, 4, 5, 6)" + ] + } + ], + "prompt_number": 59 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Multiply copies references to objects (objects themselves are not copied)\n", + "('foo', 'bar') * 2" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 60, + "text": [ + "('foo', 'bar', 'foo', 'bar')" + ] + } + ], + "prompt_number": 60 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Unpack tuples\n", + "a, b = nested_tup\n", + "a, b" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 66, + "text": [ + "([1, 2, 3, 4], (4, 5))" + ] + } + ], + "prompt_number": 66 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Unpack nested tuples\n", + "(a, b, c, d), (e, f) = nested_tup\n", + "a, b, c, d, e, f" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 68, + "text": [ + "(1, 2, 3, 4, 4, 5)" + ] + } + ], + "prompt_number": 68 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# A common use of variable unpacking is when iterating over sequences\n", + "# of tuples or lists\n", + "seq = [( 1, 2, 3), (4, 5, 6), (7, 8, 9)] \n", + "for a, b, c in seq: \n", + " print(a, b, c)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "(1, 2, 3)\n", + "(4, 5, 6)\n", + "(7, 8, 9)\n" + ] + } + ], + "prompt_number": 72 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file