Skip to content

Instantly share code, notes, and snippets.

@vanzaj
Created August 23, 2013 07:07
Show Gist options
  • Save vanzaj/6316347 to your computer and use it in GitHub Desktop.
Save vanzaj/6316347 to your computer and use it in GitHub Desktop.
Quick introduction to Python using IPython Notebook
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"from IPython.display import HTML, Image\n",
"from datetime import datetime\n",
"\n",
"title = \"Python for Scientists and Engineers\"\n",
"date = datetime(2013, 8, 22, hour=14)\n",
"speaker = \"Ivan Zimine\"\n",
"\n",
"tpl=\"\"\"\n",
"<center>\n",
"<h1>{0}</h1>\n",
"<h2>{1}</h2>\n",
"<h3>{2}</h3>\n",
"</center>\n",
"\"\"\"\n",
"\n",
"HTML(tpl.format(title, speaker, date.isoformat().replace('T', ' / ')))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n",
"<center>\n",
"<h1>Python for Scientists and Engineers</h1>\n",
"<h2>Ivan Zimine</h2>\n",
"<h3>2013-08-22 / 14:00:00</h3>\n",
"</center>\n"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 2,
"text": [
"<IPython.core.display.HTML at 0x102c66110>"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"&nbsp; \n",
"&nbsp; \n",
"\n",
"\n",
"## Plan\n",
"\n",
"- Introduction to Python\n",
" - Data types\n",
" - Functions\n",
" - Flow control\n",
"\n",
"- Python for Scientific applications\n",
" - IPython\n",
" - NumPy, Matplotlib\n",
" - Pandas, SciPy"
]
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Introduction to Python"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"HTML('<iframe src=\"http://en.m.wikipedia.org/wiki/Python_(programming_language)\" width=800 height=400></iframe>')"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<iframe src=\"http://en.m.wikipedia.org/wiki/Python_(programming_language)\" width=800 height=400></iframe>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 3,
"text": [
"<IPython.core.display.HTML at 0x102c3c850>"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Companies\n",
"- Google (original Search Engine, GMail, Groups, Maps)\n",
"- YouTube\n",
"- NASA\n",
"- Dropbox, Instagram\n",
"- ...\n",
"\n",
"## Applications\n",
"- Gimp, Blender, Inkscape\n",
"- Vim, Gedit, Eclipse (plugins)\n",
"- GNOME apps\n",
"- Mercurial, Bazaar (disctributed version control)\n",
"- Google App Engine, Django, Pyramid (web framewords)\n",
"- Trac (project/bugs/issues managment)\n",
"- Launchpad (Ubuntu's dev community)\n",
"- Twisted, Tornado (Facebook)\n",
"- OpenStack, StarCluster\n",
"- ..."
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Basics"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# this is a comment\n",
"\n",
"# standard arithmetics\n",
"(4 + 2) * 7"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 4,
"text": [
"42"
]
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# integer division\n",
"print 5/2\n",
"print 5.0/2\n",
"print 5.0//2\n",
"\n",
"# exponentiation\n",
"print 3**2"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"2\n",
"2.5\n",
"2.0\n",
"9"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# logical operations\n",
"a = True\n",
"print (5/2) < 2.3\n",
"a is True"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"True\n"
]
},
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 6,
"text": [
"True"
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print not False\n",
"print 3 != 3.0"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"True\n",
"False\n"
]
}
],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"'this is a string'\n",
"\"this is also a string\"\n",
"\n",
"\"\"\"this is a string\n",
"spanning multiple\n",
"lines\"\"\"\n",
"\n",
"\"hello\" + \" \" + \"world\" "
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 8,
"text": [
"'hello world'"
]
}
],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# formatting\n",
"\"%s is %.3f\" % (\"result\", 2.346e-1)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 9,
"text": [
"'result is 0.235'"
]
}
],
"prompt_number": 9
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Variables, names, data types"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"value = 3\n",
"Value = 5\n",
"value == Value"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 10,
"text": [
"False"
]
}
],
"prompt_number": 10
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"item_1 = \"milk\"\n",
"_item = None"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 11
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"help()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Welcome to Python 2.7! This is the online help utility.\n",
"\n",
"If this is your first time using Python, you should definitely check out\n",
"the tutorial on the Internet at http://docs.python.org/2.7/tutorial/.\n",
"\n",
"Enter the name of any module, keyword, or topic to get help on writing\n",
"Python programs and using Python modules. To quit this help utility and\n",
"return to the interpreter, just type \"quit\".\n",
"\n",
"To get a list of available modules, keywords, or topics, type \"modules\",\n",
"\"keywords\", or \"topics\". Each module also comes with a one-line summary\n",
"of what it does; to list the modules whose summaries contain a given word\n",
"such as \"spam\", type \"modules spam\".\n",
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"help> keywords\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Here is a list of the Python keywords. Enter any keyword to get more help.\n",
"\n",
"and elif if print\n",
"as else import raise\n",
"assert except in return\n",
"break exec is try\n",
"class finally lambda while\n",
"continue for not with\n",
"def from or yield\n",
"del global pass \n",
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"help> quit\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"You are now leaving help and returning to the Python interpreter.\n",
"If you want to ask for help on a particular object directly from the\n",
"interpreter, you can type \"help(object)\". Executing \"help('string')\"\n",
"has the same effect as typing a particular string at the help> prompt.\n"
]
}
],
"prompt_number": 12
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x = 1\n",
"print type(x)\n",
"print type(x * 1.0)\n",
"print type(str(1))\n",
"\n",
"print ''\n",
"\n",
"val = '2'\n",
"print \"%s * %s = %s\" % (val, 10, val*10)\n",
"\n",
"print ''\n",
"\n",
"print \"%s * %s = %s\" % (val, 10, int(val)*10)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"<type 'int'>\n",
"<type 'float'>\n",
"<type 'str'>\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"2 * 10 = 2222222222\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"2 * 10 = 20\n"
]
}
],
"prompt_number": 15
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Container data types"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# strings are \"indexable\" (sequence of characters)\n",
"x = \"abcde\"\n",
"print x[0], x[-1]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"a e\n"
]
}
],
"prompt_number": 17
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# lists (sequence of values of any type)\n",
"x = [1, 2.0, \"three\"]\n",
"print x[0], x[-1] # index starts at 0; negative index are allowed\n",
"print \"nb of elements:\", len(x)\n",
"print ''\n",
"\n",
"# changing an item\n",
"print \"before:\", x\n",
"x[1] = 100\n",
"print \"after:\", x\n",
"print ''\n",
"\n",
"# adding stuff (using append method)\n",
"x.append([100, 200])\n",
"print x\n",
"print ''\n",
"\n",
"x.extend([100,200])\n",
"print x\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1 three\n",
"nb of elements: 3\n",
"\n",
"before:"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" [1, 2.0, 'three']\n",
"after: [1, 100, 'three']\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"[1, 100, 'three', [100, 200]]\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"[1, 100, 'three', [100, 200], 100, 200]\n"
]
}
],
"prompt_number": 19
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# dictionaries (key-value mapping)\n",
"x = {\n",
" \"key\": \"value\", \n",
" 42: \"something else\",\n",
" 2.0: \"can, but why\",\n",
" (0,0): \"think lan,lot\"\n",
" }\n",
"\n",
"k = 42\n",
"print \"%r -> %r\" % (k, x[k])\n",
"k = (0,0)\n",
"print \"%r -> %r\" % (k, x[k])\n",
"print \"\"\n",
"\n",
"# adding another key\n",
"x[\"new\"] = \"another value\"\n",
"# access using get method\n",
"print x.get(\"new\")\n",
"print x.get(\"funny\", \"not set\")\n",
"print \"\"\n",
"\n",
"print \"all keys:\", x.keys()\n",
"print \"all values:\", x.values()\n",
"print \"all key-value pairs:\", x.items()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"42 -> 'something else'\n",
"(0, 0) -> 'think lan,lot'\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"another value\n",
"not set"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
"all keys: ['new', 42, (0, 0), 2.0, 'key']\n",
"all values:"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" ['another value', 'something else', 'think lan,lot', 'can, but why', 'value']\n",
"all key-value pairs: [('new', 'another value'), (42, 'something else'), ((0, 0), 'think lan,lot'), (2.0, 'can, but why'), ('key', 'value')]\n"
]
}
],
"prompt_number": 22
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# tuple (immutable list)\n",
"x = (1, 2.0, \"three\")\n",
"print x[0]\n",
"x[1] = 100"
],
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "'tuple' object does not support item assignment",
"output_type": "pyerr",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-23-30e8b0f04d0f>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2.0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"three\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mx\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m100\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1\n"
]
}
],
"prompt_number": 23
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Functions"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# function call\n",
"x = abs(-5)\n",
"\n",
"import math\n",
"\n",
"a = 15.0\n",
"print math.sin(a)**2 + math.cos(a)**2 == 1\n",
"\n",
"# direct import into current namespace\n",
"from random import randint\n",
"print randint(1, 100)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"True\n",
"79\n"
]
}
],
"prompt_number": 66
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import random as rnd\n",
"print rnd.randint(2, 100)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"63\n"
]
}
],
"prompt_number": 24
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# user defined function\n",
"def sq(x):\n",
" \"\"\"compute square of the argument\n",
" :param x: input number\n",
" \n",
" :usage:\n",
" res = sq(number)\n",
" \"\"\"\n",
" return x*x\n",
"\n",
"#### indentation is significant! (code blocks)\n",
"\n",
"print sq(3)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"9\n"
]
}
],
"prompt_number": 28
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"help(sq)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Help on function sq in module __main__:\n",
"\n",
"sq(x)\n",
" compute square of the argument\n",
" :param x: input number\n",
" \n",
" :usage:\n",
" res = sq(number)\n",
"\n"
]
}
],
"prompt_number": 29
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# funtions are values!\n",
"square = sq\n",
"print square(5)\n",
"\n",
"print map(sq, tuple(range(5)))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"25\n",
"[0, 1, 4, 9, 16]\n"
]
}
],
"prompt_number": 30
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# positional and keyword arguments\n",
"def func(arg1, arg2, kwd1=None, kwd2=True):\n",
" print \"positional arguments:\", arg1, arg2\n",
" print \"keyword arguments:\", kwd1, kwd2\n",
" \n",
"func(1, 10)\n",
"print ''\n",
"func(1, 10, kwd1=\"Something\")"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"positional arguments: 1 10\n",
"keyword arguments: None True\n",
"\n",
"positional arguments:"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" 1 10\n",
"keyword arguments: Something True\n"
]
}
],
"prompt_number": 31
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# scoping rules\n",
"out = 1\n",
"\n",
"def scope(arg):\n",
" out = 100\n",
" print \"inside scope\", out\n",
"\n",
"\n",
"print \"before scope call\", out\n",
"scope(None)\n",
"print \"after scope call\", out\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"before scope call 1\n",
"inside scope 100\n",
"after scope call 1\n"
]
}
],
"prompt_number": 32
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def scope2(arg):\n",
" global out # possible, but usually a bad idea\n",
" out = 100\n",
" print \"inside scope\", out\n",
" \n",
"print \"before scope call\", out\n",
"scope2(None)\n",
"print \"after scope call\", out"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"before scope call 1\n",
"inside scope 100\n",
"after scope call 100\n"
]
}
],
"prompt_number": 33
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# pass by reference\n",
"def func(x):\n",
" x = 105\n",
" return x\n",
"\n",
"y = 10\n",
"func(y)\n",
"print \"after func call\", y"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"after func call 10\n"
]
}
],
"prompt_number": 34
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# but!!! import string\n",
"\n",
"def func(x):\n",
" x[0] = 100\n",
" return x\n",
"\n",
"y = [5, 1, 2]\n",
"\n",
"print \"before:\", y\n",
"z = func(y)\n",
"print \"after func call:\", y"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"before: [5, 1, 2]\n",
"after func call: [100, 1, 2]\n"
]
}
],
"prompt_number": 36
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Flow control"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"pressure = 50\n",
"limit = 100\n",
"\n",
"if pressure > limit:\n",
" print \"Alarm! High pressure\"\n",
"elif pressure == limit:\n",
" print \"Warning! Pressure at the limit\"\n",
"else:\n",
" print \"All good\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"All good\n"
]
}
],
"prompt_number": 88
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"check = True\n",
"y = \"Yes\" if check else \"No\"\n",
"y"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 37,
"text": [
"'Yes'"
]
}
],
"prompt_number": 37
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# for(i=0; i<5; i++){}\n",
"\n",
"for n in xrange(5):\n",
" print \"step %d\" % n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"step 0\n",
"step 1\n",
"step 2\n",
"step 3\n",
"step 4\n"
]
}
],
"prompt_number": 38
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"browser = {\"name\": \"Chrome\", \"version\": 28.0, \"os\": \"Linux\"}\n",
"\n",
"for k,v in browser.items():\n",
" print \"%r: %r\" % (k, v)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"'version': 28.0\n",
"'os': 'Linux'\n",
"'name': 'Chrome'\n"
]
}
],
"prompt_number": 40
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Standard modules"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import sys, os, glob\n",
"\n",
"# current python version\n",
"print sys.version\n",
"print sys.version_info\n",
"print \"\"\n",
"\n",
"# access to environment\n",
"print os.environ[\"HOME\"]\n",
"\n",
"# OS independent path construction\n",
"pp = os.path.join( os.environ[\"HOME\"], \"Pictures\")\n",
"print pp\n",
"\n",
"# file globbing\n",
"fls = glob.glob(pp + \"/*.png\")\n",
"print fls"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"2.7.5 |Anaconda 1.6.1 (x86_64)| (default, Jun 28 2013, 22:20:13) \n",
"[GCC 4.0.1 (Apple Inc. build 5493)]\n",
"sys.version_info(major=2, minor=7, micro=5, releaselevel='final', serial=0)\n",
"\n",
"/Users/ivan"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"/Users/ivan/Pictures\n",
"['/Users/ivan/Pictures/brain_amist_canvas.png', '/Users/ivan/Pictures/ivz_conf_crop.png']"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 46
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import math, cmath\n",
"\n",
"z = 2+3j\n",
"print z, abs(z), type(z)\n",
"print \"\"\n",
"print cmath.exp(-math.pi * 1j)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"(2+3j) 3.60555127546 <type 'complex'>\n",
"\n",
"(-1-1.22464679915e-16j)\n"
]
}
],
"prompt_number": 47
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# content of a module\n",
"print dir(math)\n",
"print \"\"\n",
"\n",
"import random\n",
"x = range(20)\n",
"print x\n",
"print \"\"\n",
"\n",
"y = random.sample(x, 10)\n",
"print y"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"['__doc__', '__file__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']\n",
"\n",
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
"[8, 12, 2, 1, 19, 5, 17, 15, 6, 14]"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 48
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import string\n",
"\n",
"def gen_passwd(length=8, punct=False):\n",
" \"\"\"generate a string of composed of random letters and digits specified length \n",
" \"\"\"\n",
" chars = string.letters + string.digits\n",
" if punct:\n",
" chars += \"!@#$%^&*()[]{}-_+=\"\n",
" passwd = random.sample(chars, length)\n",
" return ''.join(passwd)\n",
"\n",
"\n",
"gen_passwd(8, punct=True)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 49,
"text": [
"'XTyUAzBE'"
]
}
],
"prompt_number": 49
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# regular expressions\n",
"import re\n",
"\n",
"pat = re.compile(r\"\\+?(\\d+)[- ]?(\\d+)\")\n",
"\n",
"phone = \"+65-12345678\"\n",
"\n",
"m = pat.search(phone)\n",
"\n",
"if len(m.groups()) > 1:\n",
" print \"country code:\", m.groups()[0]\n",
" print \"phone number:\", m.groups()[1]\n",
"else:\n",
" \"Failed to parse phone number\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"country code: 65\n",
"phone number: 12345678\n"
]
}
],
"prompt_number": 63
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import time\n",
"\n",
"t0 = time.time()\n",
"time.sleep(3.5)\n",
"t1 = time.time()\n",
"\n",
"print \"time spent:\", t1 - t0"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"time spent: 3.53882479668\n"
]
}
],
"prompt_number": 64
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import datetime\n",
"\n",
"today = datetime.datetime.today()\n",
"\n",
"print today + datetime.timedelta(days=30)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"2013-09-22 15:04:58.609572\n"
]
}
],
"prompt_number": 65
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import this"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The Zen of Python, by Tim Peters\n",
"\n",
"Beautiful is better than ugly.\n",
"Explicit is better than implicit.\n",
"Simple is better than complex.\n",
"Complex is better than complicated.\n",
"Flat is better than nested.\n",
"Sparse is better than dense.\n",
"Readability counts.\n",
"Special cases aren't special enough to break the rules.\n",
"Although practicality beats purity.\n",
"Errors should never pass silently.\n",
"Unless explicitly silenced.\n",
"In the face of ambiguity, refuse the temptation to guess.\n",
"There should be one-- and preferably only one --obvious way to do it.\n",
"Although that way may not be obvious at first unless you're Dutch.\n",
"Now is better than never.\n",
"Although never is often better than *right* now.\n",
"If the implementation is hard to explain, it's a bad idea.\n",
"If the implementation is easy to explain, it may be a good idea.\n",
"Namespaces are one honking great idea -- let's do more of those!\n"
]
}
],
"prompt_number": 66
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"try:\n",
" f = open('junk.txt', 'r')\n",
"except IOError:\n",
" print \"No such file\"\n",
" #f = open('default.txt')"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"No such file\n"
]
}
],
"prompt_number": 67
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Resources"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- [Think Python](http://www.greenteapress.com/thinkpython/) by Allen B. Downey\n",
"\n",
"- [Learn Python the Hard Way](http://learnpythonthehardway.org/) by Zed A. Shaw\n",
"\n",
"- [The Hitchhiker\u2019s Guide to Python](http://docs.python-guide.org/en/latest/) by Kenneth Reitz\n",
"\n",
"- [Python Module of the Week](http://pymotw.com/2/) by Doug Hellmann\n",
"\n",
"- [Learn X in Y minutes (where X == python)](http://learnxinyminutes.com/docs/python/) by Louie Dinh"
]
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment