Skip to content

Instantly share code, notes, and snippets.

@LeeMendelowitz
Last active August 29, 2015 13:55
Show Gist options
  • Save LeeMendelowitz/8738004 to your computer and use it in GitHub Desktop.
Save LeeMendelowitz/8738004 to your computer and use it in GitHub Desktop.
ipython notebook demonstrating Python generators and the any/all functions.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Example of calling `any` and `all`:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print any(l == 't' for l in 'python') # Returns True. Same as: 't' in 'python'\n",
"print all(l == 't' for l in 'python') # Returns False. Not all of the letters are 't'. "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"True\n",
"False\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Example of constructing a generator with a generator expression:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"g = (l == 't' for l in 'python')\n",
"print g.next() # False. 'p' is not equal to 't'\n",
"print g.next() # False. 'y' is not equal to 't'\n",
"print g.next() # True. 't' is equal to 't'"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"False\n",
"False\n",
"True\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Example of using a generator with a for loop:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"g = (l == 't' for l in 'python')\n",
"for value in g:\n",
" print value"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"False\n",
"False\n",
"True\n",
"False\n",
"False\n",
"False\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Example of calling `any` and `all` with various generators:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"g = (l == 't' for l in 'python')\n",
"print any(g)\n",
"print any((l == 't' for l in 'python')) # same thing\n",
"print any(l == 't' for l in 'python') # same thing. No double parentheses"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"True\n",
"True\n",
"True\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Illustrating how many values `any` and `all` consume before returning:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def t():\n",
" print 'In True!'\n",
" return True\n",
"\n",
"def f():\n",
" print 'In False!'\n",
" return False\n",
"\n",
"# Store functions to be called in a list\n",
"funcs = [t, f, f, f, t]\n",
"\n",
"def test_any():\n",
" # Pass a generator expression with function calls to any\n",
" print 'Testing any:'\n",
" print any(func() for func in funcs)\n",
" print 'done.\\n\\n'\n",
"\n",
"def test_all():\n",
" # Pass a generator expression with function calls to all\n",
" print 'Testing all:'\n",
" print all(func() for func in funcs)\n",
" print 'done.\\n\\n'\n",
"\n",
"\n",
"test_any() # Calls t() once and stops.\n",
"test_all() # Calls t(), then f(), then stops"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Testing any:\n",
"In True!\n",
"True\n",
"done.\n",
"\n",
"\n",
"Testing all:\n",
"In True!\n",
"In False!\n",
"False\n",
"done.\n",
"\n",
"\n"
]
}
],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment