Last active
August 29, 2015 14:15
-
-
Save mei-li/aba45adf4bc0e4183a61 to your computer and use it in GitHub Desktop.
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": "code", | |
"collapsed": false, | |
"input": [ | |
"# Swap variables Don't\n", | |
"a = 1\n", | |
"b = 2\n", | |
"print a, b\n", | |
"\n", | |
"tmp = b\n", | |
"b = a\n", | |
"a = tmp\n", | |
"\n", | |
"print a, b" | |
], | |
"language": "python", | |
"metadata": { | |
"slideshow": { | |
"slide_type": "slide" | |
} | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"1 2\n", | |
"2 1\n" | |
] | |
} | |
], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Swap variables Do\n", | |
"a = 1\n", | |
"b = 2\n", | |
"print a, b\n", | |
"\n", | |
"a, b = b, a\n", | |
"print a, b" | |
], | |
"language": "python", | |
"metadata": { | |
"slideshow": { | |
"slide_type": "slide" | |
} | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"1 2\n", | |
"2 1\n" | |
] | |
} | |
], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Unpacking tuples Don't\n", | |
"person_info = (\"Mary\", 27)\n", | |
"\n", | |
"name = person_info[0]\n", | |
"age = person_info[1]\n", | |
"\n", | |
"print name\n", | |
"print age" | |
], | |
"language": "python", | |
"metadata": { | |
"slideshow": { | |
"slide_type": "slide" | |
} | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Mary\n", | |
"27\n" | |
] | |
} | |
], | |
"prompt_number": 3 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Unpacking tuples Do\n", | |
"person_info = (\"Mary\", 27)\n", | |
"\n", | |
"## Do\n", | |
"name, age = person_info\n", | |
"print name\n", | |
"print age" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Mary\n", | |
"27\n" | |
] | |
} | |
], | |
"prompt_number": 4 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Iteration over incrementing numbers\n", | |
"## Don't do\n", | |
"for i in [0, 1, 2, 3, 4]:\n", | |
" print i\n", | |
"print \"----------------\"\n", | |
"\n", | |
"## Don't do\n", | |
"for i in range(5):\n", | |
" print i\n", | |
"print \"----------------\"\n", | |
"\n", | |
"## Do - in python 3 do range instead\n", | |
"for i in xrange(5):\n", | |
" print i" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"0\n", | |
"1\n", | |
"2\n", | |
"3\n", | |
"4\n", | |
"----------------\n", | |
"0\n", | |
"1\n", | |
"2\n", | |
"3\n", | |
"4\n", | |
"----------------\n", | |
"0\n", | |
"1\n", | |
"2\n", | |
"3\n", | |
"4\n" | |
] | |
} | |
], | |
"prompt_number": 5 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Iteration over list - Don't do\n", | |
"colors = ['red', 'green', 'blue', 'yellow']\n", | |
"\n", | |
"for i in range(len(colors)):\n", | |
" print colors[i]\n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"red\n", | |
"green\n", | |
"blue\n", | |
"yellow\n" | |
] | |
} | |
], | |
"prompt_number": 6 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Iteration over list - Do\n", | |
"colors = ['red', 'green', 'blue', 'yellow']\n", | |
"\n", | |
"for color in colors:\n", | |
" print color" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"red\n", | |
"green\n", | |
"blue\n", | |
"yellow\n" | |
] | |
} | |
], | |
"prompt_number": 7 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Iteration over list with index - Don't do\n", | |
"colors = ['red', 'green', 'blue', 'yellow']\n", | |
"\n", | |
"for i in range(len(colors)):\n", | |
" print i, '-->', colors[i]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"0 --> red\n", | |
"1 --> green\n", | |
"2 --> blue\n", | |
"3 --> yellow\n" | |
] | |
} | |
], | |
"prompt_number": 8 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Iteration over list with index - Do\n", | |
"colors = ['red', 'green', 'blue', 'yellow']\n", | |
"for i, color in enumerate(colors):\n", | |
" print i, '-->', color" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"0 --> red\n", | |
"1 --> green\n", | |
"2 --> blue\n", | |
"3 --> yellow\n", | |
"[(0, 'red'), (1, 'green'), (2, 'blue'), (3, 'yellow')]\n" | |
] | |
} | |
], | |
"prompt_number": 33 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Constructing list - Don't do\n", | |
"kids = [{\"name\": \"peter\", \"age\": 3}, {\"name\": \"leon\", \"age\": 0.3}, {\"name\": \"erwin\", \"age\": 0.2}, {\"name\": \"mirto\", \"age\": 2}]\n", | |
"names = []\n", | |
"for kid in kids:\n", | |
" names.append(kid[\"name\"])\n", | |
"print names" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"['peter', 'leon', 'erwin', 'mirto']\n" | |
] | |
} | |
], | |
"prompt_number": 10 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Constructing list - Do list comprehension\n", | |
"kids = [{\"name\": \"peter\", \"age\": 3}, {\"name\": \"leon\", \"age\": 0.3}, {\"name\": \"erwin\", \"age\": 0.2}, {\"name\": \"mirto\", \"age\": 2}]\n", | |
"names = [kid[\"name\"] for kid in kids]\n", | |
"print names\n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"['peter', 'leon', 'erwin', 'mirto']\n" | |
] | |
} | |
], | |
"prompt_number": 11 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Constructing list - Don't do\n", | |
"kids = [{\"name\": \"peter\", \"age\": 3}, {\"name\": \"leon\", \"age\": 0.3}, {\"name\": \"erwin\", \"age\": 0.2}, {\"name\": \"mirto\", \"age\": 2}]\n", | |
"\n", | |
"baby_names = []\n", | |
"for kid in kids:\n", | |
" if kid[\"age\"] < 1:\n", | |
" baby_names.append(kid[\"name\"])\n", | |
"print baby_names" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"['leon', 'erwin']\n" | |
] | |
} | |
], | |
"prompt_number": 12 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"\n", | |
"# Constructing list - Do list comprehension\n", | |
"kids = [{\"name\": \"peter\", \"age\": 3}, {\"name\": \"leon\", \"age\": 0.3}, {\"name\": \"erwin\", \"age\": 0.2}, {\"name\": \"mirto\", \"age\": 2}]\n", | |
"\n", | |
"baby_names = [kid[\"name\"] for kid in kids if kid[\"age\"] < 1]\n", | |
"print baby_names" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"['leon', 'erwin']\n" | |
] | |
} | |
], | |
"prompt_number": 36 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Get sum of all numbers until 100\n", | |
"# Don't do\n", | |
"total = 0\n", | |
"for num in range(101):\n", | |
" total += num\n", | |
"\n", | |
"## Do\n", | |
"total = sum(range(101))\n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 14 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Get sum of all squares of numbers until 100\n", | |
"# Don't do\n", | |
"total = 0\n", | |
"for num in range(101):\n", | |
" total += num**2\n", | |
"\n", | |
"# Do\n", | |
"total = sum([n**2 for n in range(101)])\n", | |
"\n", | |
"# Even Better - Generators\n", | |
"total = sum(n**2 for n in range(101))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 15 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Concatenate strings from list - Don't\n", | |
"names = [\"Betty\", \"Patty\", \"Johanna\", \"Ivana\", \"Luminitsa\"]\n", | |
"\n", | |
"s = names[0]\n", | |
"for name in names[1:]:\n", | |
" s += ', ' + name\n", | |
"print s" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Betty, Patty, Johanna, Ivana, Luminitsa\n" | |
] | |
} | |
], | |
"prompt_number": 16 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Concatenate strings from list - Do\n", | |
"names = [\"Betty\", \"Patty\", \"Johanna\", \"Ivana\", \"Luminitsa\"]\n", | |
"\n", | |
"s = ', '.join(names)\n", | |
"print s" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Betty, Patty, Johanna, Ivana, Luminitsa\n" | |
] | |
} | |
], | |
"prompt_number": 17 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Sorting - alphabetically\n", | |
"names = [\"Betty\", \"Patty\", \"Johanna\", \"Ivana\", \"Luminitsa\"]\n", | |
"\n", | |
"for name in sorted(names):\n", | |
" print name" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Betty\n", | |
"Ivana\n", | |
"Johanna\n", | |
"Luminitsa\n", | |
"Patty\n" | |
] | |
} | |
], | |
"prompt_number": 18 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"## Sorting descending\n", | |
"names = [\"Betty\", \"Patty\", \"Johanna\", \"Ivana\", \"Luminitsa\"]\n", | |
"\n", | |
"for name in sorted(names, reverse=True):\n", | |
" print name" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Patty\n", | |
"Luminitsa\n", | |
"Johanna\n", | |
"Ivana\n", | |
"Betty\n" | |
] | |
} | |
], | |
"prompt_number": 19 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Sorting by string length\n", | |
"names = [\"Betty\", \"Patty\", \"Johanna\", \"Ivana\", \"Luminitsa\"]\n", | |
"for name in sorted(names, key=len):\n", | |
" print name\n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Betty\n", | |
"Patty\n", | |
"Ivana\n", | |
"Johanna\n", | |
"Luminitsa\n" | |
] | |
} | |
], | |
"prompt_number": 20 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Sorting by number of a\n", | |
"def count_a(word):\n", | |
" return sum(1 for ch in word if ch == 'a')\n", | |
"\n", | |
"for name in sorted(names, key=count_a):\n", | |
" print name" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Betty\n", | |
"Patty\n", | |
"Luminitsa\n", | |
"Johanna\n", | |
"Ivana\n" | |
] | |
} | |
], | |
"prompt_number": 21 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Sorting by second tuple element\n", | |
"names = [(\"Mary\", 27), (\"John\", 28), \n", | |
" (\"Peter\", 17), (\"Helen\", 13)]\n", | |
"\n", | |
"from operator import itemgetter\n", | |
"\n", | |
"for name, age in sorted(names, key=itemgetter(1)):\n", | |
" print name\n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Helen\n", | |
"Peter\n", | |
"Mary\n", | |
"John\n" | |
] | |
} | |
], | |
"prompt_number": 22 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Counting with dictionaries - Don't do\n", | |
"colors = ['red', 'green', 'red', 'blue', 'green', 'red']\n", | |
"\n", | |
"d = {}\n", | |
"for color in colors:\n", | |
" if color not in d:\n", | |
" d[color] = 0\n", | |
" d[color] += 1\n", | |
"print d\n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"{'blue': 1, 'green': 2, 'red': 3}\n" | |
] | |
} | |
], | |
"prompt_number": 23 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Counting with dictionaries - Do\n", | |
"colors = ['red', 'green', 'red', 'blue', 'green', 'red']\n", | |
"d = {}\n", | |
"for color in colors:\n", | |
" d[color] = d.get(color, 0) + 1\n", | |
"print d" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"{'blue': 1, 'green': 2, 'red': 3}\n" | |
] | |
} | |
], | |
"prompt_number": 24 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Counting with dictionaries - Do alterrnative\n", | |
"colors = ['red', 'green', 'red', 'blue', 'green', 'red']\n", | |
"from collections import defaultdict\n", | |
"d = defaultdict(int)\n", | |
"for color in colors:\n", | |
" d[color] += 1\n", | |
"print d" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"defaultdict(<type 'int'>, {'blue': 1, 'green': 2, 'red': 3})\n" | |
] | |
} | |
], | |
"prompt_number": 25 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Grouping with dictionaries - Don't do\n", | |
"colors = ['gray', 'black', 'blue', 'green', 'red']\n", | |
"\n", | |
"d = {}\n", | |
"for color in colors:\n", | |
" first_char = color[0]\n", | |
" if first_char not in d:\n", | |
" d[first_char] = []\n", | |
" d[first_char].append(color)\n", | |
"print d\n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"{'r': ['red'], 'b': ['black', 'blue'], 'g': ['gray', 'green']}\n" | |
] | |
} | |
], | |
"prompt_number": 26 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Grouping with dictionaries - Do\n", | |
"from collections import defaultdict\n", | |
"d = defaultdict(list)\n", | |
"for color in colors:\n", | |
" first_char = color[0]\n", | |
" d[first_char].append(color)\n", | |
"print d \n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"defaultdict(<type 'list'>, {'r': ['red'], 'b': ['black', 'blue'], 'g': ['gray', 'green']})\n" | |
] | |
} | |
], | |
"prompt_number": 27 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Looping over dictionaries keys\n", | |
"city_music = {\"berlin\": \"techno\", \n", | |
" \"seville\": \"flamenco\", \n", | |
" \"new york\": \"jazz\"}\n", | |
"\n", | |
"for city in city_music:\n", | |
" print city\n", | |
" \n", | |
" \n", | |
"europe = ('berlin', 'seville', 'madrid', 'paris',)\n", | |
"\n", | |
"# to change the dictionary while looping - Do\n", | |
"for city in city_music.keys():\n", | |
" if city not in europe:\n", | |
" del city_music[city]\n", | |
"print city_music \n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"berlin\n", | |
"new york\n", | |
"seville\n", | |
"{'berlin': 'techno', 'seville': 'flamenco'}\n" | |
] | |
} | |
], | |
"prompt_number": 28 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Looping over dictionaries keys and values\n", | |
"city_music = {\"berlin\": \"techno\", \n", | |
" \"seville\": \"flamenco\", \n", | |
" \"new york\": \"jazz\"}\n", | |
"\n", | |
"for city in city_music:\n", | |
" print city, 'listens to', city_music[city] " | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"berlin listens to techno\n", | |
"new york listens to jazz\n", | |
"seville listens to flamenco\n" | |
] | |
} | |
], | |
"prompt_number": 29 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"\n", | |
"# Looping over dictionaries keys and values \n", | |
"# more readable\n", | |
"for city, music in city_music.items():\n", | |
" print city, 'listens to', music " | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"berlin listens to techno\n", | |
"new york listens to jazz\n", | |
"seville listens to flamenco\n" | |
] | |
} | |
], | |
"prompt_number": 30 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Looping over dictionaries keys and values \n", | |
"# more readable and faster\n", | |
"for city, music in city_music.iteritems():\n", | |
" print city, 'listens to', music" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"berlin listens to techno\n", | |
"new york listens to jazz\n", | |
"seville listens to flamenco\n" | |
] | |
} | |
], | |
"prompt_number": 31 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 31 | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment