Skip to content

Instantly share code, notes, and snippets.

@katyhuff
Created November 4, 2012 19:47
Show Gist options
  • Save katyhuff/4013292 to your computer and use it in GitHub Desktop.
Save katyhuff/4013292 to your computer and use it in GitHub Desktop.
This is a python notebook giving a quick rundown of Control Flow, for the Software Carpentry Teaching01 section.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "ControlFlow"
},
"nbformat": 2,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"source": [
"# Introduction to Control Flow",
"",
"__Software Carpentry Teaching01__ ",
"",
"Prepared by: Katy Huff"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from IPython.core.display import HTML",
"HTML('<img src=\"http://teaching.software-carpentry.org/wp-content/uploads/2012/09/ControlFlow.png\" height=\"500px\">')"
],
"language": "python",
"outputs": [
{
"html": [
"<img src=\"http://teaching.software-carpentry.org/wp-content/uploads/2012/09/ControlFlow.png\" height=\"500px\">"
],
"output_type": "pyout",
"prompt_number": 4,
"text": [
"<IPython.core.display.HTML at 0x13eaed0>"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"source": [
"",
"## 1. Repetition",
"",
"Computers are good at repetition. People are not."
]
},
{
"cell_type": "markdown",
"source": [
"### Loops",
"",
"Loops are typically constructed with \"for\" or \"while\" statements. ",
"In english, we might say something like \"I could eat all of the apples in this basket.\"",
"",
"In python, we might instead use a \"for loop\" to say:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"basket = [\"macintosh\", \"pink lady\", \"granny smith\", \"fuji\", \"spartan\"]",
"for apple in basket : ",
" print \"I ate a \" + apple "
],
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"I ate a macintosh",
"I ate a pink lady",
"I ate a granny smith",
"I ate a fuji",
"I ate a spartan"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"source": [
"Alternatively, we could say the same thing using a while loop : "
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"while basket :",
" print \"I ate a \" + basket.pop(0)"
],
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"I ate a macintosh",
"I ate a pink lady",
"I ate a granny smith",
"I ate a fuji",
"I ate a spartan"
]
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"source": [
"#### Exercise : Can you think of a way to do the same as above, but print the list backwards?"
]
},
{
"cell_type": "code",
"collapsed": true,
"input": [
"..."
],
"language": "python",
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"### Iterators",
"",
"Iterators indicate the items of a data structure over which the for or while statement indexes itself. ",
"In the **for** loop above, an iterator for a list data structure was used implicitly when we named ",
"the **apple** variable. Each **apple** was one item in the list object. ",
"",
"Other data structures have automatic iterators as well.",
"Let's make a dictionary of the variants and amounts of apples in the basket."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"basket_dict = {\"macintosh\":2, \"pink lady\":1, \"granny smith\":3, \"fuji\":1, \"spartan\":3}",
"for variant, amount in basket_dict.iteritems():",
" print \"I ate \" + str(amount) + \" \" + variant + \".\""
],
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"I ate 3 granny smith.",
"I ate 1 fuji.",
"I ate 3 spartan.",
"I ate 1 pink lady.",
"I ate 2 macintosh."
]
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"source": [
"Other types of iterators include iterators you create yourself using functions like **range()** and **len()**."
]
},
{
"cell_type": "markdown",
"source": [
"#### Exercise: Using len(), can you create your own iterator for a list object? (Use help to understand len())"
]
},
{
"cell_type": "code",
"collapsed": true,
"input": [
"basket = [\"macintosh\", \"pink lady\", \"granny smith\", \"fuji\", \"spartan\"]"
],
"language": "python",
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"",
"",
"## 2. Selection",
"",
"### Redirection",
"",
"Sometimes, you don't want to eat all of the apples. ",
"Even in moments of extreme gluttony, I care for my housemates and try not to eat the last of a certain variant of apple in the basket. ",
"So, I'll eat all but one of each variant of apple. ",
"If there was only one of some variant of apple, of course, I won't eat any of those."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"basket_dict = {\"macintosh\":2, \"pink lady\":1, \"granny smith\":3, \"fuji\":1, \"spartan\":3}",
"for variant, amount in basket_dict.iteritems():",
" if amount > 1 :",
" print \"I ate \" + str(amount-1) + \" \" + variant + \".\""
],
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"I ate 2 granny smith.",
"I ate 2 spartan.",
"I ate 1 macintosh."
]
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"source": [
"The **if** redirection loop helps the program to do different actions in different situations.",
"Related notions include **else**, **elif**, **while**, **continue**, and **break**."
]
},
{
"cell_type": "markdown",
"source": [
"",
"#### Exercise : Let's say you know you're the only member of your house that likes spartan apples. ",
"Try creating a new loop in which you eat all of the spartans and all but one of everything else. ",
"You may need to learn how to use **elif**."
]
},
{
"cell_type": "code",
"collapsed": true,
"input": [
"..."
],
"language": "python",
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"",
"### Conditionals",
"",
"The conditional we used above (amount > 1) helped to define the redirection. ",
"That conditional returns a boolean (that is, True(1) or False(0)). ",
"We used the greater than logical operator: **>**. ",
"Other logical operators include **==, !=, <, <=, >=, &&, ||, in, and, or**.",
"While loops and if loops accept boolean statements. ",
"Here are some boolean statements. ",
"Try guessing whether they'll return true or false, then check the answer.",
"Then, try to create some booleans of your own."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"True == 1"
],
"language": "python",
"outputs": [
{
"output_type": "pyout",
"prompt_number": 9,
"text": [
"True"
]
}
],
"prompt_number": 9
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"True == 0"
],
"language": "python",
"outputs": [
{
"output_type": "pyout",
"prompt_number": 10,
"text": [
"False"
]
}
],
"prompt_number": 10
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"False == True"
],
"language": "python",
"outputs": [
{
"output_type": "pyout",
"prompt_number": 11,
"text": [
"False"
]
}
],
"prompt_number": 11
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\"macintosh\" in basket_dict"
],
"language": "python",
"outputs": [
{
"output_type": "pyout",
"prompt_number": 12,
"text": [
"True"
]
}
],
"prompt_number": 12
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\"Ruby Red\" in basket_dict"
],
"language": "python",
"outputs": [
{
"output_type": "pyout",
"prompt_number": 13,
"text": [
"False"
]
}
],
"prompt_number": 13
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\"macintosh\" in basket_dict or \"Ruby Red\" in basket_dict"
],
"language": "python",
"outputs": [
{
"output_type": "pyout",
"prompt_number": 14,
"text": [
"True"
]
}
],
"prompt_number": 14
},
{
"cell_type": "code",
"collapsed": true,
"input": [
"..."
],
"language": "python",
"outputs": []
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment