diff --git a/core/basics.ipynb b/core/basics.ipynb index 738c5d0..451a891 100644 --- a/core/basics.ipynb +++ b/core/basics.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:13e6414e26c196bc6cbaf0093750187f11c2637e20975d01f8e38416d518abb0" + "signature": "sha256:4c5eb8edee509b17587401073eef999428a00c5241d92995407623e288d445f1" }, "nbformat": 3, "nbformat_minor": 0, @@ -64,11 +64,17 @@ ], "prompt_number": 2 }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "list() always creates a new list" + ] + }, { "cell_type": "code", "collapsed": false, "input": [ - "# list always creates a new list\n", "c = list(a)\n", "a == c" ], @@ -113,12 +119,17 @@ "## Types" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Knowing the type of an object is important, as it's useful to write functions that can handle many types of input." + ] + }, { "cell_type": "code", "collapsed": false, "input": [ - "# Knowing the type of an object is important, as it's useful to write \n", - "# functions that can handle many types of input.\n", "type('foo')" ], "language": "python", @@ -135,11 +146,17 @@ ], "prompt_number": 5 }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "isinstance() can take a type or tuple" + ] + }, { "cell_type": "code", "collapsed": false, "input": [ - "# isinstance can take a type or tuple\n", "isinstance(7, (int, float))" ], "language": "python", @@ -163,11 +180,17 @@ "## Attributes and Methods" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "List all attributes and methods" + ] + }, { "cell_type": "code", "collapsed": false, "input": [ - "# List all attributes and methods\n", "dir('foo')" ], "language": "python", @@ -254,11 +277,17 @@ ], "prompt_number": 7 }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Access help" + ] + }, { "cell_type": "code", "collapsed": false, "input": [ - "# Access help\n", "help(str.upper)" ], "language": "python", @@ -280,12 +309,17 @@ ], "prompt_number": 8 }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Access an attribute or method by name getattr, hasattr, and setattr can be used to write generic, reusable code" + ] + }, { "cell_type": "code", "collapsed": false, "input": [ - "# Access an attribute or method by name\n", - "# getattr, hasattr, and setattr can be used to write generic, reusable code\n", "getattr('foo', 'split')" ], "language": "python", @@ -332,8 +366,8 @@ "\n", " @classmethod\n", " def convert_to_list(cls, obj):\n", - " \"\"\"Converts obj to a list if it is not a list and it is iterable, else\n", - " returns the original obj.\n", + " \"\"\"Converts obj to a list if it is not a list and it is iterable, \n", + " else returns the original obj.\n", " \"\"\"\n", " if not isinstance(obj, list) and cls.is_iterable(obj):\n", " obj = list(obj)\n", @@ -508,12 +542,17 @@ "## range and xrange" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Generate a list of evenly spaced integers. Note: range is no longer avalable in Python 3 " + ] + }, { "cell_type": "code", "collapsed": false, "input": [ - "# Note: range is no longer avalable in Python 3\n", - "# Generate a list of evenly spaced integers\n", "range(10)" ], "language": "python", @@ -530,11 +569,17 @@ ], "prompt_number": 16 }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Range can take start, stop, and step arguments" + ] + }, { "cell_type": "code", "collapsed": false, "input": [ - "# Range can take start, stop, and step arguments\n", "range(0, 20, 3)" ], "language": "python", @@ -551,11 +596,17 @@ ], "prompt_number": 17 }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "It is very common to iterate through sequences by index with range" + ] + }, { "cell_type": "code", "collapsed": false, "input": [ - "# It is very common to iterate through sequences by index with range\n", "seq = [1, 2, 3]\n", "for i in range(len(seq)):\n", " val = seq[i]\n", @@ -576,13 +627,17 @@ ], "prompt_number": 18 }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "xrange is recommended for longer ranges and is available in Python 3 as range. It returns an iterator that generates integers one by one rather than all at once and storing them in a large list." + ] + }, { "cell_type": "code", "collapsed": false, "input": [ - "# xrange is recommended for longer ranges and is available in Python 3 as \n", - "# range. It returns an iterator that generates integers one by one rather\n", - "# than all at once and storing them in a large list.\n", "sum = 0\n", "for i in xrange(100000):\n", " if i % 2 == 0:\n", @@ -601,15 +656,6 @@ } ], "prompt_number": 19 - }, - { - "cell_type": "code", - "collapsed": false, - "input": [], - "language": "python", - "metadata": {}, - "outputs": [], - "prompt_number": 19 } ], "metadata": {}