Added snippet for range and xrange.

This commit is contained in:
Donne Martin 2015-01-25 06:02:30 -05:00
parent 09fb505a19
commit a3ca7175e5

View File

@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:e8f1f87360c2cd61701f17bc189a56ae6bab746a065f05366ae4bd3aa5539639"
"signature": "sha256:13e6414e26c196bc6cbaf0093750187f11c2637e20975d01f8e38416d518abb0"
},
"nbformat": 3,
"nbformat_minor": 0,
@ -278,7 +278,7 @@
]
}
],
"prompt_number": 13
"prompt_number": 8
},
{
"cell_type": "code",
@ -294,13 +294,13 @@
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 8,
"prompt_number": 9,
"text": [
"<function split>"
]
}
],
"prompt_number": 8
"prompt_number": 9
},
{
"cell_type": "markdown",
@ -350,7 +350,7 @@
]
}
],
"prompt_number": 9
"prompt_number": 10
},
{
"cell_type": "markdown",
@ -389,7 +389,7 @@
]
}
],
"prompt_number": 10
"prompt_number": 11
},
{
"cell_type": "code",
@ -441,7 +441,7 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 36
"prompt_number": 13
},
{
"cell_type": "code",
@ -464,13 +464,13 @@
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 37,
"prompt_number": 14,
"text": [
"7.0"
]
}
],
"prompt_number": 37
"prompt_number": 14
},
{
"cell_type": "code",
@ -493,14 +493,124 @@
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 38,
"prompt_number": 15,
"text": [
"'a'"
]
}
],
"prompt_number": 38
"prompt_number": 15
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## range and xrange"
]
},
{
"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",
"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": "code",
"collapsed": false,
"input": [
"# Range can take start, stop, and step arguments\n",
"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": "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",
" print(val)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1\n",
"2\n",
"3\n"
]
}
],
"prompt_number": 18
},
{
"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",
" sum += 1\n",
"print(sum)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"50000\n"
]
}
],
"prompt_number": 19
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 19
}
],
"metadata": {}
}