Created
September 3, 2014 17:11
-
-
Save kanungo/9770a56ed004d0aa28cc 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": [ | |
"# Slide 3\n", | |
"mylist = [1,3,2,5,8]\n", | |
"# Create a list comprehension that squares every element in mylist\n", | |
"mynewlist = [elmnt*2 for elmnt in mylist]\n", | |
"print mynewlist" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Slide 4\n", | |
"mynames = ['Tom', 'Dick', 'Harry']\n", | |
"x = ['Hello ' + person for person in mynames]\n", | |
"print x\n", | |
"\n", | |
"myfloats = [1.2345, 2.3456, 3,4567]\n", | |
"x = [round(mynum,2) for mynum in myfloats]\n", | |
"print x\n", | |
"\n", | |
"def mysqplusone(a):\n", | |
" return a**2+1\n", | |
"mynums = [1,2,3]\n", | |
"x = [mysqplusone(i) for i in mynums]\n", | |
"print x\n", | |
"\n", | |
"a = [1,2,3]\n", | |
"b = [3,4,5]\n", | |
"x = [c+d for c in a for d in b]\n", | |
"print x" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Slide 7\n", | |
"mynames = ['Tom', 'Dick', 'Harry']\n", | |
"x = ['Hello ' + person for person in mynames if len(person) > 3]\n", | |
"print x\n", | |
"\n", | |
"myfloats = [1.2345, 2.3456, 3,4567]\n", | |
"x = [round(mynum,2) for mynum in myfloats if int(mynum)%2==0]\n", | |
"print x\n", | |
"\n", | |
"a = [1,2,3]\n", | |
"b = [3,4,5]\n", | |
"x = [(c,d) for c in a for d in b if c%2 == 0 and d%2 == 0]\n", | |
"print x" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Slide 8\n", | |
"# S = {x\u00b2 : x in {0 ... 9}}\n", | |
"S = [x**2 for x in range(10)]\n", | |
"print S\n", | |
"\n", | |
"# V = (1, 2, 4, 8, ..., 2\u00b9\u00b2)\n", | |
"V = [2**i for i in range(13)]\n", | |
"print V\n", | |
"\n", | |
"#M = {x | x in S and x even}\n", | |
"M = [x for x in S if x % 2 == 0]\n", | |
"print M" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment