Moved range and xrange snippets from basics to structs_utils IPython Notebook.

This commit is contained in:
Donne Martin 2015-01-26 07:23:28 -05:00
parent 79165b5dee
commit a5a778ae6b
2 changed files with 151 additions and 151 deletions

View File

@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:de3e7aeb6537656e464e30d624c213fb29ce962afef43280fb2d84c3a1ca1858"
"signature": "sha256:f18acafc8b418eb856d25b1dc48f6dd4e97b85f845419650b4b4cd4fa7fa3e3c"
},
"nbformat": 3,
"nbformat_minor": 0,
@ -534,128 +534,6 @@
}
],
"prompt_number": 15
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 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": [
"range(10)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 16,
"text": [
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
]
}
],
"prompt_number": 16
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Range can take start, stop, and step arguments"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"range(0, 20, 3)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 17,
"text": [
"[0, 3, 6, 9, 12, 15, 18]"
]
}
],
"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": [
"seq = [1, 2, 3]\n",
"for i in range(len(seq)):\n",
" val = seq[i]\n",
" print(val)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1\n",
"2\n",
"3\n"
]
}
],
"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": [
"sum = 0\n",
"for i in xrange(100000):\n",
" if i % 2 == 0:\n",
" sum += 1\n",
"print(sum)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"50000\n"
]
}
],
"prompt_number": 19
}
],
"metadata": {}

View File

@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:aff21c8ceeca329c78b70a0f35aeb222827eaa712b68639150e540efb9afb0ea"
"signature": "sha256:e8e69422df4f0a0c0dd9481fd9ceb6bf8bc028d54fbe134de2957e387db35dca"
},
"nbformat": 3,
"nbformat_minor": 0,
@ -250,6 +250,128 @@
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 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": [
"range(10)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 9,
"text": [
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
]
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Range can take start, stop, and step arguments"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"range(0, 20, 3)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 10,
"text": [
"[0, 3, 6, 9, 12, 15, 18]"
]
}
],
"prompt_number": 10
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It is very common to iterate through sequences by index with range"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"seq = [1, 2, 3]\n",
"for i in range(len(seq)):\n",
" val = seq[i]\n",
" print(val)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1\n",
"2\n",
"3\n"
]
}
],
"prompt_number": 11
},
{
"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": [
"sum = 0\n",
"for i in xrange(100000):\n",
" if i % 2 == 0:\n",
" sum += 1\n",
"print(sum)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"50000\n"
]
}
],
"prompt_number": 12
},
{
"cell_type": "markdown",
"metadata": {},
@ -273,7 +395,7 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 9
"prompt_number": 13
},
{
"cell_type": "markdown",
@ -295,13 +417,13 @@
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 10,
"prompt_number": 14,
"text": [
"5"
]
}
],
"prompt_number": 10
"prompt_number": 14
},
{
"cell_type": "markdown",
@ -323,13 +445,13 @@
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 11,
"prompt_number": 15,
"text": [
"[1, 2, 2, 3, 5, 8, 13]"
]
}
],
"prompt_number": 11
"prompt_number": 15
},
{
"cell_type": "markdown",
@ -359,13 +481,13 @@
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 12,
"prompt_number": 16,
"text": [
"[1, 3, 5, 6, 7, 9]"
]
}
],
"prompt_number": 12
"prompt_number": 16
},
{
"cell_type": "markdown",
@ -388,13 +510,13 @@
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 13,
"prompt_number": 17,
"text": [
"['the', 'fox', 'over', 'quick', 'brown', 'jumps']"
]
}
],
"prompt_number": 13
"prompt_number": 17
},
{
"cell_type": "markdown",
@ -422,13 +544,13 @@
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 14,
"prompt_number": 18,
"text": [
"[1, 2, 5, 7, 8, 9]"
]
}
],
"prompt_number": 14
"prompt_number": 18
},
{
"cell_type": "code",
@ -442,13 +564,13 @@
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 15,
"prompt_number": 19,
"text": [
"[' ', ' ', 'a', 'a', 'b', 'b', 'f', 'o', 'o', 'r', 'z']"
]
}
],
"prompt_number": 15
"prompt_number": 19
},
{
"cell_type": "markdown",
@ -470,13 +592,13 @@
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 16,
"prompt_number": 20,
"text": [
"[1, 2, 5, 7, 8, 9, (1, 2), (4, 2)]"
]
}
],
"prompt_number": 16
"prompt_number": 20
},
{
"cell_type": "markdown",
@ -504,13 +626,13 @@
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 17,
"prompt_number": 21,
"text": [
"[(1, 2), (1, 2), (4, 2), 1, 5, 2, 9, 9, 7, 8, 1, 5, 2]"
]
}
],
"prompt_number": 17
"prompt_number": 21
},
{
"cell_type": "markdown",
@ -548,7 +670,7 @@
]
}
],
"prompt_number": 18
"prompt_number": 22
},
{
"cell_type": "markdown",
@ -578,13 +700,13 @@
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 19,
"prompt_number": 23,
"text": [
"[(1, 'foo'), (2, 'bar'), (3, 'baz')]"
]
}
],
"prompt_number": 19
"prompt_number": 23
},
{
"cell_type": "markdown",
@ -606,13 +728,13 @@
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 20,
"prompt_number": 24,
"text": [
"[(1, 'foo', True), (2, 'bar', False)]"
]
}
],
"prompt_number": 20
"prompt_number": 24
},
{
"cell_type": "markdown",
@ -641,7 +763,7 @@
]
}
],
"prompt_number": 21
"prompt_number": 25
},
{
"cell_type": "markdown",
@ -664,13 +786,13 @@
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 22,
"prompt_number": 26,
"text": [
"(1, 2, 3)"
]
}
],
"prompt_number": 22
"prompt_number": 26
},
{
"cell_type": "code",
@ -684,13 +806,13 @@
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 23,
"prompt_number": 27,
"text": [
"('one', 'two', 'three')"
]
}
],
"prompt_number": 23
"prompt_number": 27
}
],
"metadata": {}