Created
January 26, 2014 02:10
-
-
Save sarguido/8627109 to your computer and use it in GitHub Desktop.
A2 PyLadies Intro to Python part 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Intro to Python - Part 2\n", | |
"\n", | |
"For the next part of this class, we'll be working out of the text editor, instead of the interactive shell. We're going to learn a few more useful things and then start on our hangman program. Let's open up the file we created in part one, 'script.py'.\n", | |
"\n", | |
"## Comments\n", | |
"\n", | |
"A useful strategy for writing code is to write in comments. Comments are lines of code that the program doesn't execute, but are written in so you know what's going on in the code. In Python, comments start with the '#' symbol. You can write comments like this:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# This is a comment!" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"So let's go back to our file and comment out the line we wrote. Your line should look like this:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# print 'Hello, world!'" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 4 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Conditionals\n", | |
"\n", | |
"Conditionals are extremely important to writing programs. Sometimes, we don't want to execute everything we write all the time but only when certain critera is met (or under certain conditions). For that, we have if and else statements. \n", | |
"\n", | |
"### A note about whitespace and indentation\n", | |
"\n", | |
"Before we write some conditionals, there are a couple of things we need to know. First, whitespace is very important in Python. Writing conditionals (and loops and functions) creates code blocks which, if not properly indented, will throw an error when run. This can be annoying at first, but it ensures that everyone's code is readable and written in the same style. Hitting 'tab' to indent will do the trick. \n", | |
"\n", | |
"Back in our file, write the following, save the file, and then type 'python script.py' and hit enter in the command line:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"x = 5\n", | |
"\n", | |
"# If statement\n", | |
"if x == 5:\n", | |
" print 'x is equal to 5'" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"x is equal to 5\n" | |
] | |
} | |
], | |
"prompt_number": 5 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Let's make it a little more interesting. Add the following to your file so it looks like this, and run it again:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"x = 5\n", | |
"\n", | |
"# If statement\n", | |
"if x == 5:\n", | |
" print 'x is equal to 5'\n", | |
" \n", | |
"y = 10\n", | |
"\n", | |
"# If/else statement\n", | |
"if x > y:\n", | |
" print 'x is greater than y'\n", | |
"else:\n", | |
" print 'y is greater than x'" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"x is equal to 5\n", | |
"y is greater than x\n" | |
] | |
} | |
], | |
"prompt_number": 7 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Conditionals can also be extended, to check for multiple criteria. You can include multiple conditions in one line, and you can also extend them separately, with 'elif', which is short for 'else if'. Edit your code so it looks like the following:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"x = 5\n", | |
"y = 10\n", | |
"z = 15\n", | |
"\n", | |
"# If/else statement\n", | |
"\n", | |
"# Checking if y is both greater than x and less than z\n", | |
"if x < y and y < z:\n", | |
" print 'y is in the middle'\n", | |
"# Checking if z is less than y or z is less than x\n", | |
"elif z < y or z < x:\n", | |
" print 'z is less'" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"y is in the middle\n" | |
] | |
} | |
], | |
"prompt_number": 8 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Only the first print statement gets executed, because x is less than y and y is less than z. Z isn't less than either of the other two variables, so the second print statement doesn't get executed." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Data structures\n", | |
"\n", | |
"A large part of programming is storing collections of information. There are several ways to do this is Python, but we're going to talk about two of them: lists and dictionaries.\n", | |
"\n", | |
"### Lists\n", | |
"\n", | |
"A list is simply an ordered collection of elements. A list can be made up of variables, strings, numbers, or even other lists. Lists look like this:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"my_list = ['item1', 'item2', 'item3']\n", | |
"print my_list" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"['item1', 'item2', 'item3']\n" | |
] | |
} | |
], | |
"prompt_number": 4 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"with square brackets, and with the items separated by commas. Lists are mutable, meaning that you can change the values of a list within a program. Lists also have several methods, meaning that they have built-in functions you can use on them. A particularly useful method is 'append', which adds an item onto the end of a list:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"my_list.append('item4')\n", | |
"print my_list" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"['item1', 'item2', 'item3', 'item4']\n" | |
] | |
} | |
], | |
"prompt_number": 5 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"List items are also indexed. This means that you can access each item in the list based on its numerical position. Lists are zero-indexed, meaning that the first item has an index number of 0, the second number has an index number of 1, and so on:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print my_list[0]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"item1\n" | |
] | |
} | |
], | |
"prompt_number": 6 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print my_list[3]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"item4\n" | |
] | |
} | |
], | |
"prompt_number": 8 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Let's add a list to our script.py file. We'll create a list with the variables we already have, go into our if/else statement, and if the first condition is met, we'll add another number to our list. You can see that it is, and that 20 gets appended to our list. We also print out both the third list item as well as the last list item:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"x = 5 \n", | |
"y = 10\n", | |
"z = 15\n", | |
"\n", | |
"# List of our variables\n", | |
"numbers = [x, y, z]\n", | |
"\n", | |
"# Checking if y is both greater than x and less than z\n", | |
"if x < y and y < z:\n", | |
"\tprint 'y is in the middle'\n", | |
"\tnumbers.append(20) # If this is true, append 20 to our numbers list\n", | |
"\n", | |
"# Checking if z is less than y or less than x\n", | |
"elif z < y or z < x:\n", | |
" print 'z is less'\n", | |
"\n", | |
"print numbers\n", | |
"\n", | |
"print numbers[2] # Print the third list item\n", | |
"print numbers[-1] # Print the last list item" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"y is in the middle\n", | |
"[5, 10, 15, 20]\n", | |
"15\n", | |
"20\n" | |
] | |
} | |
], | |
"prompt_number": 10 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Dictionaries\n", | |
"\n", | |
"Another useful data structure is the dictionary. Dictionaries are made up of key/value pairs, meaning that you can access the items in a dictionary through its keys (similar to how you can access items in a list based on its indices). Dictionaries are surrounded by curly braces, and key/value pairs are designated with a colon:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"my_dict = {'item1': 20, 'item2': 30, 'item3': 40}\n", | |
"print my_dict" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"{'item2': 30, 'item3': 40, 'item1': 20}\n" | |
] | |
} | |
], | |
"prompt_number": 11 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"To access a value, you use the key:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print my_dict['item2']" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"30\n" | |
] | |
} | |
], | |
"prompt_number": 12 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"You'll notice that the dictionary items printed out in a different order than the order they were put in. Dictionaries, unlike lists, are unordered. You can add items to dictionaries though, by simply creating a new key/value pair. An important thing to note is that each key in a dictionary must be unique." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"my_dict['item4'] = 50\n", | |
"print my_dict" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"{'item2': 30, 'item3': 40, 'item1': 20, 'item4': 50}\n" | |
] | |
} | |
], | |
"prompt_number": 14 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"You can see that the 'item4':50 pair was added to the dictionary. Dictionaries have some useful methods. If you want to see all of the keys for a dictionary:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print my_dict.keys()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"['item2', 'item3', 'item1', 'item4']\n" | |
] | |
} | |
], | |
"prompt_number": 15 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"And if you want to see the values:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print my_dict.values()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[30, 40, 20, 50]\n" | |
] | |
} | |
], | |
"prompt_number": 16 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Let's add a dictionary to our script! Edit your code so it looks like this, and then run it:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"x = 5 \n", | |
"y = 10\n", | |
"z = 15\n", | |
"\n", | |
"# List of our variables\n", | |
"numbers = [x, y, z]\n", | |
"\n", | |
"# Checking if y is both greater than x and less than z\n", | |
"if x < y and y < z:\n", | |
"\tprint 'y is in the middle'\n", | |
"\tnumbers.append(20) # If this is true, append 20 to our numbers list\n", | |
"\n", | |
"# Checking if z is less than y or less than x\n", | |
"elif z < y or z < x:\n", | |
" print 'z is less'\n", | |
"\n", | |
"print numbers\n", | |
"\n", | |
"# Making a dictionary of the number of letters in each word\n", | |
"structures = {'lists': 5, 'dictionaries': 12}\n", | |
"print structures.keys()\n", | |
"\n", | |
"structures['items'] = 5\n", | |
"print structures" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"y is in the middle\n", | |
"[5, 10, 15, 20]\n", | |
"['dictionaries', 'lists']\n", | |
"{'items': 5, 'dictionaries': 12, 'lists': 5}\n" | |
] | |
} | |
], | |
"prompt_number": 17 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"We create a small dictionary, print out the keys, and then add another item to the dictionary." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Loops\n", | |
"\n", | |
"Sometimes, you'll want to repeat a task until you've gone through all of the items in a list or dictionary, or reached a certain point in a program. For iterative processes, we have for loops and while loops. " | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### For\n", | |
"\n", | |
"With for loops, you can access each item in a collection and do something with it. Here's a simple loop that prints out each number in the range of 1-10 (10 is not accessed)." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"for i in range(1,10):\n", | |
" print i" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"1\n", | |
"2\n", | |
"3\n", | |
"4\n", | |
"5\n", | |
"6\n", | |
"7\n", | |
"8\n", | |
"9\n" | |
] | |
} | |
], | |
"prompt_number": 6 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Let's write our own for loops! Create a new file in your text editor, name it 'loops.py', and save it to the desktop. Add in the following:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"x = 5 \n", | |
"y = 10\n", | |
"z = 15\n", | |
"\n", | |
"# List of our variables\n", | |
"numbers = [x, y, z]\n", | |
"\n", | |
"# For each item in the numbers list, print out\n", | |
"for num in numbers:\n", | |
"\tprint \"The number is\", num\n", | |
"\n", | |
"\t# If the item is less than 10, print the first condition\n", | |
"\t# If the item is greater than 10, print the second condition\n", | |
"\tif num < 10:\n", | |
"\t\tprint num, \"is less than 10\"\n", | |
"\telif num > 10:\n", | |
"\t\tprint num, \"is greater than 10\"" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"The number is 5\n", | |
"5 is less than 10\n", | |
"The number is 10\n", | |
"The number is 15\n", | |
"15 is greater than 10\n" | |
] | |
} | |
], | |
"prompt_number": 3 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"In our for loop, we start with our variable x, which has the value of 5. We first print out \"The number is 5\". Then, we check to see if 5 is less than 10. Since it is, we print out that 5 is less than 10. That completes the loop for our first list item, so we move on to variable y, the second list item. Since 10 is not greater nor less than itself, neither of the conditionals gets printed out. And then we look at the final variable in the list, z." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### While\n", | |
"\n", | |
"Another loop we can use is the while loop. The while loop basically says that while something is going on, do something else. Here's the simple for loop from above, written as a while loop." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"i = 1\n", | |
"while i < 10:\n", | |
" print i\n", | |
" i = i + 1" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"1\n", | |
"2\n", | |
"3\n", | |
"4\n", | |
"5\n", | |
"6\n", | |
"7\n", | |
"8\n", | |
"9\n" | |
] | |
} | |
], | |
"prompt_number": 7 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"In this loop, we first give i the value of 1. Then, while i is less than 10, we first print out the value of i, and then we add one to the value i. The second time we go through the loop, i now has the value of 2, and so on.\n", | |
"\n", | |
"Let's add a while loop to our loops.py script:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"x = 5 \n", | |
"y = 10\n", | |
"z = 15\n", | |
"\n", | |
"# List of our variables\n", | |
"numbers = [x, y, z]\n", | |
"\n", | |
"# For each item in the numbers list, print out\n", | |
"for num in numbers:\n", | |
"\tprint \"The number is\", num\n", | |
"\n", | |
"\t# If the item is less than 10, print the first condition\n", | |
"\t# If the item is greater than 10, print the second condition\n", | |
"\tif num < 10:\n", | |
"\t\tprint num, \"is less than 10\"\n", | |
"\telif num > 10:\n", | |
"\t\tprint num, \"is greater than 10\"\n", | |
"\n", | |
"# While x is not equal to 0, print out the value of x\n", | |
"# Then subtract one from x and repeat, until x gets to be 0\n", | |
"while x != 0:\n", | |
"\tprint 'x is now', x\n", | |
"\tx = x - 1" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"The number is 5\n", | |
"5 is less than 10\n", | |
"The number is 10\n", | |
"The number is 15\n", | |
"15 is greater than 10\n", | |
"x is now 5\n", | |
"x is now 4\n", | |
"x is now 3\n", | |
"x is now 2\n", | |
"x is now 1\n" | |
] | |
} | |
], | |
"prompt_number": 8 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Functions\n", | |
"\n", | |
"Let's move on to functions now. Functions are named units of code that perform specific tasks. We've already encountered some functions:\n", | |
"\n", | |
"- append(): adds something to a list\n", | |
"- range(): supplies a range of numbers\n", | |
"- type(): tells you what the type of a value or variable is\n", | |
"- raw_input(): takes in user input\n", | |
"- keys(): grabs the keys from a dictionary\n", | |
"\n", | |
"You might have noticed that sometimes, when we call a function, we do so like this:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"numbers.append(20)\n", | |
"numbers" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 9, | |
"text": [ | |
"[5, 10, 15, 20]" | |
] | |
} | |
], | |
"prompt_number": 9 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"But other times, we call functions like this:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"type(numbers)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 10, | |
"text": [ | |
"list" | |
] | |
} | |
], | |
"prompt_number": 10 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"This is because some functions are bound to a specific type of object. A function like append() is specific to the type *list*, and a function like keys() is specific to the type *dict*. Functions that are bound to specific object types are called methods. Lists have their own specific methods, as do dictionaries, strings, and so on.\n", | |
"\n", | |
"Functions are more general, and aren't specific to a type of object. Type() can be called on anything, and functions like range() and raw_input() aren't related to a specific object. Range() and raw_input() do take specific parameters, however. A parameter is something that you pass into a function. For example, range() takes up to three parameters:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Prints from 0 to 10\n", | |
"print range(10)\n", | |
"\n", | |
"# Prints from a specific number to 10\n", | |
"print range(5, 10)\n", | |
"\n", | |
"# Prints from a specific number to 10, by specific increment\n", | |
"print range(2, 10, 2)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n", | |
"[5, 6, 7, 8, 9]\n", | |
"[2, 4, 6, 8]\n" | |
] | |
} | |
], | |
"prompt_number": 12 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Apart from built-in functions, we can create our own. To write a function, you have to define it, with def, followed by the name of the function, and then any parameters. This function simply prints out the value you have passed it:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def my_function(a):\n", | |
" print \"The value you passed in was\", a\n", | |
" \n", | |
"my_function(13)\n", | |
"my_function('Python')" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"The value you passed in was 13\n", | |
"The value you passed in was Python\n" | |
] | |
} | |
], | |
"prompt_number": 17 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Let's add in some functions to our loops script. Edit it so it looks like the following:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# You should usually declare functions at the beginning of your file\n", | |
"# Simple print function\n", | |
"def printing(item):\n", | |
"\tprint \"The number is\", item\n", | |
"\n", | |
"x = 5 \n", | |
"y = 10\n", | |
"z = 15\n", | |
"\n", | |
"# List of our variables\n", | |
"numbers = [x, y, z]\n", | |
"\n", | |
"# For each item in the numbers list, print out\n", | |
"for num in numbers:\n", | |
"\tprinting(num)\n", | |
"\n", | |
"\t# If the item is less than 10, print the first condition\n", | |
"\t# If the item is greater than 10, print the second condition\n", | |
"\tif num < 10:\n", | |
"\t\tprint num, \"is less than 10\"\n", | |
"\telif num > 10:\n", | |
"\t\tprint num, \"is greater than 10\"\n", | |
"\n", | |
"# While x is not equal to 0, print out the value of x\n", | |
"# Then subtract one from x and repeat, until x gets to be 0\n", | |
"while x != 0:\n", | |
"\tprinting(x)\n", | |
"\tx = x - 1" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"The number is 5\n", | |
"5 is less than 10\n", | |
"The number is 10\n", | |
"The number is 15\n", | |
"15 is greater than 10\n", | |
"The number is 5\n", | |
"The number is 4\n", | |
"The number is 3\n", | |
"The number is 2\n", | |
"The number is 1\n" | |
] | |
} | |
], | |
"prompt_number": 19 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"This function takes in whatever the value is and prints out a simple sentence, printing out different values when the value changes in our loops.\n", | |
"\n", | |
"## import\n", | |
"\n", | |
"The last thing we'll cover before getting into our hangman program is the import statement. One of the great things about Python is all of the modules you can use to make your programs even more awesome. There are modules for web development, machine learning, file input/output, and data visualization, among others. Let's look a couple we'll need for our hangman program.\n", | |
"\n", | |
"### Time\n", | |
"\n", | |
"The time module contains various time-related functions. We're going to be using it to affect the pacing of our program. Let's take a look at what the time module can do. The sleep() function delays output:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import time\n", | |
"\n", | |
"print \"Printing me now!\"\n", | |
"time.sleep(3)\n", | |
"print \"Printing me three seconds later!\"" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Printing me now!\n", | |
"Printing me three seconds later!" | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\n" | |
] | |
} | |
], | |
"prompt_number": 20 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Random\n", | |
"\n", | |
"The random module is useful for a variety of things, including generating random numbers, and more importantly for us, select a random choice. You can give the choice function a list of items and it'll randomly choose one from the list. Let's see how that works:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import random\n", | |
"\n", | |
"words = ['hello', 'world', 'i', 'love', 'programming', 'do', 'you']\n", | |
"print random.choice(words)\n", | |
"print random.choice(words)\n", | |
"print random.choice(words)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"hello\n", | |
"world\n", | |
"programming\n" | |
] | |
} | |
], | |
"prompt_number": 25 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"This ends part 2. Let's put together everything we've learned, learn a few new things, and write a hangman program!" | |
] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment