Converted python comments to markdown to improve readability.

This commit is contained in:
Donne Martin 2015-01-25 09:13:09 -05:00
parent 60b1901186
commit 1bc5e1cf83

View File

@ -1,7 +1,7 @@
{ {
"metadata": { "metadata": {
"name": "", "name": "",
"signature": "sha256:13e6414e26c196bc6cbaf0093750187f11c2637e20975d01f8e38416d518abb0" "signature": "sha256:4c5eb8edee509b17587401073eef999428a00c5241d92995407623e288d445f1"
}, },
"nbformat": 3, "nbformat": 3,
"nbformat_minor": 0, "nbformat_minor": 0,
@ -64,11 +64,17 @@
], ],
"prompt_number": 2 "prompt_number": 2
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"list() always creates a new list"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "collapsed": false,
"input": [ "input": [
"# list always creates a new list\n",
"c = list(a)\n", "c = list(a)\n",
"a == c" "a == c"
], ],
@ -113,12 +119,17 @@
"## Types" "## 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", "cell_type": "code",
"collapsed": false, "collapsed": false,
"input": [ "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')" "type('foo')"
], ],
"language": "python", "language": "python",
@ -135,11 +146,17 @@
], ],
"prompt_number": 5 "prompt_number": 5
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"isinstance() can take a type or tuple"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "collapsed": false,
"input": [ "input": [
"# isinstance can take a type or tuple\n",
"isinstance(7, (int, float))" "isinstance(7, (int, float))"
], ],
"language": "python", "language": "python",
@ -163,11 +180,17 @@
"## Attributes and Methods" "## Attributes and Methods"
] ]
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"List all attributes and methods"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "collapsed": false,
"input": [ "input": [
"# List all attributes and methods\n",
"dir('foo')" "dir('foo')"
], ],
"language": "python", "language": "python",
@ -254,11 +277,17 @@
], ],
"prompt_number": 7 "prompt_number": 7
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Access help"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "collapsed": false,
"input": [ "input": [
"# Access help\n",
"help(str.upper)" "help(str.upper)"
], ],
"language": "python", "language": "python",
@ -280,12 +309,17 @@
], ],
"prompt_number": 8 "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", "cell_type": "code",
"collapsed": false, "collapsed": false,
"input": [ "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')" "getattr('foo', 'split')"
], ],
"language": "python", "language": "python",
@ -332,8 +366,8 @@
"\n", "\n",
" @classmethod\n", " @classmethod\n",
" def convert_to_list(cls, obj):\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", " \"\"\"Converts obj to a list if it is not a list and it is iterable, \n",
" returns the original obj.\n", " else returns the original obj.\n",
" \"\"\"\n", " \"\"\"\n",
" if not isinstance(obj, list) and cls.is_iterable(obj):\n", " if not isinstance(obj, list) and cls.is_iterable(obj):\n",
" obj = list(obj)\n", " obj = list(obj)\n",
@ -508,12 +542,17 @@
"## range and xrange" "## 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", "cell_type": "code",
"collapsed": false, "collapsed": false,
"input": [ "input": [
"# Note: range is no longer avalable in Python 3\n",
"# Generate a list of evenly spaced integers\n",
"range(10)" "range(10)"
], ],
"language": "python", "language": "python",
@ -530,11 +569,17 @@
], ],
"prompt_number": 16 "prompt_number": 16
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Range can take start, stop, and step arguments"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "collapsed": false,
"input": [ "input": [
"# Range can take start, stop, and step arguments\n",
"range(0, 20, 3)" "range(0, 20, 3)"
], ],
"language": "python", "language": "python",
@ -551,11 +596,17 @@
], ],
"prompt_number": 17 "prompt_number": 17
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It is very common to iterate through sequences by index with range"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "collapsed": false,
"input": [ "input": [
"# It is very common to iterate through sequences by index with range\n",
"seq = [1, 2, 3]\n", "seq = [1, 2, 3]\n",
"for i in range(len(seq)):\n", "for i in range(len(seq)):\n",
" val = seq[i]\n", " val = seq[i]\n",
@ -576,13 +627,17 @@
], ],
"prompt_number": 18 "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", "cell_type": "code",
"collapsed": false, "collapsed": false,
"input": [ "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", "sum = 0\n",
"for i in xrange(100000):\n", "for i in xrange(100000):\n",
" if i % 2 == 0:\n", " if i % 2 == 0:\n",
@ -601,15 +656,6 @@
} }
], ],
"prompt_number": 19 "prompt_number": 19
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 19
} }
], ],
"metadata": {} "metadata": {}